This commit is contained in:
3056762376@qq.com 2022-11-07 15:17:03 +08:00
parent 1bd62a211b
commit dadd6ff863
4 changed files with 140 additions and 5 deletions

View File

@ -20,7 +20,9 @@
<template id="tpl">
<div>
<div>
<slot name="item"></slot>
<slot name="item">
<div></div>
</slot>
</div>
<div>
<slot>

View File

@ -9,16 +9,19 @@
<body>
<div id="app">
<apps>
<h1>这是父组件</h1>
<div slot-scope="data">
<span v-for="li in data">{{li.join("-")}}</span>
<div slot-scope="item">
<h1>这是父组件</h1>
<span v-for="li in item">
<span>{{li.join("__-_")}}</span>
</span>
</div>
</apps>
</div>
<template id="tpl">
<div>
<slot :datas="list">
<slot :item="list">
<ul>
<li v-for="item in list">{{item}}</li>
</ul>

View File

@ -0,0 +1,48 @@
<!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">
<apps>
<div slot-scope="data">
<ul v-for="item in data">
<div>{{item.join("__*__")}}</div>
</ul>
</div>
</apps>
<apps></apps>
</div>
<template id="tpl">
<div>
<slot :data="list">
<div v-for="item in list">
{{item}}
</div>
</slot>
</div>
</template>
</body>
<script src="../../../JavaScript/vue.js"></script>
<script>
new Vue({
el:"#app",
components:{
'apps':{
template:"#tpl",
data(){
return {
list: ['这是内容1', '这是内容2', '这是内容3']
}
}
}
}
})
</script>
</html>

View File

@ -0,0 +1,82 @@
<!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">
<!-- 具名插槽不能直接写在apps上 -->
<!-- 作用域插槽通过 在组件上的:变量=值的做法将子组件中的数据短暂存储到变量,
在父组件中使用slot-scope方法将子组件的内容存到这个方法里面再通过遍历得出来的
-->
<apps>
<!-- 具名插槽 -->
<div slot="form_table">
<form>
<label for="">用户名:<input type="text" name="" id=""></label>
<br>
<label for="">密码:<input type="password" name="" id=""></label>
<br>
<label for="">验证码:<input type="text" name="" id=""></label>
<br>
<button>注册</button>
</form>
</div>
</apps>
<br>
<!-- 作用域插槽 -->
<apps>
<div slot="items" slot-scope="datas">
<span v-for="li in datas">
<span>{{li.join("====")}}</span>
</span>
<span v-for="lis in datas">
<div>{{lis.join("\t")}}</div>
</span>
</div>
</apps>
</div>
<template id="tpl">
<div>
<div>
<slot name="form_table">
<form>
<label for="">用户名:<input type="text" name="" id=""></label>
<br>
<label for="">密码:<input type="password" name="" id=""></label>
<br>
<button>登录</button>
</form>
</slot>
</div>
<slot name="items" :datas="list">
<div>
<ul v-for="item in list">
<li>{{item}}</li>
</ul>
</div>
</slot>
</div>
</template>
</body>
<script src="../../JavaScript/vue.js"></script>
<script>
new Vue({
el:"#app",
components:{
'apps':{
template:"#tpl",
data(){
return {
list:['内容1','内容2','内容3']
}
}
}
}
})
</script>
</html>