如何在插件中使用 Vuex 存储
How to use Vuex store in a plugin
我有一个自定义 Vue 插件,它有一个自定义实例方法
import Echo from 'laravel-echo';
import Vue from 'vue';
import config from '@/config';
const echor = {
install(Vue){
Vue.prototype.$echo = options => {
return new Echo({
// how can I get store value inside an instance methods?
auth: {
headers: {
Authorization: `${store.state.auth.token.token_type} ${store.state.auth.token.access_token}`
}
}
});
}
}
};
Vue.use(echor);
如何在实例方法中获取存储值以使 Authorization
header 有效?我用谷歌搜索,但没有任何帮助。
非常感谢!
您可以在插件的 install
方法中为商店添加一个附加参数:
install(Vue, store){
...
}
您插件的使用者会这样称呼您的插件:
import echor from '@/echor'; // or however you distribute it
import store from '@/store'; // user's own store
Vue.use(echor, store);
您可以通过这种方式向您的安装函数添加任意数量的附加参数。
我有一个自定义 Vue 插件,它有一个自定义实例方法
import Echo from 'laravel-echo';
import Vue from 'vue';
import config from '@/config';
const echor = {
install(Vue){
Vue.prototype.$echo = options => {
return new Echo({
// how can I get store value inside an instance methods?
auth: {
headers: {
Authorization: `${store.state.auth.token.token_type} ${store.state.auth.token.access_token}`
}
}
});
}
}
};
Vue.use(echor);
如何在实例方法中获取存储值以使 Authorization
header 有效?我用谷歌搜索,但没有任何帮助。
非常感谢!
您可以在插件的 install
方法中为商店添加一个附加参数:
install(Vue, store){
...
}
您插件的使用者会这样称呼您的插件:
import echor from '@/echor'; // or however you distribute it
import store from '@/store'; // user's own store
Vue.use(echor, store);
您可以通过这种方式向您的安装函数添加任意数量的附加参数。