main.js
file. There, in addition to creating an instance of Vue, there is an import and a kind of Dependency Injection of all your global dependencies (directives, components, plugins). The larger the project, the more dependencies become, which, moreover, each have their own configuration. As a result, we get one huge file with all configurations.inject
. It does Dependency Injection in the root instance of Vue and in the store
object. And this means that in each component, in each storage function, the specified dependency will be available through this
.main.js
“lose weight” significantly, you will also be able to use dependencies anywhere in the application without any extra imports.$wait
property not only to the Vue instance, but also to the vuex store. Given the specifics of the plugin, it is extremely useful. For example, the store has an action that is called in several components. And in each case, you need to show the loader on some element. Instead of calling $wait.start('action')
and $wait.end('action')
before and after each call to action, you can simply call these methods once in the action itself. And this is much more readable and less verbose than dispatch('wait/start', 'action' {root: true})
. In the case of the store is syntactic sugar.src
- store
- App.vue
- main.js
main.js
looks like this: import Vue from 'vue'; import App from './App.vue'; import store from './store'; new Vue({ render: h => h(App), store }).$mount('#app');
plugins
directory in src
. Inside the directory are the index.js
and axios.js
.src
- plugins
-- index.js
-- axios.js
- store
- App.vue
- main.js
inject
function.axios.js
import axios from 'axios'; export default function (app) { // – , , interceptors .. axios.defaults.baseURL = process.env.API_BASE_URL; axios.defaults.headers.common['Accept'] = 'application/json'; axios.defaults.headers.post['Content-Type'] = 'application/json'; axios.interceptors.request.use(config => { ... return config; }); }
index.js
: import Vue from 'vue'; import axios from './axios'; export default function (app) { let inject = () => {}; // inject, Dependency Injection axios(app, inject); // Vue }
index.js
file also exports the function. This is done in order to be able to pass an app
object there. Now let's change main.js
and call this function.main.js
: import Vue from 'vue'; import App from './App.vue'; import store from './store'; import initPlugins from './plugins'; // // , Vue, , initPlugins const app = { render: h => h(App), store }; initPlugins(app); new Vue(app).$mount('#app'); // initPlugins
main.js
to a separate file.app
object to all our plugins is that within each plug-in we now have access to the store. You can freely use it by calling commit
, dispatch
, as well as by accessing store.state
and store.getters
.axios.js
import axios from 'axios'; export default function ({store: {dispatch, commit, state, getters}}) { ... }
src
- plugins
-- index.js
-- axios.js
- store
- App.vue
- main.js
Vue.use
, we will create our own simple plugin.vue-wait
does. This is quite a heavy library, so if you want to show a loader on a pair of buttons, it is better to abandon it. However, I could not resist its convenience and repeated in my project its basic functionality, including syntactic sugar in the store.plugins
directory - wait.js
wait
. He makes three simple steps:start
- sets the state of the object with the name action
to true
end
- removes from the state the property of the object with the name action
is
- gets from state a property of the object named action
wait.js
export default function ({store: {dispatch, getters}}, inject) { const wait = { start: action => dispatch('wait/start', action), end: action => dispatch('wait/end', action), is: action => getters['wait/waiting'](action) }; inject('wait', wait); }
index.js
: import Vue from 'vue'; import axios from './axios'; import wait from './wait'; export default function (app) { let inject = () => {}; Injection axios(app, inject); wait(app, inject); }
inject
function. // 2 : // name – , this. , Vue Dependency Injection // plugin – , this. , , let inject = (name, plugin) => { let key = `$${name}`; // app[key] = plugin; // app app.store[key] = plugin; // store // Vue.prototype Vue.use(() => { if (Vue.prototype.hasOwnProperty(key)) { return; } Object.defineProperty(Vue.prototype, key, { get () { return this.$root.$options[key]; } }); }); };
Vue.prototype.$appName = ' ';
and $appName
will be available in this
.vue-wait
plugin. They offer this implementation (the source code is cleaned for clarity): Vue.mixin({ beforeCreate() { const { wait, store } = this.$options; let instance = null; instance.init(Vue, store); // inject to store this.$wait = instance; // inject to app } });
inject
function inject
was borrowed from Nuxt. It looks a lot more right way than the global mixin, so I settled on it. Vue.use(() => { // , if (Vue.prototype.hasOwnProperty(key)) { return; } // , app Object.defineProperty(Vue.prototype, key, { get () { return this.$root.$options[key]; // , this } }); });
this.$wait
from any component, as well as any method in the store.src
- plugins
-- index.js
-- axios.js
-- wait.js
- store
- App.vue
- main.js
index.js
: import Vue from 'vue'; import axios from './axios'; import wait from './wait'; export default function (app) { let inject = (name, plugin) => { let key = `$${name}`; app[key] = plugin; app.store[key] = plugin; Vue.use(() => { if (Vue.prototype.hasOwnProperty(key)) { return; } Object.defineProperty(Vue.prototype, key, { get () { return this.$root.$options[key]; } }); }); }; axios(app, inject); wait(app, inject); }
wait.js
export default function ({store: {dispatch, getters}}, inject) { const wait = { start: action => dispatch('wait/start', action), end: action => dispatch('wait/end', action), is: action => getters['wait/waiting'](action) }; inject('wait', wait); }
axios.js
import axios from 'axios'; export default function (app) { axios.defaults.baseURL = process.env.API_BASE_URL; axios.defaults.headers.common['Accept'] = 'application/json'; axios.defaults.headers.post['Content-Type'] = 'application/json'; }
main.js
: import Vue from 'vue'; import App from './App.vue'; import store from './store'; import initPlugins from './plugins'; const app = { render: h => h(App), store }; initPlugins(app); new Vue(app).$mount('#app');
main.js
file. And now it is immediately clear where to find the config for each plug-in and every global dependency.index.js
and call this function.Source: https://habr.com/ru/post/423013/
All Articles