利用混入將公共方法掛載到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