找不到CSS和JS的Vue dist路径

问题描述 投票:1回答:1

构建后呈现我的VueJS应用程序时遇到麻烦。当我查看build文件夹并检查路径时,到我的资产,css和javascript的路径的开头有一个斜线。这使得无法找到我的CSS,资产和JavaScript。我想知道如何在运行构建脚本之前避免出现此问题。谢谢。这是我的代码:

App.vue

<template>
  <div id="app">
    <BaseTimer />
    <!-- <PomodoroTimer /> -->
  </div>
</template>

<script>
import BaseTimer from "./components/BaseTimer";
// import PomodoroTimer from "./components/PomodoroTimer";

export default {
  name: "App",

  components: {
    BaseTimer
    // PomodoroTimer
  }
};
</script>

<style>
body {
  background-color: #fff;
}

#app {
  font-family: "Avenir", Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  width: 100%;
}
</style>

BaseTimer.vue(在components文件夹内-src文件夹内)

<template>
  <div class="base-timer">
    <svg class="base-timer__svg" viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg">
      <g class="base-timer__circle">
        <circle class="base-timer__path-elapsed" cx="50" cy="50" r="45" />
        <path
          :stroke-dasharray="circleDasharray"
          class="base-timer__path-remaining"
          :class="remainingPathColor"
          d="
            M 50, 50
            m -45, 0
            a 45,45 0 1,0 90,0
            a 45,45 0 1,0 -90,0
          "
        />
      </g>
    </svg>
    <span class="base-timer__label">{{ formattedTimeLeft }}</span>
    <br />
    <select name="timer-setter" id="timer-setter" @change="selectedTime($event)">
      <option value selected disabled>---Select a Time---</option>
      <option v-for="second in seconds" :value="second.value" v-bind:key="second.id">{{second.text}}</option>
    </select>
    <button id="start" class="button is-dark is-large" @click="startTimer">GO</button>
    <div class="pause-or-kill">
      <!--     Pause Timer -->
      <button id="stop" class="button is-dark is-large" @click="pauseTimer">Pause</button>
      <!--     Restart Timer -->
      <button id="reset" class="button is-dark is-large" @click="restartTimer">Restart</button>
      <button id="kill" class="button" @click="kill">Reset</button>
    </div>
  </div>
</template>

<script>
let audio = new Audio(require("/assets/audio/ding.mp3"));

const FULL_DASH_ARRAY = 283;
const WARNING_THRESHOLD = 10;
const ALERT_THRESHOLD = 5;

const COLOR_CODES = {
  info: {
    color: "green"
  },
  warning: {
    color: "orange",
    threshold: WARNING_THRESHOLD
  },
  alert: {
    color: "red",
    threshold: ALERT_THRESHOLD
  }
};

let TIME_LIMIT = null;

export default {
  data() {
    return {
      timePassed: 0,
      timerInterval: null,
      seconds: [
        { id: 0, value: null, text: "--- Select a Time ---" },
        { id: 1, value: 20, text: "20 Seconds" },
        { id: 2, value: 30, text: "30 Seconds" },
        { id: 3, value: 60, text: "1 Minute" },
        { id: 4, value: 120, text: "2 Minutes" },
        { id: 5, value: 300, text: "5 Minutes" },
        { id: 6, value: 600, text: "10 Minutes" },
        { id: 7, value: 900, text: "15 Minutes" },
        { id: 8, value: 1800, text: "30 Minutes" },
        { id: 9, value: 3600, text: "1 Hour" }
      ]
    };
  },

  computed: {
    circleDasharray() {
      return `${(this.timeFraction * FULL_DASH_ARRAY).toFixed(0)} 283`;
    },

    formattedTimeLeft() {
      const timeLeft = this.timeLeft;
      const minutes = Math.floor(timeLeft / 60);
      let seconds = timeLeft % 60;

      if (seconds < 10) {
        seconds = `0${seconds}`;
      }

      return `${minutes}:${seconds}`;
    },

    timeLeft() {
      return TIME_LIMIT - this.timePassed;
    },

    timeFraction() {
      const rawTimeFraction = this.timeLeft / TIME_LIMIT;
      return rawTimeFraction - (1 / TIME_LIMIT) * (1 - rawTimeFraction);
    },

    remainingPathColor() {
      const { alert, warning, info } = COLOR_CODES;

      if (this.timeLeft <= alert.threshold) {
        return alert.color;
      } else if (this.timeLeft <= warning.threshold) {
        return warning.color;
      } else {
        return info.color;
      }
    }
  },

  watch: {
    timeLeft(newValue) {
      if (newValue === 0) {
        this.onTimesUp();
      }
    }
  },

  // startTimer() {
  //   this.startTimer();
  // },

  methods: {
    onTimesUp() {
      clearInterval(this.timerInterval);
      audio.play();
      setTimeout(function() {
        location.reload();
      }, 2000);
    },
    startTimer() {
      this.timerInterval = setInterval(() => (this.timePassed += 1), 1000);
    },
    restartTimer() {
      clearInterval(this.timerInterval);
      this.timerInterval = setInterval(() => (this.timePassed += 1), 1000);
    },
    pauseTimer() {
      clearInterval(this.timerInterval);
    },
    kill() {
      clearInterval(this.timerInterval);
      location.reload();
    },
    selectedTime(event) {
      console.log(event.target.value);
      TIME_LIMIT = event.target.value;
    }
  }
};
</script>

<style scoped lang="scss">
.base-timer {
  position: relative;
  width: 300px;
  height: 300px;

  &__svg {
    transform: scaleX(-1);
  }

  &__circle {
    fill: none;
    stroke: none;
  }

  &__path-elapsed {
    stroke-width: 7px;
    stroke: grey;
  }

  &__path-remaining {
    stroke-width: 7px;
    stroke-linecap: round;
    transform: rotate(90deg);
    transform-origin: center;
    transition: 1s linear all;
    fill-rule: nonzero;
    stroke: currentColor;

    &.green {
      color: rgb(65, 184, 131);
    }

    &.orange {
      color: orange;
    }

    &.red {
      color: red;
    }
  }

  &__label {
    position: absolute;
    width: 300px;
    height: 300px;
    top: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 48px;
  }
}

select {
  margin-top: 30px;
  margin-right: 15px;
  padding: 15px;
  border-radius: 8px;
  background-color: rgb(65, 184, 131);
  color: #fff;
  font-size: 18px;
  -webkit-box-shadow: 10px 10px 20px -8px rgba(0, 0, 0, 0.75);
  -moz-box-shadow: 10px 10px 20px -8px rgba(0, 0, 0, 0.75);
  box-shadow: 10px 10px 20px -8px rgba(0, 0, 0, 0.75);
}

button {
  padding: 15px;
  font-size: 18px;
  background-color: rgb(65, 184, 131);
  border-radius: 8px;
  color: #fff;
  -webkit-box-shadow: 10px 10px 20px -8px rgba(0, 0, 0, 0.75);
  -moz-box-shadow: 10px 10px 20px -8px rgba(0, 0, 0, 0.75);
  box-shadow: 10px 10px 20px -8px rgba(0, 0, 0, 0.75);
  outline: none;
}
.pause-or-kill {
  margin-top: 20px;
}
</style>

src文件夹中的index.html

<!DOCTYPE html>
<html>

<head>
    <title>Vue Parcel</title>
    <meta charset="utf-8">
</head>

<body>
    <div id="app"></div>
    <script src="index.js"></script>
</body>

</html>

src文件夹内的index.js

import Vue from "vue";
import App from "./App.vue";

Vue.config.productionTip = false;


new Vue({
  render: h => h(App)
}).$mount("#app"); 
javascript css vue.js webpack vue-cli
1个回答
2
投票

默认情况下,路径指向服务器根目录。这表示如果在Web服务器(例如Apache / node)上静态地提供文件,则为/;例如,如果双击Windows,则甚至直接在c:\中提供文件。

假设您正在使用Vue CLI,您可以通过使用vue.config.js在项目根目录中编辑-或创建-publicPath来配置此位置。>

module.exports = {
  publicPath: process.env.NODE_ENV === 'production'
    ? '/your/path/to/dist/'
    : '/'
}

这使Vue可以按预期在/处为开发服务器提供服务,并在生产中使用其他路径。如果您想知道,CLI会自动设置process.env.NODE_ENV。修改此文件后,不要忘记重建。

© www.soinside.com 2019 - 2024. All rights reserved.