我正在尝试使用vuex和Nuxt实现CASL。当我的应用程序启动且我已经登录时,尝试初始化能力规则时遇到问题。
基本上,我想获取规则并在应用启动时更新Ability实例。但是,当我尝试从存储中获取规则时,它将返回null
。目前,我需要注销并登录以获取规则。
store / ability.js
import ability from '../config/ability'
export const updateAbilities = store => {
ability.update(store['users/getRules']) // this does not work and returns null
return store.subscribe(mutation => {
if (mutation.type === 'users/setRules') {
ability.update(mutation.payload)
}
})
}
config / ability.js
import { Ability } from '@casl/ability'
export default new Ability()
store / index.js
import { updateAbilities } from './ability'
export const plugins = [updateAbilities]
感谢您的帮助。
答案 0 :(得分:0)
我最终从本地存储中获取了规则,并且可以正常工作。
import ability from '../config/ability'
export const updateAbilities = store => {
const vuexData = JSON.parse(localStorage.getItem('vuex'))
const rules = vuexData.users.rules
ability.update(rules)
return store.subscribe(mutation => {
if (mutation.type === 'users/setRules') {
ability.update(mutation.payload)
}
})
}