欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

vuejs中父子組件之間通信方法實(shí)例詳解

 更新時(shí)間:2020年01月17日 10:36:45   作者:林飛的夢囈  
這篇文章主要介紹了vuejs中父子組件之間通信方法,結(jié)合實(shí)例形式詳細(xì)分析了vue.js父組件向子組件傳遞消息以及子組件向父組件傳遞消息具體操作實(shí)現(xiàn)技巧,需要的朋友可以參考下

本文實(shí)例講述了vuejs中父子組件之間通信方法。分享給大家供大家參考,具體如下:

一、父組件向子組件傳遞消息

// Parent.vue

<template>
 <div class="parent">
  <v-child :msg="message"></v-child>
 </div>
</template>
<script>
 import VChild from './child.vue'
 export default {
  components: {
   VChild
  },
  data () {
   return {
    // 父組件將message作為參數(shù)傳入子組件中
    message: '來自父組件消息'
   }
  }
 }
</script>

// Child.vue
<template>
 <div class="child">
  <h1>child</h1>
  <p>{{ msg }}</p>
 </div>
</template>
<script>
 export default {
  // 通過props定義外部系統(tǒng)可以傳入的參數(shù)
  // 定義了一個(gè)msg變量,類型是String,默認(rèn)是空字符串
  props: {
   msg: {
    type: String,
    default: ""
   }
  }
 }
</script>

// router/index.js

import Vue from 'vue'
import Router from 'vue-router'
import HelloWorld from '@/components/HelloWorld'
import Parent from '@/test/Parent'
Vue.use(Router)
export default new Router({
 routes: [
  {
   path: '/',
   name: 'HelloWorld',
   component: HelloWorld
  },
  {
   path: '/parent',
   component: Parent
  }
 ]
})

二、子組件向父組件傳遞消息

// Parent.vue

<template>
 <div class="parent">
  <v-child :msg="message" @childNotify="childNotify"></v-child>
 </div>
</template>
<script>
 import VChild from './child.vue'
 export default {
  components: {
   VChild
  },
  data () {
   return {
    // 父組件將message作為參數(shù)傳入子組件中
    message: '來自父組件消息'
   }
  },
  methods: {
   childNotify (params) {
    console.log(params)
   }
  }
 }
</script>

// Child.vue

<template>
 <div class="child" @click="notifyParent">
  <h1>child</h1>
  <p>{{ msg }}</p>
 </div>
</template>
<script>
 export default {
  // 通過props定義外部系統(tǒng)可以傳入的參數(shù)
  // 定義了一個(gè)msg變量,類型是String,默認(rèn)是空字符串
  props: {
   msg: {
    type: String,
    default: ""
   }
  },
  methods: {
   notifyParent () {
    var params = {
     m: 1,
     n: 2
    }
    // 子組件以事件的形式通知父組件(需要使用$emit方法,第一個(gè)參數(shù),事件名稱;第二個(gè)事件附帶的參數(shù))
    this.$emit('childNotify', params)
   }
  }
 }
</script>

參考:https://jingyan.baidu.com/article/455a99505b639da1662778e1.html

希望本文所述對大家vue.js程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評論