如何为所有 vuex 操作添加插件?
How to add plugin to all vuex actions?
我可以将我的插件添加到所有 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]' 无济于事。
好的,我找到了。它 Vuex.Store.$swagg = client.apis
然后 Vuex.Store.$swagg.myModel
等
我可以将我的插件添加到所有 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]' 无济于事。
好的,我找到了。它 Vuex.Store.$swagg = client.apis
然后 Vuex.Store.$swagg.myModel
等