首先我们想先创建一个子组件
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Vue中的插槽(slot)</title>
<script src='./vue.js'></script>
</head>
<body>
<div id="root">
<child></child>
</div>
<script>
Vue.component('child', {
template: '<div><p>hello</p></div>'
})
var vm = new Vue({
el: '#root'
})
</script>
</body>
</html>
然后 我如何转意p标签呢
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Vue中的插槽(slot)</title>
<script src='./vue.js'></script>
</head>
<body>
<div id="root">
<!-- 向子组件进行传值 -->
<child content='<p>Dell</p>'></child>
</div>
<script>
Vue.component('child', {
props: ['content'],
// 接受过content后我们可以插入插值表达式里了
template: `<div>
<p>hello</p>
<div v-html="this.content"></div>
</div>`
})
var vm = new Vue({
el: '#root'
})
</script>
</body>
</html>
运行效果图
我们发现转义成功
但是我们有更好的解决方案
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Vue中的插槽(slot)</title>
<script src='./vue.js'></script>
</head>
<body>
<div id="root">
<child>
<p>Dell</p>
</child>
</div>
<script>
Vue.component('child', {
// 这里显示的就是父组件向子组件插入的标签
template: `<div>
<slot></slot>
</div>`
})
var vm = new Vue({
el: '#root'
})
</script>
</body>
</html>
通过插槽 我们可以更好的调用dom元素
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Vue中的插槽(slot)</title>
<script src='./vue.js'></script>
</head>
<body>
<div id="root">
<h1>
<p>Dell</p>
</h1>
</div>
<script>
Vue.component('child', {
// 这里显示的就是父组件向子组件插入的标签
template: `<div>
<p>Hello</p>
<slot></slot>
</div>`
})
var vm = new Vue({
el: '#root'
})
</script>
</body>
</html>
这里还有个有趣的现象 下面的默认内容不显示
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Vue中的插槽(slot)</title>
<script src='./vue.js'></script>
</head>
<body>
<div id="root">
<h1>
<p>Dell</p>
</h1>
</div>
<script>
Vue.component('child', {
// 这里显示的就是父组件向子组件插入的标签
template: `<div>
<p>Hello</p>
<slot>默认内容</slot>
</div>`
})
var vm = new Vue({
el: '#root'
})
</script>
</body>
</html>
但是如果将插槽的内容取消
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Vue中的插槽(slot)</title>
<script src='./vue.js'></script>
</head>
<body>
<div id="root">
<child></child>
</div>
<script>
Vue.component('child', {
// 这里显示的就是父组件向子组件插入的标签
template: `<div>
<p>Hello</p>
<slot>默认内容</slot>
</div>`
})
var vm = new Vue({
el: '#root'
})
</script>
</body>
</html>
此时我们看另外一个例子
首先不使用插槽:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Vue中的插槽(slot)</title>
<script src='./vue.js'></script>
</head>
<body>
<div id="root">
<content>
</content>
</div>
<script>
Vue.component('child', {
template: `<div>
<div class= 'content'>content</div>
</div>`
})
var vm = new Vue({
el: '#root'
})
</script>
</body>
</html>
运行效果
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Vue中的插槽(slot)</title>
<script src='./vue.js'></script>
</head>
<body>
<div id="root">
<content>
</content>
</div>
<script>
Vue.component('content', {
template: `<div>
<div class= 'content'>content</div>
</div>`
})
var vm = new Vue({
el: '#root'
})
</script>
</body>
</html>
因为content是关键字 所以我们改一个名字就可以了
具体代码如下
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Vue中的插槽(slot)</title>
<script src='./vue.js'></script>
</head>
<body>
<div id="root">
<body-content>
</body-content>
</div>
<script>
Vue.component('body-content', {
template: `<div>
<div class= 'content'>content</div>
</div>`
})
var vm = new Vue({
el: '#root'
})
</script>
</body>
</html>
接下来 我们再写一个例子 看看效果
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Vue中的插槽(slot)</title>
<script src='./vue.js'></script>
</head>
<body>
<div id="root">
<body-content>
<div class='header'>header</div>
<div class='footer'>footer</div>
</body-content>
</div>
<script>
Vue.component('body-content', {
template: `<div>
<slot></slot>
<div class= 'content'>content</div>
<slot></slot>
</div>`
})
var vm = new Vue({
el: '#root'
})
</script>
</body>
</html>
之所以显示成这样是因为
这两个插槽 都起作用了 但是有没有好的解决方案
我们可以给插槽起名字
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Vue中的插槽(slot)</title>
<script src='./vue.js'></script>
</head>
<body>
<div id="root">
<body-content>
<div class='header' slot='header'>header</div>
<div class='footer' slot='footer'>footer</div>
</body-content>
</div>
<script>
Vue.component('body-content', {
template: `<div>
<slot name='header'></slot>
<div class= 'content'>content</div>
<slot name='footer'></slot>
</div>`
})
var vm = new Vue({
el: '#root'
})
</script>
</body>
</html>
<div id="root">
<body-content>
<div class='footer' slot='footer'>footer</div>
</body-content>
</div>
<script>
Vue.component('body-content', {
template: `<div>
<slot name='header'>default header</slot>
<div class= 'content'>content</div>
<slot name='footer'></slot>
</div>`
})
var vm = new Vue({
el: '#root'
})
</script>
<div id="root">
<body-content>
<div class='footer' slot='footer'>footer</div>
</body-content>
</div>
<script>
Vue.component('body-content', {
template: `<div>
<slot name='header'>
<h1>default header</h1>
</slot>
<div class= 'content'>content</div>
<slot name='footer'></slot>
</div>`
})
var vm = new Vue({
el: '#root'
})
</script>