【发布时间】:2018-05-28 14:50:00
【问题描述】:
我已经阅读了类似标题的问题,但由于它们的复杂性,我无法关注它们。我认为使用我的代码会更容易为我找到解决方案。我只会包含相关代码。
我的店铺是这样的: obs:我安装了vuex插件。
import Vue from 'vue';
import Vuex from 'vuex';
Vue.use(Vuex)
const state = {
titulo: "please, change title"
}
const mutations = {
changeTitle(state, title) {
state.title= title
}
}
export default new Vuex.Store({
state : state,
mutations : mutations
})
我的应用程序.vue
<template>
<div>
<show-title-component ></show-title-component>
<change-title-component></change-title-component>
</div>
</template>
<script>
import ShowTitleComponent from './components/ShowtitleComponent';
import ChangeTitleComponent from './components/ChangeTitleComponent';
import store from './vuex/store';
export default {
components: {ShowTitleComponent, ChangeTitleComponent},
store,
data: function() {
return {title: 'placeholder'}
}
}
</script>
产生错误的组件:
<template><div>{{ title}}</div></template>
<script>
export default {
name: "show-title-component",
computed: {
title() {
return this.$store.state.title /** error here */
}
}
}
</script>
【问题讨论】:
-
你需要把它包含在
main.js -
将其包含在您调用
new Vue的入口文件中。 -
我正在重做一本书的练习,以检查我是否已经修复了知识。我将我的与作者的进行了比较,找不到任何代码不匹配的地方。我做错的唯一一件事是将存储文件命名为 .vue 文件。在我将其更改为 .js (store.js) 后,它起作用了!
-
还有一件事,在我将文件系统上的 store.vue 文件重命名为 store.js 后,我必须重新启动服务器,否则我会得到“store.vue”未找到,或者类似的东西, 在控制台上。
标签: typescript vue.js vuejs2