傳值(父子、子父)


父組件→子組件
<div class="father">
    <child :inputName="name">
</div>

<div class="child">
    {{inputName}}
</div>
通過props接受數據
(1)props: {
   inputName: String,
   required: true
  }
(2)props: ["inputName"]



子組件→父組件
子組件使用 $emit('方法名','其他參數')傳遞

子組件:
<input type="button" value="點擊觸發" @click="childClick">
data:{
    childdata:'hello'
}
methods:{
    childClick(){
        this.$emit('goFather',childdata)
    }
}

父組件:
<child @goFather="fatherFunction"></child>
methhods:{
    fatherFunction(content){
        console.log(content) //content是傳遞上來的數據
    }
}


父組件調用子組件的方法
父組件:
<div @click="parentMethod">
    <children ref="child"></children>
</div>

this.$refs.child.childMethods(); //childMethods()是子組件中的方法


父子組件之間修改數據

this.$children.msg='123'
this.$parent.msg='456'

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM