How to register Vue's global component with use

php中世界最好的语言
Release: 2018-03-28 17:45:00
Original
1866 people have browsed it

This time I will show you how to register the global components of Vue with use, and what are theprecautions for registering Vue global components with use. The following is a practical case, let's take a look.

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, so use Vue.use() to "

Install" Global components appear 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.Introduce components in the app.js root instance file .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 writing, the global component Eg is registered.

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

Global directive

For the registration of global directives, the method given in the official documentation is to use Vue.

directive (), the location is also under the root instance file, so here comes the problem. If there are multiple global instructions, coupled with 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
This way The global directive is registered.

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:

How to use vue2.0axios cross-domain and rendering

How to remove all duplicate characters in JS

The above is the detailed content of How to register Vue's global component with use. 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