mixins

mixins
概念:
mixins: 混合 ,将 根实例或是组件中的配置项 抽离出来, 单独管理
类型
A:局部混入

var mixin = {
	methods: {
		sum(){
			alert( 10*10 )
		}
	},
}
new Vue({
	el: '#app',
	data: {},
	watch: {},
	mixins: [mixin],
	computed: {}
})

注意:

  1. 即使分离出去, 我们的配置项中也可以继续写分离出去的配置
  2. 如果说分离出去中的内容有冲突, 以组件中的配置项为准

B: 全局混入
注意: 全局混入慎用(不建议你使用)
理由:全局混入会影响所有的组件(实例)

Vue.mixin({
	watch: {},
	methods: {}
})

例如:

Html:
   <div id="app">
   <button @click = "sum"> 点击 </button>
   <button @click = "redecer"> 点击 </button>
   <button @click = "changeName"> 点击 </button>
</div>

Js:
   Vue.mixin({
   methods: {
   changeName(){
   alert('zhangsan')
   }
   }
   })
   new Vue({
   	el: '#app',
   	data: {},
   	watch: {},
   	methods: {
   		redecer(){
   			alert( 100-10 )
   		},
   		sum(){
   			alert('张三')
   		}
   	},
   	computed: {}
   })

猜你喜欢

转载自blog.csdn.net/clover____/article/details/89408850