<!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 id="app">{{message}}
<div v-bind:id="message"></div>
<todo-list>
<todo-item @remove-delete="handleDelete" v-for="item in list" :title="item.title" :del="item.del">
<!-- <span slot="pre-icon">前置图标</span>
<span slot="suf-icon">后置图标</span> -->
<!-- <template v-slot:pre-icon>
<span>前置图标</span>
</template> -->
<template v-slot:suf-icon ="{value}">
<span>后置图标{{value}}</span>
</template>
</todo-item>
</todo-list>
</div>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/vue.js"></script>
<script>
Vue.component('todo-item', {
// 属性声明
props: {
title: String, // 直接定义类型,没有默认值
del: {
type: Boolean, // 定义类型
default: false // 默认值
},
},
// 绑定对象
data: function() {
return {
val: Math.random()
}
},
// 方法
methods: {
handleClick() {
console.log("点击了删除按钮")
// 向外抛出事件,以及丢出参数
this.$emit('remove-delete', this.title)
}
},
// 模板
template: `
<li>
<slot name="pre-icon">这是默认值</slot>
<span v-if="!del">{{title}}</span>
<span v-else style="text-decoration: line-through;">{{title}}</span>
<slot name="suf-icon" :value="val"></slot>
<button v-show="!del" @click.stop=handleClick>删除</button>
</li>
`
})
Vue.component('todo-list', {
template: `
<ul>
<slot></slot>
</ul>
`,
data: function() {
return {
}
}
})
var vm = new Vue({
el: '#app',
data: {
message: 'hello world',
list: [{
title: '课程1',
del: false,
},{
title: '课程2',
del: true,
}]
},
methods: {
handleDelete(val) {
console.log("handleDelete点击了删除", val)
}
}
})
</script>
</body>
</html>