国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > JavaScript > 正文

vue組件間通信解析

2024-05-06 15:10:30
字體:
來源:轉載
供稿:網友

組件間通信(父子,兄弟)

相關鏈接/組件通信:點擊查看

學習鏈接:Vue.js——60分鐘快速入門點擊查看

分分鐘玩轉Vue.js組件點擊查看

父組件傳子組件

父傳子方法(一) 屬性傳遞 props

//子組件<template>  <ul> <li v-for="item in dataList">{{item}}</li> </ul> </template><script> export default {  props : { dataList : [] } }</script>
//父組件<template> <component-child v-bind:data-list="dataList"> </component-child>  <input v-model="dataInput" v-on:keyup.13="addDataItem()" ></input></template><script>import ComponentChild from './child.vue'export default {  data () {  return {  dataInput: "",  dataList : [ 'hello world!','welcome to use vue.js' ]  }  },  components : { ComponentChild },  methods : {  addDataItem () {  let self = this  if( !(self.dataInput && self.dataInput.length > 0) ) { return }  self.dataList.push( self.dataInput )  self.dataInput = ""  }  }}</script>

父傳子方法(二) 廣播事件傳遞 vm.$broadcast

//子組件<template>  <ul>  <li v-for="item in dataList">{{item}}</li>  </ul> </template><script>export default {  data () {  return {  dataList : [ 'hello world!', 'welcome to use vue.js' ]  }  },  events : {  addChildDataEvent : function ( dataInput ) {  this.dataList.push( dataInput )  }  }}</script>
//父組件<template>  <component-child></component-child>  <input v-model="dataInput" v-on:keyup.13="addDataItem()" ></input></template><script> import ComponentChild from './child.vue' export default {  data () {  return { dataInput: "" }  },  components : { ComponentChild },  methods : {  addDataItem () {  let self = this  if( !(self.dataInput && self.dataInput.length > 0) ) { return }  //廣播到子組件  self.$broadcast( 'addChildDataEvent', self.dataInput )  self.dataInput = "" }  } }</script>

子組件傳父組件

子傳父方法 派遣事件傳遞 vm.$dispatch

//子組件<template>  <input v-model="dataInput" v-on:keyup.13="addDataItem()" ></input></template><script> export default {  data () {  return {  dataInput: ""  }  },  methods : {  addDataItem () {  let self = this if( !(self.dataInput && self.dataInput.length > 0) ) { return } //派遣事件到父組件  self.$dispatch( 'addFatherDataEvent', self.dataInput ) self.dataInput = ""  }  } }</script>
//父組件<template>  <ul>  <li v-for="item in dataList">{{item}}</li>  </ul>  <component-child></component-child></template><script>import ComponentChild from './child.vue'export default {  data () {  return {  dataList : [ 'hello world!', 'welcome to use vue.js' ]  }  }, components : { ComponentChild },  events : {  addFatherDataEvent : function ( dataInput ) {  this.dataList.push( dataInput )  }  }}</script>            
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 永定县| 东宁县| 荣成市| 广昌县| 开封市| 临清市| 东乡族自治县| 云安县| 象山县| 阿拉善左旗| 霞浦县| 嘉禾县| 莎车县| 阳泉市| 章丘市| 嘉禾县| 绵竹市| 新竹县| 碌曲县| 开鲁县| 汤原县| 永胜县| 浙江省| 南充市| 伊春市| 高雄县| 项城市| 宣化县| 登封市| 万盛区| 都兰县| 论坛| 牙克石市| 德清县| 潢川县| 嵊泗县| 龙川县| 阿瓦提县| 芦山县| 合作市| 荣成市|