solt:插槽,在template占位用来替换其他组件,作用让组件更具有拓展性。
- 基本使用:<slot></slot>
- 默认值<slot><p>我是默认插槽</p></slot>
- 如果用多个标签,会同时放到插槽中
- 多个插槽,会同时替换
我是分割线
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<!--作用让组件更具有拓展性
1.基本使用<slot></slot>
2.默认值<slot><p>我是默认插槽</p></slot>
3.如果用多个值,会同时放到插槽中
4.多个插槽,会同时替换
-->
<template id="son">
<div>
<div>我是子组件</div>
<p>hhhh</p>
<!--插入插槽-->
<slot></slot>
<!--插槽可以有默认-->
<slot><p>我是默认插槽</p></slot>
</div>
</template>
<div id="app">
<son></son>
<son>
<button>我是插槽内容</button>
</son>
</div>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script>
const son = {
template: '#son',
methods: {
sonClick(){
//通过$parent获取父组件
alert(this.$parent.message);
//通过$root获取根组件
alert(this.$root.movies);
}
},
};
var app = new Vue({
el: '#app',
data: {
message: 'Hello Vue!',
},
components: {
son
},
});
</script>
</body>
</html>
我是分割线
前台效果:
Title 我是子组件hhhh
我是默认插槽