vue 父子元件通訊方法

2021-08-17 16:09:06 字數 770 閱讀 9204

父元件傳送data資料給子元件

:data="data">
子元件接收

export

default

父元件定義乙個show的方法,並且傳達給子元件,命名為@show

@show="show">

show()

子元件直接用$emit呼叫@show方法

this.

$emit('show');

還可以傳參

show(params)

this.

$emit('show', 'i am children!');

父元件

我是父元件h1>

ref="child">

child>

div>

template>

import child from './child'

export default ,

methods:

}}script>

子元件

我是子元件h2>

div>

template>

import child from './child'

export default ,

methods:

}}script>

Vue父子元件通訊

1.父與子通訊,通過屬性傳遞值,如下 lang zh cn charset utf 8 src js vue.js script title head id example p parent div type text x template id parenttpl 這是父元件h1 name zha...

vue父子元件通訊

一 父元件利用props往子元件傳輸資料 父元件 注意傳遞引數時要用 代替駝峰命名,html不區分大小寫 子元件 vue.component child template 二 子元件向父元件傳遞引數利用事件機制 子元件通過this.emit 派發事件,父元件利用v on對事件進行監聽,實現引數的傳遞...

Vue父子元件通訊

抓住幾個語義特點就可以了。子元件要接收父元件傳過來的內容,需要引入props,也就是說有props就是子元件 以下是子元件的內容 p div template export default script 父元件需要往子元件傳入引數,則某個部分要和引數post對應,即 v bind post post...