简体   繁体   English

如何在插件中使用 Vuex 存储

[英]How to use Vuex store in a plugin

I have a custom Vue plugin which has a custom instance method我有一个自定义 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);

How can I get store value inside an instance methods to make Authorization header works?如何在实例方法中获取存储值以使Authorization header 有效? I googled, but nothings help.我用谷歌搜索,但没有任何帮助。

Thanks a lot!非常感谢!

You can add an additional argument for the store to your plugin's install method:您可以将商店的附加参数添加到插件的install方法中:

install(Vue, store){
...
}

A consumer of your plugin will call your plugin like:您的插件的使用者会这样称呼您的插件:

import echor from '@/echor';  // or however you distribute it
import store from '@/store';  // user's own store

Vue.use(echor, store);

You can add any number of additional arguments this way to your install function.您可以通过这种方式将任意数量的附加 arguments 添加到您的安装 function。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM