Vue mixin(混入) && 外掛
阿新 • • 發佈:2022-05-31
1 # mixin(混入) 2 # 功能:可以把多個元件公用的配置提取成一個混入物件 3 # 使用方法: 4 # 第一步:{data(){return {...}}, methods:{...},...} 5 # 第二步:1.全域性混入:Vue.mixin(xxx)、2.區域性混入:mixins:[xxx] 6 7 # Vue 外掛 8 # 第一步:定義外掛 plugins.js 9 export default { 10 install(Vue, a, b, c){ // a, b, c為Vue.use()時傳入的引數11 console.log(a, b, c) 12 // 定義全域性過濾器 13 Vue.filter('mySlice', function(value){ 14 return value.slice(0,4) 15 }); 16 17 // 定義全域性指令 18 Vue.directive('big-number',{ 19 bind(element, binding){ 20 element.value = binding.value;21 }, 22 inserted(element, binding){ 23 element.setFouse(); 24 }, 25 update(element, binding){ 26 element.value = binding.value; 27 } 28 }) 29 30 // 定義混入 31 Vue.mixin({...}); // 全域性混入 32 Vue.mixin({...});33 34 // 給原型新增方法 35 Vue.prototype.hello = () =>{alert('hello a!')} 36 } 37 } 38 # 第二步:引入外掛 39 Vue.use(plugins, 1, 2, 3) 40 41 # scoped樣式 42 作用:讓樣式在區域性生效,防止樣式名衝突 43 寫法:<style scoped>