gpt4 book ai didi

javascript - Vuex 未捕获的语法错误和标识符

转载 作者:搜寻专家 更新时间:2023-10-30 22:47:58 24 4
gpt4 key购买 nike

我正在学习 Vuex,但在运行 mapState 时遇到了挑战,我认为 mapGetters 和 mapMutations 也会带来同样的挑战。

我在非节点环境中的 xampp localhost 中运行代码,但我不断收到类似

的错误
Uncaught SyntaxError: Unexpected token { 

下面是我的代码:

index.html

<script src="./vue.js"></script>
<!--<script src="./vuex.min.js"></script>-->

<div id="app">
<counter></counter>
<p>
<button @click="increment">+</button>
<button @click="decrement">-</button>
</p>
</div>
<script src="./example.js"></script>

例子.js

import { mapState } from './vuex.min';

const store = new Vuex.Store({
state: {
count: 0
},
mutations: {
increment: state => state.count++,
decrement: state => state.count--
}
})

const Counter = {
template: `<div>{{ count }}</div>`,
/*computed: {
count () {
return this.$store.state.count
}
}*/
computed:mapState({})

/*
I have inserted mapState this way

though the example given is

import { mapState } from 'vuex';

export default {
computed: mapState({
//some codes here
})
}

*/

}

const app = new Vue({
el: '#app',
store,
components: { Counter },
methods: {
increment () {
store.commit('increment')
},
decrement () {
store.commit('decrement')
}
}
})

我确信某处一定有错误或尚未声明某些内容以使其正常工作,这就是我寻求帮助的原因;我已经尝试了很多方法,甚至将 Vue devtool 扩展安装到 chrome 中,但无法启动 devtool 来让我在 vue devtool 中运行代码。

最佳答案

您不能从压缩(生产)构建中导出 ES6。您需要使用 Vuex.mapState()

导出

这是工作示例。

https://jsfiddle.net/exckyse3/

关于javascript - Vuex 未捕获的语法错误和标识符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48165090/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com