免费视频淫片aa毛片_日韩高清在线亚洲专区vr_日韩大片免费观看视频播放_亚洲欧美国产精品完整版

打開APP
userphoto
未登錄

開通VIP,暢享免費電子書等14項超值服

開通VIP
vue 子組件觸發(fā)父組件方法的兩種方式
父組件:

<template>
<div>
<div @click="openChild">彈彈彈,彈出子組件</div>
<childs ref="alert" @watchChild="parentReceive"></childs> <!--監(jiān)聽子組件綁定的方法-->
</div>
</template>
 
<script>
import childs from '../components/modal/Alert'
export default{
data(){
return {
msg: '我是父組件的msg!?。?
}
},
components:{
childs
},
methods:{
parentReceive(message){
alert(message);
},
openChild(){
this.$refs.alert.open('我是子組件','子組件內(nèi)容?。。。。?);
}
}
}
</script>
 
<style>
</style>
子組件:

<template>
  <modal :show='show' ref="modal">
    <div slot="title">{{title}}</div>
    <div slot="content">{{content}}</div>
    <div slot="buttons" class="modal-buttons">
      <span class="modal-button" @click="_onClick()">確定</span>
    </div>
  </modal>
</template>
 
<script>
import Modal from './Modal'
 
export default {
  props: {
    show: {
      type: Boolean,
      default: false
    },
    okText: {
      type: String,
      default: '確定'
    },
  },
  data(){
  return {
  title: '',
  content: ''
  }
  },
  components: {
    Modal
  },
  methods: {
    open (title, content) {
    this.title = title
    this.content = content
      this.$refs.modal.open()
    },
    close () {
    this.title = ''
    this.content = ''   
      this.$refs.modal.close()
    },
_onClick(){
this.close();
this.$emit('watchChild','我是從子組件傳過來的內(nèi)容?。?!');    //觸發(fā)$emit綁定的watchChild方法
}
  }
}
</script>
第一種方法:

如上:通過this.$emit()來觸發(fā)父組件的方法。具體就是子組件觸發(fā)$emit綁定的事件watchChild,然后父組件監(jiān)聽watchChild,一旦watchChild被觸發(fā)便會觸發(fā)父組件的parentReceive方法。

父組件:

<template>
<div>
<div @click="openChild">彈彈彈,彈出子組件</div>
<childs ref="alert" :on-ok="parentReceive"></childs>  <!--把父組件的方法名傳給子組件的onOK屬性-->
</div>
</template>
 
<script>
import childs from '../components/modal/Alert'
export default{
data(){
return {
msg: '我是父組件的msg!??!'
}
},
components:{
childs
},
methods:{
parentReceive(message){
alert(message);
},
openChild(){
this.$refs.alert.open('我是子組件','子組件內(nèi)容!?。。?!');
}
}
}
</script>
 
<style>
</style>
子組件:

<template>
  <modal :show='show' ref="modal">
    <div slot="title">{{title}}</div>
    <div slot="content">{{content}}</div>
    <div slot="buttons" class="modal-buttons">
      <span class="modal-button" @click="_onClick()">確定</span>
    </div>
  </modal>
</template>
 
<script>
import Modal from './Modal'
 
export default {
  props: {
    show: {
      type: Boolean,
      default: false
    },
    okText: {
      type: String,
      default: '確定'
    },
    onOk: {    //定義onOK屬性
      type: Function
    }
  },
  data(){
  return {
  title: '',
  content: ''
  }
  },
  components: {
    Modal
  },
  methods: {
    open (title, content) {
    this.title = title
    this.content = content
      this.$refs.modal.open()
    },
    close () {
    this.title = ''
    this.content = ''   
      this.$refs.modal.close()
    },
_onClick(){
this.close();
if(this.onOk){
this.onOk('我是子組件傳遞過來的數(shù)據(jù)?。。。?);
}
}
  }
}
</script>
第二種方法:

在子組件props中定義屬性onOK,類型為function,然后在父組件中把要觸發(fā)的方法名傳遞給onOK屬性,最后在子組件中判斷onOk是否存在,是的話直接執(zhí)行這個方法。

效果:


--------------------- 
作者:yemuxia_sinian 
來源:CSDN 
原文:https://blog.csdn.net/yemuxia_sinian/article/details/80534296 
版權(quán)聲明:本文為博主原創(chuàng)文章,轉(zhuǎn)載請附上博文鏈接!
本站僅提供存儲服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊舉報
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
Vue 在父(子)組件引用其子(父)組件方法和屬性
Vue3.x 從零開始(五)—— Router + Vuex + TypeScript 實戰(zhàn)演練(上)
vue.js2.0父組件點擊觸發(fā)子組件方法
vue3 teleport的使用案例詳解
Vue.js - Day4
Vue.js
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長圖 關(guān)注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點擊這里聯(lián)系客服!

聯(lián)系客服