Home >Web Front-end >JS Tutorial >How to register Vue's global component with use
This time I will show you how to register the global components of Vue with use, and what are the precautions 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.3.Introduce all global components to be registered in the components.js file4.Introduce components in the app.js root instance file .jsTake the eg component as an example:components.js:
import eg from '../components/eg.vue'; export default (Vue)=>{ Vue.component("Eg",eg); }
app.js:
import components from './plugins/components.js'; Vue.use(components);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. 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.js3. Introduce all global directives to be registered in the directives.js file4. 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(); } }) }
app.js
import directives from "./plugins/directives.js" Vue.use(directives);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!