89 lines
2.1 KiB
HTML
89 lines
2.1 KiB
HTML
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta http-equiv="X-UA-Compatible" content="IE=edge">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<title>Document</title>
|
|
</head>
|
|
<body>
|
|
<div class="app">
|
|
<item1 @change="getChange">
|
|
<template v-slot:back>
|
|
<i class="iconfont test"></i>
|
|
<span>返回</span>
|
|
</template>
|
|
|
|
<template v-slot:icon>
|
|
<i class="iconfont test"></i>
|
|
<i class="iconfont test"></i>
|
|
</template>
|
|
</item1>
|
|
<item2 :demo="text"></item2>
|
|
</div>
|
|
</body>
|
|
<script src="https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.js"></script>
|
|
<script>
|
|
// 作用域插槽
|
|
Vue.component('item1', {
|
|
data() {
|
|
return {
|
|
title: 'item1',
|
|
test: 'item1 test data'
|
|
}
|
|
},
|
|
template: `
|
|
<div class="item1" @click="clickData">
|
|
<div class="back">
|
|
<slot name="back"></slot>
|
|
</div>
|
|
<div class="icon">
|
|
<slot name="icon"></slot>
|
|
</div>
|
|
</div>
|
|
`,
|
|
methods: {
|
|
clickData() {
|
|
this.$emit("change", this.test)
|
|
}
|
|
}
|
|
});
|
|
|
|
Vue.component('item2', {
|
|
props: {
|
|
demo: {
|
|
type: String,
|
|
default: ''
|
|
}
|
|
},
|
|
data() {
|
|
return {
|
|
title: 'item2'
|
|
}
|
|
},
|
|
created() {
|
|
console.log(this.$parent.$children[0].$data.test);
|
|
},
|
|
watch: {
|
|
demo(n,o) {
|
|
console.log(n,o);
|
|
}
|
|
},
|
|
template: `
|
|
<div class="item2">item2</div>
|
|
`
|
|
});
|
|
|
|
new Vue({
|
|
el: '.app',
|
|
data:{
|
|
text: ''
|
|
},
|
|
methods:{
|
|
getChange(data) {
|
|
this.text = data
|
|
}
|
|
}
|
|
})
|
|
</script>
|
|
</html> |