【问题标题】:How to add plugin to all vuex actions?如何将插件添加到所有 vuex 操作?
【发布时间】:2017-11-26 14:59:07
【问题描述】:

我可以将我的插件添加到所有 Vue 组件中,例如使用 Vue.use(myPlugin),然后通过 this.$myPlugin 访问它。

我想从 vuex 调用我的插件。

我怎样才能用 Vuex 存储操作做同样的事情?

插件是这样的:

const SwaggerApi = {
  install(Vue, options) {
    Swagger({
      url: 'http://localhost:3000/explorer/swagger.json',
      requestInterceptor(req) {
        req.headers['Accept'] = 'application/json'
        req.headers['content-type'] = 'application/json'
        req.headers['Access-Control-Allow-Origin'] = 'http://localhost:3000'
        req.headers['Authorization'] = localStorage.getItem('token')
        return req
      }
    })
      .then((client) => {
        // console.log(client)
        client.spec.basePath = '/api'
        Vue.prototype.$swagg = client.apis // <=== What should be here?

      }, (error) => {
        console.error('failed to load api spec: %o', error)
      })
  }
}

我尝试在我的商店中添加 Vue.use(SwaggerApi),但 'Vuex.use(SwaggerApi)' 和 `plugins:[SwaggerApi]' 无济于事。

【问题讨论】:

    标签: javascript vuejs2 vuex


    【解决方案1】:

    好的,我找到了。它是Vuex.Store.$swagg = client.apis,然后是Vuex.Store.$swagg.myModel 等等。

    【讨论】:

      猜你喜欢
      • 2017-06-29
      • 2021-04-28
      • 1970-01-01
      • 2015-12-05
      • 2018-01-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多