小程序:组件间通信与事件
<children count="{{count}}"></children>
Page({
data: {
count: '114514'
}
})
properties: {
count: Number
},
<children bind:parentevent="parentMethod"></children>
Page({
parentMethod (e) {
console.log('子组件传来的值',e.detail.value)
}
})
<button bindTap="childrenTap">子组件按钮</button>
Component({
data: {
childrenText: 'this is children!'
},
Methods: {
childrenTap () {
this.triggerEvent('parentevent',{value: this.data.childrenText})
}
}
})
<button bindtap="getChildComponent">获取子组件实例</button>
Page({
getChildComponent: function(){
const child = this.selectComponent('.children')
console.log(child)
},
})
vue3: 深入组件
<template>
<children-component :count="123"></children-component>
</template>
<script setup>
defineProps(['count'])
</script>
<template>
<h1>{{count}}</h1>
</template>
<script setup>
import { ref } from 'vue'
const count = ref(1)
import ChildrenComponent from './components/ChildrenComponent.vue'
function addCount(e) {
count.value++
console.log(e)
}
</script>
<template>
<children-component :count="123" @add-count="addCount"></children-component>
<h1>{{ count }}</h1>
</template>
<script setup>
import { ref } from 'vue';
defineProps(['count'])
defineEmits(['add-count'])
const childrenMsg = 'hello kugou'
</script>
<template>
<h1>{{count}}</h1>
<button @click="$emit('add-count',childrenMsg)">子组件按钮</button>
</template>
<script setup>
import { ref } from 'vue'
const childRef = ref(null)
import ChildrenComponent from './components/ChildrenComponent.vue'
function getChildComponent () {
console.log(childRef.value.childrenMsg)
childRef.value.childrenMethods()
}
</script>
<template>
<children-component ref="childRef"></children-component>
<button @click="getChildComponent">父组件按钮</button>
</template>
<script setup>
defineExpose({
childrenMsg,
childrenMethods
})
const childrenMsg = 'hello kugou'
function childrenMethods () {
console.log('ok')
}
</script>
<template>
<h1>{{childrenMsg}}</h1>
</template>
react通过props传值
import ChildrenComponent from "../Components/ChildrenComponent"
export default function App () {
let count = 114514
return (
<>
<ChildrenComponent count={count}></ChildrenComponent>
</>
)
}
import React from 'react'
export default function ChildrenComponent({count}) {
return (
<div>{count}</div>
)
}
import ChildrenComponent from "../Components/ChildrenComponent"
export default function App () {
let count = 114514
function clickHandler (str) {
console.log('parent clicked!',str)
}
return (
<>
<ChildrenComponent count={count} parentMethods={clickHandler}></ChildrenComponent>
<button onClick={clickHandler}>父组件按钮+1</button>
</>
)
}
import React from 'react';
export default function ChildrenComponent({ count,parentMethods }) {
let str = 'aotuman'
function childrenHandler () {
parentMethods(str)
}
return (
<>
<div>{count}</div>
<button onClick={childrenHandler}>子组件按钮触发父组件事件</button>
</>
)
}
import React from 'react';
import ChildrenComponent from "../Components/ChildrenComponent"
export default function App () {
let child = null;
function parentHandler () {
console.log(child.str);
child.childrenHandler();
}
function getChild (instance) {
child = instance;
}
return (
<>
<ChildrenComponent getChild={getChild} ></ChildrenComponent>
<button onClick={parentHandler}>父组件按钮触发子组件事件</button>
</>
)
}
import React, { useEffect } from 'react';
export default function ChildrenComponent({ getChild }) {
let str = 'aotuman'
function childrenHandler () {
console.log('children click!')
}
useEffect(() => {
getChild({ str, childrenHandler });
}, []);
return (
<>
<button onClick={childrenHandler}>子组件按钮</button>
</>
);
}
相同点:
都是通过props属性来实现父组件向子组件传值。
都是通过事件或回调函数来实现子组件向父组件传值。
不同点:
- 小程序的props属性可以传递任意类型的数据,而vue和react的props属性只能传递基本类型的数据,如果要传递对象或数组,需要使用v-bind或{}语法。
- 小程序的子组件可以直接修改父组件传递过来的props属性,而vue和react的子组件不能直接修改父组件传递过来的props属性,需要使用emit或setState方法。
- vue和react的子组件可以使用defineProps或propTypes来定义和校验接收的props属性,而小程序的子组件没有这个功能。
- react的父组件可以使用createContext和useContext来实现跨组件传值,而vue和小程序没有这个功能。
小程序 | vue | react | |
---|---|---|---|
父向子传值 | props | props | props |
子向父传值 | 事件/回调函数 | 事件/回调函数 | 事件/回调函数 |
props类型 | 任意类型 | 基本类型 | 基本类型 |
props修改 | 直接修改 | emit方法 | setState方法 |
props定义和校验 | 无 | defineProps | propTypes |
跨组件传值 | 无 | 无 | createContext/useContext |