利用混入将公共方法挂载到VUE实例原型上

Spublic.js
let Vue
 
const testFun = (e) =>{
    console.log(e,"OK")
}

function install(_Vue) {
     Vue = _Vue
     Vue.mixin({
         beforeCreate(){
             if(this.$options.Song){
                 Vue.prototype.$testFun=testFun
             }
         }
     })

}

export default {install}
复制代码
main.js
import Vue from 'vue'
import Song from './utils/sPublic';
Vue.use(Song)

new Vue({
  Song,
  render: h => h(App),
}).$mount('#app')
复制代码
.vue
beforeCreate(){
  this.$testFun('调用')
}
复制代码
© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享