Vue add a global method

A method frequently used, I do not want to introduce every time, with the introduction of a Plugin, and more calls, Bang Bang da

1. Create Method
  • srcUnder the new pluginfolder, used to store plug-ins
  • For example, I want to call the method named $toTop, the new toTop.js, the preparation method (global best way to start with $)
export default {
  install (Vue, options) {
    Vue.prototype.$toTop = function () {
      console.log('Plugin Test')
    }
  }
}

2. Add a Vueglobal approach

  • In main.jsthe introduction, add
// 插件
import ToTop from './plugin/toTop' // 引入
Vue.use(ToTop) // 添加

3. Use

  • Then you can freely use it in the assembly
 mounted () {
    this.$toTop()
}
So easy ~

Guess you like

Origin blog.csdn.net/weixin_33720956/article/details/90830303