vue222/10-组件化开发/09-组件插槽使用/练习插槽的使用.html

82 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 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>