Use to register Vue global components and global commands, usevue

Source: Internet
Author: User

Use to register Vue global components and global commands, usevue

Vue components and Commands include local components, local commands, global components, and global commands. When you register a certain number of global commands and global components, the methods in the official documentation are somewhat refreshing.

Global Components

Vue. component (tagName, options) is used 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, the root instance file will be too heavy. Therefore, Vue is used. use () to "Install" global components, it looks lighter.

Method:

1. Create a New plugins folder

2. Create the components. js file in the folder where the global components are placed.

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);}

App. js:

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

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

When you need to register multiple global components, it is even more refreshing to use this method!

Global commands

For the registration of global commands, the official documentation provides a way to use Vue. directive () is also located in the root instance file. The problem arises. If multiple global commands and multiple global components are added, then the app. js files will become bloated.

Therefore, the method for registering global components is similar to that for registering global components. Vue. use () is also used to "Install" Global commands.

Method:

1. Create a New plugins folder

2. Create the directives. js file in the folder to place global components.

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

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

Take the v-focus command 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);

In this way, the global command is registered.

The above method of using use to register Vue global components and global commands is all the content shared by Alibaba Cloud. I hope you can give us a reference and support for the customer's home.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.