文章搬遷搬遷~~
因為自己弄了HEXO將來文章會放到那,這裡將停止更新,而文章也會慢慢更新到那裏去,歡迎大家來看與指教。 網址:https://donggualemon.github.io/
<template>
<child :text="text1" message='I am a child.' ></child>
</template>
<script>
import child from './child.vue';
export default {
components: {
child
},
data () {
return {
text1: 'father props';
}
}
}
<script>
子組件程式碼
<template>
<div>{{text}}{{message}}</div>
</template>
<script>
export default {
props: {
text:"",
message:""
}
}
<script>
這樣就能把數值、資料傳到父組件,我們來整理一下props功能吧!
<template>
<child @childpass="func" ></child>
</template>
<script>
import child from './child.vue';
export default {
components: {
child
},
methods:{
func(){
//假如有傳送值,記得塞入function.
console.log("hello,this is father")
}
}
}
<script>
子組件程式碼
<template>
<button @click="emitevents"></button>
</template>
<script>
export default {
methods:{
emitevents(){
console.log("hello,this is child")
this.$emit('childpass')//呼叫事件
this.$emit('childpass',value)//假如有數值等等
}
}
}
<script>
這個就是簡單的emit操作,而emit還可以傳送在兄弟元件之中,需要搭配$on來監聽事件,$emit來觸發事件,有興趣的各位可以去官方文件看看。
留言
張貼留言