plugin插件
一.School.vue
< template>
< div>
< h2> 学校名称:{ { name | mySlice} } < / h2>
< h2> 学校地址:{ { address} } < / h2>
< button @click= "test" > 点我测试一个hello方法< / button>
< / div>
< / template>
< script>
export default {
name : 'School' ,
data ( ) {
return {
name : '尚硅谷atguigu' ,
address : '北京' ,
}
} ,
methods : {
test ( ) {
this . hello ( )
}
} ,
}
< / script>
二.Student.vue
< template>
< div>
< h2> 学生姓名:{ { name} } < / h2>
< h2> 学生性别:{ { sex} } < / h2>
< input type= "text" v- fbind: value= "name" >
< / div>
< / template>
< script>
export default {
name : 'Student' ,
data ( ) {
return {
name : '张三' ,
sex : '男'
}
} ,
}
< / script>
三.App.vue
< template>
< div>
< School/ >
< hr>
< Student/ >
< / div>
< / template>
< script>
import School from './components/School'
import Student from './components/Student'
export default {
name : 'App' ,
components : { School, Student}
}
< / script>
四.main.js
import Vue from 'vue'
import App from './App.vue'
import plugins from './plugins'
Vue. config. productionTip = false
Vue. use ( plugins, 1 , 2 , 3 )
new Vue ( {
el : '#app' ,
render : h => h ( App)
} )
五.plugins.js
export default {
install ( Vue, x, y, z ) {
console. log ( x, y, z)
Vue. filter ( 'mySlice' , function ( value ) {
return value. slice ( 0 , 4 )
} )
Vue. directive ( 'fbind' , {
bind ( element, binding ) {
element. value = binding. value
} ,
inserted ( element, binding ) {
element. focus ( )
} ,
update ( element, binding ) {
element. value = binding. value
}
} )
Vue. mixin ( {
data ( ) {
return {
x : 100 ,
y : 200
}
} ,
} )
Vue . prototype. hello = ( ) => { alert ( '你好啊' ) }
}
}