How to use use to register Vue global components and global directives

亚连
Release: 2018-05-31 16:28:47
Original
1927 people have browsed it

Below I will share with you a method of using use to register Vue global components and global instructions. It has a good reference value and I hope it will be helpful to everyone.

The components and instructions in Vue are divided into local components, local instructions and global components and global instructions. When registering a certain number of global instructions and global components, the method in the official document seems a bit unclear.

Global component

Introduced in the Vue official documentation is to use Vue.component(tagName, options) to create a global component. However, this method is written in the same file as the root instance. If you want to register multiple global components at the same time, it will make the root instance file too heavy. Therefore, it is more convenient to use Vue.use() to "install" global components. Be lighter.

Method:

1. Create a new plugins folder

2. Create a global component in the folder File components.js

3. Introduce all global components to be registered in the components.js file

4. In the app.js root instance file, introduce components.js

Take the eg component as an example:

components.js:

import eg from '../components/eg.vue'; export default (Vue)=>{ Vue.component("Eg",eg); }
Copy after login

app.js:

import components from './plugins/components.js'; Vue.use(components);
Copy after login

After the above After writing, the global component Eg is registered.

When multiple global components need to be registered, it is more refreshing to use this method!

Global directives

For the registration of global directives, the method given in the official documentation is to use Vue.directive(), and the location is also in Under the root instance file, the problem arises. If there are multiple global instructions and multiple global components, the app.js file will become extremely bloated.

Therefore, similar to the above method of registering global components, Vue.use() is also used to "install" global instructions.

Method:

1. Create a new plugins folder

2. Create a global component in the folder File directives.js

3. Introduce all global directives to be registered in the directives.js file

4. In the app.js root instance file, introduce directives.js

Take the v-focus directive as an example:

directives.js:

export default (Vue)=>{ Vue.directive("focus",{ inserted:function(el){ el.focus(); } }) }
Copy after login

app.js

import directives from "./plugins/directives.js" Vue.use(directives);
Copy after login

Above I compiled it for everyone. I hope it will be helpful to everyone in the future.

Related articles:

jQuery ajax reads json data and sorts it by price example

vue data control view source code analysis

vue Sample code for developing a button component

The above is the detailed content of How to use use to register Vue global components and global directives. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn