父组件 index.js
import Taro, { Component } from '@tarojs/taro'
import { View, Text, Button } from '@tarojs/components'
import './index.less'
import Hello from "../../components/hello/hello.js"
import MyComponent from '../../components/myComponent/myComponent.js'
export default class Index extends Component {
constructor(){
this.state = {message:'hello taro'}
}
config = {
navigationBarTitleText: '首页'
}
componentWillMount () {
console.log('组件挂载前');
console.log('componentWillMount');
Taro.hideShareMenu()
}
componentDidMount () {
console.log('组件挂载完成');
console.log('componentDidMount');
}
componentWillUnmount () {
console.log('组件卸载前')
console.log('componentWillUnmount');
}
componentDidShow () {
console.log('组件显示')
console.log('componentDidShow');
}
componentDidHide () {
console.log('组件隐藏')
console.log('componentDidHide');
}
toCate(){
Taro.navigateTo({
url:"/pages/cate/cate"
})
}
setVal(){
this.setState({
message: '我变了'
})
}
onShareAppMessage(){
return {
title: '分享标题',
path: '/pages/cate/cate?id=1'
}
}
render () {
const {message} = this.state
return (
<View className='index'>
<View>我是变量:{message}</View>
<Text>我是首页</Text>
<Button onClick={this.toCate.bind(this)} >跳转路由</Button>
<Button onClick={this.setVal.bind(this)} >设置变量</Button>
<Button openType="share" >分享功能</Button>
<Hello message={message}></Hello>
<MyComponent params ="参数"></MyComponent>
</View>
)
}
}
子组件 hello.js
import Taro, { Component } from '@tarojs/taro'
import { View, Text } from '@tarojs/components'
export default class Hello extends Component {
constructor(){
super(...arguments)
}
static defaultProps = {
message: '我是子组件',
loading: true,
}
componentWillMount () {
console.log('组件挂载前');
console.log('componentWillMount');
}
componentWillReceiveProps(){
console.log('componentWillReceiveProps', this.props);
}
componentDidMount () {
console.log('组件挂载完成');
console.log('componentDidMount');
}
componentWillUnmount () {
console.log('组件卸载前')
console.log('componentWillUnmount');
}
componentDidShow () {
console.log('组件显示')
console.log('componentDidShow');
}
componentDidHide () {
console.log('组件隐藏')
console.log('componentDidHide');
}
render () {
const {message} = this.props
return (
<View className='index'>
<View>hello 组件 为什么接收不到父组件的传值 </View>
<Text>{message}</Text>
</View>
)
}
}
现在组件是可以显示 但是子组件无法接手父组件的传值
请移步taro官方社区