Vue入門(十一)---- mixins
阿新 • • 發佈:2019-01-01
混入 (mixins)
是一種分發 Vue 元件中可複用功能的非常靈活的方式。混入物件可以包含任意元件選項。當元件使用混入物件時,所有混入物件的選項將被混入該元件本身的選項。
main.js
var base = {
methods: {
show: function() {
this.visible = true
},
hide: function() {
this.visible = false
},
hide: function() {
this .visible = false;
},
toggle: function(){
this.visible = !this.visible
},
},
data() {
return {
visible: false,
}
},
}
Vue.component('tooltip', {
template: `
<div>
<span @mouseenter="show" @mouseleave="hide" >bys</span>
<div v-if="visible">
白巖鬆
</div>
</div>
`,
mixins: [base]
})
Vue.component('popup', {
template: `
<div>
<button @click="toggle">Pupup</button>
<div v-if="visible">
<span @click="hide" >X</span>
<h4>title</h4>
Lorem, ipsum dolor sit amet consectetur adipisicing elit. In exercitationem beatae facilis? Cumque nesciunt dolorem odit ipsum obcaecati. Possimus saepe dicta, neque harum mollitia provident nisi voluptatibus corrupti minima quasi.
</div>
</div>
`,
mixins: [base]
})
new Vue({
el: '#app',
data: {
card1: {
pinned: false,
},
card2: {
pinned: false,
}
},
})
index.js
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
[v-cloak] {
display: none;
}
</style>
</head>
<body>
<div id="app" v-cloak>
<tooltip></tooltip>
<popup></popup>
</div>
<script src="./lib/vue.js"></script>
<script src="main.js"></script>
</body>
</html>