Vue_划分组件以及组件之间的通信

在Vue中,每个组件都是一个实例,组件可以看成是一个页面的区域。
1.组件划分:
作为一个功能模块系统,划分出一个个的组件,有两种划分模式:功能模块和页面区域:

//如何划分组件
功能模块:select,pagenation...
页面模块:header,footer,sidebar...

这里写图片描述

App.vue是项目的入口,我们分好的组件也是一个Vue文件,在APP.vue我们把各个组件导入,然后在components(是数组[])中注册,在模板层面上就可以使用了<header><footer>来调用组件的内容 了,注意的是Vue组件 需要注册才能使用

2.组件之间的通信:
这里写图片描述
组件之间的通信包括父向子传参和子向父传参。
具体参照下一笔记。
3.在当前页面引入组件
eg:
我们在components写一个Vue组件,命名为ComponentsA.vue,想将它引用到App.vue中,ComponentsA.vue如下:

<template>
    <div class="componentsA">
     <h1>{{ msgfromfather }}</h1>
  <button v-on:click='onClickme'>Click!</button>
    </div>
</template>
<script>
export default{
  data(){
     return{
     }
  },
  props:['msgfromfather'],
  methods:{
    onClickme:function(){
       this.$emit('child-tell-me-something',this.msg)
    }
  }
}
</script>
<style scoped>
</style>

如果写组件要在当前页面App.vue使用的话,首先要引入import进来该组件,然后在export default注册一下:

 import componentsA from './components/componentsA'
  export default {
   components:{componentsA}
}

然后在用标签<组件名>,就可以在页面中中显示子组件的内容了。

<componentsA></componentsA>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值