Home >Web Front-end >Vue.js >When to use vue.use

When to use vue.use

coldplay.xixi
coldplay.xixiOriginal
2020-11-12 11:42:572625browse

Time to use vue.use: When it is used, it actually calls the install method of the plug-in, so if the current plug-in introduced contains the install method, we need to use [Vue.use()].

When to use vue.use

【Recommended related articles: vue.js

What is Vue.use ?

Official description of the Vue.use() method: Use the global method Vue.use() to use plug-ins. Vue.use will automatically prevent multiple registrations of the same plug-in. It requires you to call new Vue. () Completed before starting the application, the Vue.use() method passes in at least one parameter. The parameter type must be Object or Function. If it is Object, then the Object needs to define an install method. If it is Function, then this function is regarded as install. method. When Vue.use() is executed, install will be executed by default. When install is executed, the first parameter is Vue, and the other parameters are other parameters passed in when Vue.use() is executed. That is to say, after using it, the install method of the component is called.

When should Vue.use() be used?

When used, it actually calls the install method of the plug-in, so if the current plug-in introduced contains the install method, we need to use Vue.use(). For example, refer to Element in Vue as follows:

import Vue from 'vue'
import Element from 'element-ui'
Vue.use(Element)

Because the install method is exposed in the Element source code, it needs to be introduced with Vue.use().

We can also define an install method in our own vue project, and then introduce it through the Vue.use() method to test it:

const plugin = {
  install() {
    alert("我是install内的代码")
  },
}
import Vue from "vue"
Vue.use(plugin) // 页面显示"我是install内的代码"

When we open the page, "I It’s the code in the install” prompt.

Related free learning recommendations: JavaScript (video)

The above is the detailed content of When to use vue.use. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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
Previous article:What is Muse-UI in vueNext article:What is Muse-UI in vue