我正在为Vue.js运行一些测试代码,并通过脚本标签包含Vue.js,Vuex和javascript文件(因为它仅用于测试目的,我不想使用构建工具) .
大多数代码运行正常,但Vuex映射函数(mapState,mapGetters ...)将无法正常工作 . 我总是得到 ReferenceError: Can't find variable: mapState
. 为什么't I access the mapState? Aren' t通过脚本标记包含全局函数?
只是使用vue文档中的代码的示例:
的index.html
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
<title></title>
</head>
<body>
<div id="app"></div>
<!-- Libraries ---------- -->
<script src="vendor/js/vue.js" type="text/javascript"></script>
<script src="vendor/js/vuex.js" type="text/javascript"></script>
<script src="app/js/store.js" type="text/javascript"></script>
<script src="app/js/app.js" type="text/javascript"></script>
</body>
</html>
store.js
const state = {
count: 0
}
const getters = {
evenOrOdd: state => state.count % 2 === 0 ? 'even' : 'odd'
}
const mutations = {
increment (state) {
state.count++
},
decrement (state) {
state.count--
}
}
const actions = {
increment: ({ commit }) => commit('increment'),
decrement: ({ commit }) => commit('decrement'),
incrementIfOdd: ({ commit, state }) => {
if ((state.count + 1) % 2 === 0) {
commit('increment')
}
},
incrementAsync: ({ commit }) => {
return new Promise((resolve, reject) => {
setTimeout(() => {
commit('increment')
resolve()
}, 1000)
})
}
}
const store = new Vuex.Store({
state,
getters,
mutations,
actions
})
app.js
const app = new Vue({
el: '#app',
template: `
<main>
<h1 class="title">Heading</h1>
</main>
`,
store,
computed: {
...mapState([count])
}
});