vue学习四,组件(慕课网学习笔记)

1.组件划分

功能模块:select,pagenation...

页面区域:header,footer,sidebar...

2.组件注册

header.vue和footer.vue文件,在App.vue中引用注册

import Header from './header'
import Footer from './footer'

new Vue({  
  components: { Header,Footer }
})

在内容中使用组件<header></header>

3.组件通信

//this is header.vue(子组件)
<template>
  <div class="hello">
    <h1>{{ msg }}</h1>
    <h1>{{ msgfromfather }}</h1>
    <button v-on:click="onClickMe">click!</button>
  </div>
</template>
<script>
export default {
  data: function() {
    return {
      msg: 'in component',
    }
  },
  props:['msgfromfather'],//定义一个属性
  methods:{
    onClickMe:function(){
      console.log(this.msgfromfather);//获取父组件传的值
      this.$emit('child-tell-me-something',this.msg);//调用定义的方法,向父组件传值(方法一)
      this.$dispatch('child-tell-me-somthing',this.msg);//由里向外传值(方法二)
    }
  }
}
</script>
<!--this is app.vue(父组件)-->
<!--msg属性给组件传值(父组件向子组件传值),v-on监听定义的方法获取子组件传的值(子组件向父组件传值-方法一)-->
<p>child tell me:{{childWords}}</p>
<header msgfromfather="you die!" v-on:child-tell-me-somthing='listenToMyBoy'></header>
<script>
import Com from './components/com'//引用
export default {
  data () {
    return {
       title: 'Todos -Vuejs',
       childWords:''//预先定义值为空
    }
  },
  components:{Com},//注册组件
  method:{
    //定义监听子组件传值(方法一)     
    listenToMyBoy:function(msg){
        this.childWords=msg;
     }
  },
//注册监听传值方法(方法二)
  events:{
     'child-tell-me-somthing':function(){
        this.childWords=msg;
     }
  },
  }
</script>

this.$emit():在它上面触发

this.$dispatch(): 由里向外传递,子向父

this.$boradcast(): 由外向里传递,广播,父向子


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值