您好,登錄后才能下訂單哦!
以下方法傳值存在兩個(gè)問(wèn)題:
1.不能去掉外面包裹的標(biāo)簽
2.如果要傳值的太多,這種方法很搓很難閱讀
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script src="./vue.js"></script>
<!-- <script src="http://cdn.staticfile.org/vue/2.6.10/vue.common.dev.js"></script> -->
</head>
<body>
<div id="root">
<child content="<p>My name is Tom Cat</p>"></child>
</div>
<script type="text/javascript">
Vue.component("child", {
props: ["content"],
template: `<div>
<p>hello</p>
<br/>//把html標(biāo)簽轉(zhuǎn)義顯示出來(lái)了:<br/><br/>
{{content}}<br/>
<br/>//正常渲染了html標(biāo)簽(也顯示了外面包裹的div):
<div v-html='this.content'></div>
//而模版占位符template也不能去掉外面包裹的標(biāo)簽,而且整個(gè)都不渲染了:<br/>
<template v-html='this.content'></template>
<br/>
</div>`
});
var vm = new Vue({
el: "#root"
})
</script>
</body>
</html>
那用啥方法?用插槽??!
插槽的使用細(xì)節(jié):
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script src="./vue.js"></script>
<!-- <script src="http://cdn.staticfile.org/vue/2.6.10/vue.common.dev.js"></script> -->
</head>
<body>
<div id="root">
<child>
//這是插槽咯:
<h2>world</h2>
</child>
//注意:content是保留關(guān)鍵字,不能用作組件,so 用my-content(用myContent這樣的駝峰命名會(huì)報(bào)錯(cuò)):
<my-content1>
<div class="header">header</div>
<div class="footer">footer</div>
</my-content1>
<br>//具名插槽:
<my-content2>
<div class="header" slot="header">header</div>
<div class="footer" slot="footer">footer</div>
</my-content2>
</div>
<script type="text/javascript">
Vue.component("child", {
//props: ["content"],
template: `<div>
<p>hello</p>
<slot>默認(rèn)內(nèi)容,當(dāng)父組件不傳遞插槽內(nèi)容的時(shí)候顯示</slot>
</div>`
});
Vue.component("my-content1", {
//props: ["content"],
template: `<div>
<slot></slot>
<div class='content'>hello</div>
<slot></slot>
</div>`
});
Vue.component("my-content2", {
//props: ["content"],
template: `<div>
<slot name="header">不給該插槽傳值,則顯示我</slot>
<div class='content'>hello</div>
<slot name="footer"></slot>
</div>`
});
var vm = new Vue({
el: "#root"
})
</script>
</body>
</html>
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。