利用混入将公共方法挂载到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('调用')
}
posted @ 2021-05-27 14:03  -1-%30  阅读(672)  评论(0)    收藏  举报