Taro 方法如何传参

Taro 方法如何传参

import Taro, { Component } from '@tarojs/taro'
import { View, Text,Button} from '@tarojs/components'
import './index.scss'

export default class Index extends Component {
  constructor () {
    super(...arguments)
    this.state = {
      title: '首页',
    }
  }
  /* 注意这里的参数顺序有点奇葩,只要有事件参数e必须放在最后面,否则报错;
   * 传参的时候必须要用bind,并且必须把this参数放在最前面
   */
  add = (str,e) => {
    e.stopPropagation();
    this.setState({
      title: str
    })

  }
  render () {
    return (
      <View className='index'>
        <View className='title'>{this.state.title}</View>
        <View className='content'>
          <Button className='add' onClick={this.add.bind(this,'Home')}>添加</Button>
        </View>
      </View>
    )
  }
}

LaoGuan 5年前

注意这里的参数顺序有点奇葩,只要有事件参数e必须放在最后面,否则报错; 传参的时候必须要用bind,并且必须把this参数放在最前面