0

我尝试使用的插件有问题。请注意,刷新页面后代码工作正常,但是在初始登录时,它设置为 watch ( createSession) 的突变没有正确响应。我不确定是否有人熟悉CASL包,但我认为问题不存在,但也许我需要做一些事情才能使插件正常工作。

这是插件ability.js

import { Ability } from '@casl/ability'

export const ability = new Ability()

export const abilityPlugin = (store) => {
    ability.update(store.state.rules)

    const rules = store.subscribe((mutation) => {
        switch (mutation.type) {
            case 'createSession':
            ability.update(mutation.payload.rules)
            break
            case 'destroySession':
            ability.update([{ actions: '', subject: '' }])
            break
          }
          console.log(ability.rules)
      })
      return rules
  }

这是我要导入的商店

import Vue from 'vue'
import Vuex from 'vuex'
import axios from 'axios'
import { abilityPlugin, ability as appAbility } from './utils/ability'
import storage from './utils/storage'

export const ability = appAbility
Vue.use(Vuex)
axios.defaults.baseURL = 'http://traxit.test/api'



export default new Vuex.Store({
  plugins: [
    storage({
      storedKeys: ['rules', 'token'],
      destroyOn: ['destroySession']
    }),
    abilityPlugin
  ],
  state: {
    rules: '',
token: localStorage.getItem('access_token') || null,
    sidebarOpen: true,
    loading: false,
  },
 mutations: {
    createSession(state, session) {
      state.rules = session[0]
      state.token = session.access_token
    },
}

我正在createSession使用来自初始登录操作的响应数据进行突变,这是在此处检索令牌和规则

retrieveToken({ commit }, credentials) {

      return new Promise((resolve, reject) => {
          axios.post('/login', {
              username: credentials.username,
              password: credentials.password,
          })
          .then(response => {
              const token = response.data.access_token

              localStorage.setItem('access_token', token)
              commit('createSession', response.data)
              resolve(response)
          })
          .catch(error => {
              console.log(error)
              reject(error)
          })
      })
    },

任何帮助将不胜感激!!我一直在这个问题上停留了一段时间..

4

1 回答 1

1

再次回答我自己的问题。哈哈

因此,在控制台登录后,mutation.payload我意识到我试图错误地访问数据。

我换了

case 'createSession':
        ability.update(mutation.payload.rules)
        break

对此

case 'createSession':
        ability.update(mutation.payload[0])
        break
于 2018-11-22T14:37:42.987 回答