Vue.use mounts global components_plugins_custom functions_custom variables

effect

It is a static method provided by Vue to register plugins with Vue (enhancing the functionality of Vue). Documentation:   https://cn.vuejs.org/v2/api/#Vue-use

illustrate:

  1. Vue.use can receive an object, Vue.use(obj)
  2. An install function needs to be provided in the object obj
  3. When Vue.use(obj), the install function will be called automatically and passed in to the Vue constructor

1. Regular global registration

main.js

import PageTools from './components/PageTools'
const MyPlugin = {
  install(a) {
    console.log('install.....', a === Vue) // install..... true
    a.component('PageTools', PageTools)
  }
}
Vue.use(MyPlugin)

2. Optimize global registration

main.js

@/components/index.js

Not only can you mount components, but you can also mount plug-ins. You can also mount global functions and global variables. These two are mounted on the prototype of Vue. Call this.xxx and this can be omitted in the line.

In fact, both global functions and variables can be mounted in main.js, but then main.js will be too messy, so it is best for us to customize a file, import and register this object with Vue.use in main.js, and more concise

Guess you like

Origin blog.csdn.net/qq_59650449/article/details/128516105