[英]Vue.js Vite app works in development but crashes after building
I have built a website using vite and vue.js.我已经使用 vite 和 vue.js 建立了一个网站。 When I run npm run dev, everything works fine.
当我运行 npm 运行开发时,一切正常。 I built the app with npm run build but the app doesn't work anymore.
我使用 npm run build 构建了应用程序,但应用程序不再工作。 I am getting a blank page with an error saying "cannot read property of undefined (reading isDark)".
我得到一个空白页,错误提示“无法读取未定义的属性(读取 isDark)”。 error message
错误信息
here is my relevant app.vue code.这是我的相关 app.vue 代码。 This is the only component, that uses 'isDark':
这是唯一使用“isDark”的组件:
<script setup>
// This starter template is using Vue 3 <script setup> SFCs
// Check out https://vuejs.org/api/sfc-script-setup.html#script-setup
import Header from "./components/Header.vue";
import Footer from "./components/Footer.vue";
</script>
<template>
<div :class="'portfolio ' + (this.isDark ? 'dark' : '')">
<Header
@toggleDarkMode="() => toggleDarkMode()"
:dark="this ? this.isDark : false"
/>
<div
class="
content
transition-colors
duration-300
ease-linear
text-black
dark:text-white
bg-gray-300
dark:bg-gray-800
pt-16
"
>
<router-view />
</div>
<Footer />
</div>
</template>
<script>
export default {
name: "App",
emits: ["toggleDarkMode"],
beforeCreate() {
console.log(this.isDark);
console.log(this.isDark != null);
this.isDark = localStorage.getItem("darkMode") == "true";
console.log(this.isDark);
},
created() {
this.isDark = localStorage.getItem("darkMode") == "true";
},
methods: {
toggleDarkMode() {
if (this === undefinded) return;
this.isDark = !this.isDark;
localStorage.setItem("darkMode", this.isDark);
},
},
data() {
return {
isDark: true,
};
},
components: [Header],
};
</script>
<style scoped>
</style>
Use isDark in template without this for example例如,在没有这个的模板中使用 isDark
<Header
@toggleDarkMode="toggleDarkMode"
:dark="isDark"
/>
Or或者
<Header
@toggleDarkMode="toggleDarkMode"
:dark="!!isDark"
/>
Also you don't need anonymous function in event handler您也不需要在事件处理程序中匿名 function
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.