store.js方法:
actions: {
async getBtnPermission({ commit }) {
const res = await getBtnCode();
const arr = res.split(',');
console.log('按钮权限列表', arr);
commit('updateBtnPerm', arr);
},
},
main.js内使用:
// 按钮权限指令
Vue.directive('has', {
inserted: (el, binding) => {
let actionList = store.dispatch('getBtnPermission');
console.log('actionList', actionList);
let values = binding.value;
let hasPermission = actionList.includes(values);
if (!hasPermission) {
el.style = 'display:none';
setTimeout(() => {
el.parentNode.removeChild(el);
}, 0);
}
},
});
为什么会报actionList.includes is not a function?
await
呢?async
的返回值是个promise
。我看你也打印了
actionList
。昂。
actions
的返回是什么呢?commit
是指触发mutation
所以你应该
await
一下,然后从store
中读取一下actionList