组件的拆分与嵌套
组件的拆分与嵌套,是将一个.vue文件内的内容按照模块分为多个,在使用过程中,通过components属性来局部注册组件:
<template>
<div class="main">
<main-banner></main-banner>
<main-product></main-product>
</div>
</template>
<script>
import MainBanner from "./MainBanner.vue";
import MainProduct from "./MainProduct.vue";
export default {
components:{
MainBanner,
MainProduct
}
}
</script>
<style scoped>
</style>组件的css作用域问题
style标签下的scoped属性表明当前的css样式只对本文件生效:
<style scoped>
h2{
color: red;
}
</style>但是VUE3会给div标签加上data-v的属性,这个属性会传给使用的局部组件的根节点:
因此如果每一个组件的template不加上一个div作为根节点的话,父组件的css也会作用到子组件上。
父组件传递给子组件
父组件传递给子组件:通过props属性
在子组件中定义props属性,props中可以定义字符串数组,也可以定义对象:
<template>
<div>
<h2>{{title}}</h2>
<p>{{content}}</p>
</div>
</template>
<script>
export default {
props: [
"title",
"content"
]
}
</script>在父组件中向子组件的属性传入值:
<template>
<div>
<show-message title="标题1" content="内容1"></show-message>
<show-message title="标题2" content="内容2"></show-message>
<show-message title="标题3" content="内容3"></show-message>
<show-message :title="title" :content="content"></show-message>
<show-message :title="message.title" :content="message.content"></show-message>
<show-message v-bind="message"></show-message>
</div>
</template>
<script>
import ShowMessage from "./ShowMessage.vue"
export default {
components:{
ShowMessage
},
data(){
return {
title: "title",
content:"content",
message:{
title:"message title",
content:"message content"
}
}
}
}
</script>props也可以传入一个对象(这也是常见的作法),可以定义是否是必须的与默认值:
props:{
title:String,
content:{
type:String,
required:true,
default:123
},
info:{
type: Object,
// 引用类型,需要写成函数
default(){
return {name:"sk"}
}
}
}html在解析props时会忽略大小写,因此props最好统一使用小写。
非Prop的Attribute的传递:
非Prop的Attribute,即对于html组件都有的id、name、style等属性,它们在自定义的组件中并没有在props定义,对于这种属性,有三种处理方式:
Attribute根组件继承:当组件有单个根节点时,非Prop的Attribute将自动添加到根节点的Attribute中
<show-message class="123" title="标题1" content="内容1"></show-message>
如果我们不希望组件的根元素继承attribute,可以在组件中设置inheritAttrs: false,可以通过$attrs来访问所有的非props的attribute
<script>
export default {
inheritAttrs:false
</script><template>
<div>
<h2 :class="$attrs.class">{{title}}</h2>
<p>{{content}}</p>
</div>
</template>或直接使用对象:
<template>
<div>
<h2 v-bind="$attrs">{{title}}</h2>
<p>{{content}}</p>
</div>
</template>如果有多个根节点的attribute,则需要显式地指明需要绑定在哪一个组件上,否则会报错:
<template>
<h2 :id="$attrs.id">MultiRootElement</h2>
<h2>MultiRootElement</h2>
<h2>MultiRootElement</h2>
<h2>MultiRootElement</h2>
</template>
子组件传递给父组件
子组件传递给父组件:通过$emit触发事件;
首先需要先在子组件中定义要出发的事件:
<script>
export default {
// 数组写法
// emits:["add","sub","addN"],
// 对象写法的目的是为了进行参数的验证
emits:{
add: null,
sub: null,
addN: (num,name,age) =>{
console.log(num,name,age);
if (num>10){
return true;
}
return false;
}
}
</script>在子组件中为按钮设置监听,在方法中触发事件传递给父组件:
<template>
<div>
<button @click="increment">+1</button>
<button @click="decrement">-1</button>
</div>
</template>
<script>
export default {
methods: {
increment(){
console.log("+1");
this.$emit("add");
},
decrement(){
console.log("-1");
this.$emit("sub");
}
},
}
</script>在父组件中监听子组件的事件,并作出处理:
<template>
<div>
<h2>当前计数{{counter}}</h2>
<counter-opetation
@add="addOne"
@sub="subOne"
@addN="addNNum">
</counter-opetation>
</div>
</template>
<script>
import CounterOpetation from "./CounterOperation.vue";
export default {
components:{
CounterOpetation
},
data(){
return {
counter:0
}
},
methods: {
addOne(){
this.counter++;
},
subOne(){
this.counter--;
}
},
}
</script>在自定义的事件中,还能够传递参数:
// 子组件
<template>
<div>
<input type="text" v-model.number="num">
<button @click="incrementN">+n</button>
</div>
</template>
<script>
export default {
// 数组写法
// emits:["add","sub","addN"],
// 对象写法的目的是为了进行参数的验证
emits:{
add: null,
sub: null,
addN: (num,name,age) =>{
console.log(num,name,age);
if (num>10){
return true;
}
return false;
}
},
data(){
return{
num:0
}
},
methods: {
incrementN(){
console.log("add",this.num);
this.$emit("addN",this.num,"sk",18);
}
},
}
</script>// 父组件
<template>
<div>
<h2>当前计数{{counter}}</h2>
<counter-opetation
@add="addOne"
@sub="subOne"
@addN="addNNum">
</counter-opetation>
</div>
</template>
<script>
import CounterOpetation from "./CounterOperation.vue";
export default {
components:{
CounterOpetation
},
data(){
return {
counter:0
}
},
methods: {
addNNum(num,name,age){
this.counter+=num;
}
},
}
</script>一个案例
// App.vue
<template>
<div>
<tab-control :titles="titles"
@titleClick="titleClick"></tab-control>
<h2>{{contents[currentIndex]}}</h2>
</div>
</template>
<script>
import TabControl from "./TabControl.vue";
export default {
components:{
TabControl
},
data(){
return {
titles: ["衣服","鞋子","裤子"],
contents: ["衣服页面","鞋子页面","裤子页面"],
currentIndex:0
}
},
methods: {
titleClick(index){
this.currentIndex=index;
}
},
}
</script>
<style scoped>
</style>// TabControl.vue
<template>
<div class="tab-control">
<div class="tab-control-item"
:class="{active: currentIndex === index}"
v-for="(title, index) in titles"
:key="title"
@click="itemClick(index)">
<span>{{title}}</span>
</div>
</div>
</template>
<script>
export default {
emits:["titleClick"],
props:{
titles:{
type: Array,
default(){
return [];
}
}
},
data(){
return {
currentIndex:0
}
},
methods: {
itemClick(index){
this.currentIndex=index;
this.$emit("titleClick",index);
}
}
}
</script>
<style scoped>
.tab-control{
display: flex;
}
.tab-control-item{
flex:1;
text-align: center;
}
.tab-control-item.active{
color: red;
}
.tab-control-item.active span{
border-bottom: 5px solid red;
padding: 5px 10px;
}
</style>效果: