<!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>結(jié)尾部分</template>
</cpn>
</div>
<!-- 子組件 -->
<template id="npv">
<div>
<header>
<!-- 我們希望把頁頭放這里 -->
<slot name="header"></slot>
</header>
<main>
<!-- 我們希望把主要內(nèi)容放這里 -->
<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>
- 作用域插槽 把子組件中的數(shù)據(jù)拿到父組件中使用
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<!-- 父組件 -->
<div id="app">
<cpn>
<!-- :后面的為slot的name 纱兑,=后面的為數(shù)據(jù) -->
<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>
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者