一 匿名插槽

   // 语法 
  Vue.component('MBtn', {
template: `
<button>
<slot></slot>
</button> `,
});

使用

<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<title>Title</title>
<meta name="viewport" content="width=device-width ,initial-scale=1">
<script src="https://cdn.jsdelivr.net/npm/vue"></script>
</head>
<body> <div id="app">
<App></App>
</div> <script>
Vue.component('MBtn', {
template: `
<button>
<slot></slot>
</button> `,
}); const App = {
data() {
return {
msg: '数据'
}
},
我们可以使用匿名插槽的名字 也可以像下面那样 但是必须要有 —
template: `
<div>
<MBtn>登陆</MBtn>
<m-btn>注册</m-btn>
</div>`
}; let app = new Vue({
el: '#app', components: {
App
} })
</script> </body>
</html>

二 具名插槽

   // 语法 
  Vue.component('MBtn', {
template: `
<button>
<slot name="login"></slot>
<slot name="register"></slot>
<slot name="submit"></slot>
</button> `
});

  <MBtn>
<template slot="login">
<a href="#">登陆</a>
</template>
</MBtn>

具体使用

<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<title>Title</title>
<meta name="viewport" content="width=device-width ,initial-scale=1">
<script src="https://cdn.jsdelivr.net/npm/vue"></script>
</head>
<body> <div id="app">
<App></App>
</div> <script>
Vue.component('MBtn', {
template: `
<button>
<slot name="login"></slot>
<slot name="register"></slot>
<slot name="submit"></slot>
</button> `
}); const App = {
template: ` <div>
<MBtn>
<template slot="login">
<a href="#">登陆</a>
</template>
</MBtn> <MBtn>
<template slot="register">
<a href="#">注册</a>
</template>
</MBtn>
<MBtn>
<template slot="submit">
<a href="#">提交</a>
</template>
</MBtn>
</div> `
}; let app = new Vue({
el: '#app',
components: {
App
}
})
</script>
</body>
</html>

三 作用域插槽

有时候让插槽内容能够访问子组件中才有的数据是很有用的。(来自官网)

首先我们有一段代码

<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<title>Title</title>
<meta name="viewport" content="width=device-width ,initial-scale=1">
<script src="https://cdn.jsdelivr.net/npm/vue"></script>
</head>
<body> <div id="app">
<App></App>
</div> <script>
const todoList={
data(){
},
props:{
todos:Array,
defaultValue:[]
},
template:`
<ul>
<li v-for="item in todos" :key="item.id"> {{item.title}}
</li>
</ul>
`
}; const App={
data(){
return{
todoList:[
{
title:'大哥你好吗',
isComplate:true,
id:1
},
{
title:'小弟我还行啊',
isComplate:false,
id:2
},
{
title:'你在干什么',
isComplate:false,
id:3
},
{
title:'抽烟喝酒烫头',
isComplate:true,
id:4
}
] }
},
components:{
todoList
},
template:`
<todoList :todos="todoList"></todoList>
` }; let app = new Vue({
el:'#app',
components:{
App
}
})
</script>
</body>
</html>

效果如下:

好了  产品经理来 说 要给完成的打对勾

你肯定会说简单来看----加有一个 input就可以啦

    const todoList={
data(){
},
props:{
todos:Array,
defaultValue:[]
},
template:`
<ul>
<li v-for="item in todos" :key="item.id">
<input type="checkbox" v-model="item.isComplate">
{{item.title}}
</li>
</ul>
`
};

但是我们的数据要在好几个组件上用 不能写死咯

具体步骤:

1 需要我们在子组件中放一个插槽

2.父组件中使用

子组件:放插槽
template:`
<ul>
<li v-for="item in todos" :key="item.id"> <slot :itemValue="item"></slot>
{{item.title}}
</li>
</ul>
`
父组件:使用
template:`
<todoList :todos="todoList"> <template v-slot="data">
<input type="checkbox" v-model="data.itemValue.isComplate">
</template>
</todoList>
`

总代码

<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<title>Title</title>
<meta name="viewport" content="width=device-width ,initial-scale=1">
<script src="https://cdn.jsdelivr.net/npm/vue"></script>
</head>
<body> <div id="app">
<App></App>
</div> <script>
const todoList={
data(){
},
props:{
todos:Array,
defaultValue:[]
},
template:`
<ul>
<li v-for="item in todos" :key="item.id"> <slot :itemValue="item"></slot>
{{item.title}}
</li>
</ul>
`
}; const App={
data(){
return{
todoList:[
{
title:'大哥你好吗',
isComplate:true,
id:1
},
{
title:'小弟我还行啊',
isComplate:false,
id:2
},
{
title:'你在干什么',
isComplate:false,
id:3
},
{
title:'抽烟喝酒烫头',
isComplate:true,
id:4
}
] }
},
components:{
todoList
},
template:`
<todoList :todos="todoList">
<template v-slot="data">    # data自己定义
<input type="checkbox" v-model="data.itemValue.isComplate">
</template>
</todoList>
` }; let app = new Vue({
el:'#app',
components:{
App
}
})
</script>
</body>
</html>

最新文章

  1. mybatis.xml文件中#与$符号的区别以及数学符号的处理
  2. JDBC总结(含DbUtils组件)
  3. 2016 - 1 - 25 第三方网络框架 AFN的简单使用
  4. 如何在Chrome下Debug Mocha的测试
  5. HTML表单入门基础
  6. AIX碎碎念
  7. QT笔记
  8. 大数据之Ganglia
  9. BZOJ 1004
  10. eclipse加速之禁用JS、jsp等文件的语法验证,eclipsejs
  11. mysqli 操作数据库
  12. 物联网操作系统HelloX已成功移植到MinnowBoard MAX开发板上
  13. sigaction
  14. mysql外键设置选项
  15. Error: Your project contains C++ files but it is not using a supported native build system
  16. .Net IOC框架入门之一 Unity
  17. js操作文章、字符串换行
  18. 如何优雅的使用 Angular 表单验证
  19. 题解 P1601 【A+B Problem(高精)】
  20. Linux 目录结构和常用命令

热门文章

  1. (十四)登陆注册 逻辑二 前端globalData的使用 和 Storage
  2. 1_03_MSSQL课程_约束详解
  3. linux动态库(.so)和静态库(.a)的区别
  4. 「AT1983 BBQ Hard」
  5. (2)LoraWAN:Lora LMIC library 编程模型及API
  6. GoJS简单示例
  7. Day3-C-Radar Installation POJ1328
  8. 大数据萌新的Python学习之路(二)
  9. 072、Java面向对象之定义构造方法
  10. 【LOJ2513】「BJOI2018」治疗之雨