所以我正在开发vuxx作为商店的nuxt应用程序。 我想到了进行一些日志记录(向laravel后端发送简单的rest api请求)以记录用户的操作。
我发现,如果我在两个不同的模块中有两个名称相同的动作,则两个动作都将被执行。
这是可接受的做法吗?还是将纠正并消除未记录的行为?
正在发生的事情的快速方案:
store/index.js
import logging from './logging';
import search from './search';
const store = () =>
new Vuex.Store({
modules: {
logging,
search,
}
});
export default store;
store/search.js
const actions = {
search(state, query) {
// some search request and processing results
}
};
const search = {
state,
mutations,
actions,
};
export default search;
store/logging.js
const actions = {
search(state, query) {
log(...)
}
};
const logging = {
state: {},
mutations: {},
actions,
};
export default logging;
答案 0 :(得分:0)
我看到两种方式:
常量:
创建文件:constants/store.js
export const LOGGING_SEARCH = 'logging search';
export const SEARCH = 'search';
然后使用它,例如:
import { LOGGING_SEARCH } from '../constants/store.js'
const actions = {
[LOGGING_SEARCH](state, query) {
log(...)
}
};
const logging = {
state: {},
mutations: {},
actions,
};
export default logging;
并调用它
import { LOGGING_SEARCH } from '../constants/store.js'
store.dispatch(LOGGING_SEARCH, 'query');
名称解析的模块 doc
store / logging.js
const actions = {
search(state, query) {
log(...)
}
};
const logging = {
namespased: true,
state: {},
mutations: {},
actions,
};
export default logging;
并调用它
store.dispatch('logging/search', 'query');
名称解析的模块特别有用,特别是对于helpers