<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<!-- 父组件 -->
<div id="app">
<cpn>
<template v-slot:header>开始部分</template>
<template v-slot:default>中间部分</template>
<template v-slot:footer>结尾部分</template>
</cpn>
</div>
<!-- 子组件 -->
<template id="npv">
<div>
<header>
<!-- 我们希望把页头放这里 -->
<slot name="header"></slot>
</header>
<main>
<!-- 我们希望把主要内容放这里 -->
<slot></slot>
</main>
<footer>
<!-- 我们希望把页脚放这里 -->
<slot name="footer"></slot>
</footer>
</div>
</template>
<script src="js/vue.js"></script>
<script type="text/javascript">
const vm = new Vue({
el: '#app',
components: {
cpn: {
template: '#npv',
}
}
});
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<!-- 父组件 -->
<div id="app">
<cpn>
<!-- :后面的为slot的name ,=后面的为数据 -->
<template v-slot:childlist="list">
<span v-for="item in list.list">{{item}} * </span>
</template>
</cpn>
</div>
<!-- 子组件 -->
<template id="npv">
<div>
<slot>
<ul>
<li v-for="item in childList">{{item}}</li>
</ul>
</slot>
<!-- 修改中间的显示方式 -->
<slot name="childlist" v-bind:list="childList">
<ul>
<li v-for="item in childList">{{item}}</li>
</ul>
</slot>
<slot>
<ul>
<li v-for="item in childList">{{item}}</li>
</ul>
</slot>
</div>
</template>
<script src="js/vue.js"></script>
<script type="text/javascript">
const vm = new Vue({
el: '#app',
components: {
cpn: {
template: '#npv',
data() {
return {
childList: ["PHP", "Java", "Go", "c++", "JavaScript"],
}
}
}
}
});
</script>
</body>
</html>