Can*_*nor 6 javascript inheritance extends vue.js vuex
我用VueJS和Vuex构建我的应用程序,当我有多个使用相同数据字段的模块时,我遇到了问题。它与API配置类似dat。
getUsers ({ state, commit }) {
axios.get(urls.API_USER_URL).then( response => {
let data = response.data;
parseApi(state, data, 'user');
}).catch( err => {
console.log('getUser error: ', err);
})
},
Run Code Online (Sandbox Code Playgroud)
其他模块中的另一个功能是
getPosts ({ state, commit }) {
axios.get(urls.API_POST_URL).then( response => {
let data = response.data;
parseApi(state, data, 'posts');
}).catch( err => {
console.log('getUser error: ', err);
})
},
Run Code Online (Sandbox Code Playgroud)
我想知道是否可以继承我的模块并在其中添加其他数据字段/函数?
我的每个模块都会有消息和状态字段,这些消息和状态字段是我对API的响应。
export default {
state : {
message : "",
status : 0
},
parseApi: function(state, data, property) {
if (data.hasOwnProperty('message')) {
state.message = data.message;
}
if (data.hasOwnProperty('status')) {
state.status = data.status;
}
if (data.hasOwnProperty(property)) {
state[property] = data[property];
}
}
}
Run Code Online (Sandbox Code Playgroud)
就像那样。
有没有一种方法可以编写一次此代码,并在每个Im使用的模块中使用它?
编辑:
我什至无法在其中获得此apiParse函数,我需要对这些字段进行更改。但是,一直重复下去是没有意义的……有什么建议吗?
Eri*_*rin 10
我将可重用的Vuex代码放在小类中。例如
crud.js
export default class {
constructor ( endpoint ) {
this.state = {
endpoint: endpoint,
meta: {},
status: null,
known: [],
currentId: null,
};
this.getters = {
id: state => id => state.known.find( o => o.id === id )
};
this.actions = {
async store( context, payload ) {
*(call to API)*
},
async update( context, payload ) {
*(call to API)*
},
*...etc*
};
this.mutations = {
STORED(state, item) {
state.known.push(item);
},
*...etc*
};
}
}
Run Code Online (Sandbox Code Playgroud)
然后,我可以在所有模块中使用它:
user.module.js
import Crud from '/crud';
var crud = new Crud('/api/users');
const state = {
...crud.state,
};
const getters = {
...crud.getters,
};
const actions = {
...crud.actions,
};
const mutations = {
...crud.mutations,
};
export default {
namespaced: true,
state,
getters,
actions,
mutations
};
Run Code Online (Sandbox Code Playgroud)
开发更多 Erin 的响应,您可以定义一个具有如下常见功能的基类:
export default class BaseModule {
protected state() {
return {
isLoading: false,
};
};
protected getters() {
return {
isLoading(s) {
return s.isLoading;
},
};
};
protected actions() {
return {};
};
protected mutations() {
return {
[START_TRANSACTION]: (s) => {
s.isLoading = true;
},
[END_TRANSACTION]: (s) => {
s.isLoading = false;
},
};
}
protected modules() {
return {};
};
public getModule = () => {
return {
namespaced: true,
state: this.state(),
getters: this.getters(),
actions: this.actions(),
mutations: this.mutations(),
modules: this.modules(),
};
}
}
Run Code Online (Sandbox Code Playgroud)
您现在可以通过类继承仅扩展/覆盖派生类中所需的部分;例如,如果您需要扩展模块...:
import BaseModule from './BaseModule';
import rowDensity from '@/store/modules/reusable/rowDensity';
export default class ItemListModule extends BaseModule {
protected modules() {
return {
...super.modules(),
rowDensity,
};
};
}
Run Code Online (Sandbox Code Playgroud)
最后,要将它们用作商店中的模块,您可以实例化它们并调用.getModule():
import Vue from 'vue';
import Vuex from 'vuex';
import ItemListModule from './modules/ItemListModule';
Vue.use(Vuex);
const debug = process.env.NODE_ENV !== 'production';
export const MODULE_NAMESPACES = {
List: 'list',
};
export default new Vuex.Store({
modules: {
[MODULE_NAMESPACES.List]: new ItemListModule().getModule(),
},
strict: debug,
});
Run Code Online (Sandbox Code Playgroud)