<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>组件的双向绑定</title>
<script type="application/x-javascript" src="js/vue.js"></script>
</head>
<body>
<div id="app">
<alert-com :html='content'></alert-com>
<alert-com1 :html='content'>
<p>小心熊出没</p>
</alert-com1>
</div>
<script type="text/javascript">
Vue.component("alert-com", {
props:["html"],
template:`
<div class="alert">
<h3>温馨提示</h3>
<div class="content">
{{html}}
</div>
</div>
`,
})
Vue.component("alert-com1", {
props:["html"],
template:`
<div class="alert">
<h3>温馨提示</h3>
<div class="content">
<slot></slot>
</div>
</div>
`,
})
var app = new Vue({
el: "#app",
data: {
content: "消息提醒~"
}
})
</script>
</body>
</html>
总结
- 建议使用
alert-com1
组件的方式实现插槽功能,核心是子组件中使用slot