vuex 2.0组件无法使用模块vuex访问操作方法
vuejs 2.0 components failed to access action methods with module vuex
我正在测试Vuejs 2.0 &Vuex与模块设计,但组件不能访问操作方法。
my component:
import {mapGetters, mapActions} from 'vuex'
export default {
computed: mapGetters({
clients: 'clients',
fields: 'fields'
}),
methods: mapActions({
init: 'init'
}),
created: () => {
console.log(this.init)
}
}
my module:
const state = {
'fields': [
{
'field': 'name',
'label': 'Nom'
},
{
'field': 'adresse',
'label': 'Adresse'
},
{
'field': 'amount',
'label': 'Amount'
},
{
'field': 'contact',
'label': 'Contact'
}
],
items : []
}
export const SET_CLIENTS = 'SET_CLIENTS'
const mutations = {
[SET_CLIENTS] (state, clients) {
state.items = clients;
}
}
const actions = {
init: ({ commit }, payload) => {
let clients = []
for(let i = 0; i < 100; i++){
clients.push({
'name': 'Client '+i,
'adresse': '14000 Caen',
'amount': '1000',
'contact': 'contact@client'+i+'.com'
})
}
commit(SET_CLIENTS, { clients })
}
}
const getters = {
clients (state) {
return state.items;
},
fields (state) {
return state.fields;
}
}
export default {
state,
mutations,
getters,
actions
}
商店创建:
import Vuex from 'vuex'
import clients from './modules/clients'
import filters from './modules/filters'
import Vue from 'vue'
Vue.use(Vuex)
export default new Vuex.Store({
modules: {
clients,
filters
}
})
所有项目代码都可以在这里获得:https://github.com/robynico/vuejs-2.0-modules
如果你测试它,你会看到init方法在组件创建时是未定义的。
提前感谢!
我认为你出口你的商店模块错误。试试这个:
Inside your module.js
:
export default {
state: {}, // define your state here
getter: {}, // define your getters here
actions: {}, // define your actions here
mutations: {} // define your mutations here
}
import Vue from 'vue'
import Vuex from 'vuex'
import module from './modules/module.js'
Vue.use(Vuex)
const store = new Vuex.Store({
modules: {
module // your moudle
}
})
export default store
相关文章:
- Javascript,访问一个主要对象模块模式中的每个对象
- 节点Js:How to catch a“;没有这样的文件或目录“;读取线模块出错
- 如何从模块链中调用函数.导出到节点中
- 尽管链接成功并已成功下载,但未找到NPM模块
- 节点是否需要模块传递带有方括号的arg?这是个错误吗
- 从模块内部访问Express装载路径
- RequireJS向模块传递配置总是返回undefined
- Node.js正在更改应用程序以使用集群模块
- 在Meteor项目中安装并包含npm模块后出错
- 模块模式和这个
- 将依赖外部库的UMD模块与browserfy捆绑在一起
- 找不到模块捆绑包
- JavaScript命名约定;静态“;类或模块
- 显示模块模式在Knockout中设置模型的新实例
- 有没有比在app.js上绑定模块名称更好的方法来动态加载视图模型和视图以显示模态
- Javascript模块系统I'I’我正在努力,但搞不明白
- 如何捆绑循环相关模块
- 如何捆绑commonjs模块,除非指定需要
- 在beforeEach期间将vuex模块状态传递给vue-router
- vuex 2.0组件无法使用模块vuex访问操作方法