Vue-mixins

by hysunny

HTML

<div id="app">
</div>

CSS

body {
  background: #20262E;
  padding: 20px;
  font-family: Helvetica;
}

#app {
  background: #fff;
  border-radius: 4px;
  padding: 20px;
  transition: all 0.2s;
}

li {
  margin: 8px 0;
}

h2 {
  font-weight: bold;
  margin-bottom: 15px;
}

del {
  color: rgba(0, 0, 0, 0.3);
}

Vue

// 打开控制台
const mixin = {
	template: `
    	<div @click="handleClick">请打开控制台</div>
    `,
     methods: {
    	handleClick() {
    		console.log('我是混入对象的点击事件');
    	},
    },
    mounted() {
    	console.log('我是混入对象的mounted');
    }
}

new Vue({
  el: "#app",
  mixins: [mixin],
  methods: {
    	handleClick() {
    		console.log('我是组件的点击事件');
    	},
    },
  mounted() {
  	console.log('我是组件的mounted');
  }
})