利用混入将公共方法挂载到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('调用')
}


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM