Taro: 函数传参无法取到

Created on 5 Jan 2019  ·  9Comments  ·  Source: NervJS/taro

问题描述
事件绑定传参, 取不到参数, 取到的是 arguments对象

复现步骤

handleClick = (item) => {
    console.log(item);
  };

render() {
    return (
      <View>
        {[1,2].map((t, r) => (
          <Button key={r} onClick={this.handleClick.bind(this, t)}>
            {t}
          </Button>
        ))}
      </View>
    )
  }

期望行为
可以正常的拿到事件函数的参数

报错信息
image

系统信息
image

  • 报错平台: weapp

补充信息
最新依赖:
image

Most helpful comment

我更新完,也需要上面的问题,

onClick={this.handleClick.bind(this, 1)} 

handleClick = (type) => {console.log(type)}

wx20190105-165811 2x

All 9 comments

欢迎提交 Issue~

如果你提交的是 bug 报告,请务必遵循 Issue 模板的规范,尽量用简洁的语言描述你的问题,最好能提供一个稳定简单的复现。🙏🙏🙏

如果你的信息提供过于模糊或不足,或者已经其他 issue 已经存在相关内容,你的 issue 有可能会被关闭。

Good luck and happy coding~

我更新完,也需要上面的问题,

onClick={this.handleClick.bind(this, 1)} 

handleClick = (type) => {console.log(type)}

wx20190105-165811 2x

是的, 我什么方法都试了, 比如删除 node_modules , npm update, 还是取不到正常的值

@hyyqcweb @ZhiGang-Fang Taro 1.2.4 版本上尝试,没有问题,能正确取值。

你们的问题肯定是 CLI 和依赖版本不一致导致的,可以删掉 node_modules 后手动 npm install 试试。

import Taro, { Component } from '@tarojs/taro'
import { View } from '@tarojs/components'

export default class PageView extends Component {
  handleClick = (item) => {
    console.log(item)
  }

  render() {
    return (
      <View>
        {[1,2].map((t, r) => (
          <View key={r} onClick={this.handleClick.bind(this, t)}>
            {t}
          </View>
        ))}
      </View>
    )
  }
}

image

这个问题怎么解决的呢?我用的taro版本是1.2.12

@wswax 要么升级taro版本依赖(必须统一), 要么降级taro版本(我是降级处理了)

@hyyqcweb 嗯我刚刚升级了。
目前是这样子,可以了。

Taro v1.2.13

Taro CLI 1.2.13 environment info:
System:
OS: macOS High Sierra 10.13.6
Shell: 3.2.57 - /bin/bash
Binaries:
Node: 10.14.2 - /usr/local/bin/node
Yarn: 1.9.4 - /usr/local/bin/yarn
npm: 6.4.1 - /usr/local/bin/npm
npmPackages:
@tarojs/async-await: ^1.2.0 => 1.2.12
@tarojs/components: ^1.2.0 => 1.2.12
@tarojs/plugin-babel: ^1.2.0 => 1.2.12
@tarojs/plugin-csso: ^1.2.0 => 1.2.12
@tarojs/plugin-sass: ^1.2.0 => 1.2.12
@tarojs/plugin-uglifyjs: ^1.2.0 => 1.2.12
@tarojs/redux: ^1.2.0 => 1.2.12
@tarojs/redux-h5: ^1.2.0 => 1.2.12
@tarojs/router: ^1.2.0 => 1.2.12
@tarojs/taro: ^1.2.0 => 1.2.12
@tarojs/taro-alipay: ^1.2.0 => 1.2.12
@tarojs/taro-h5: ^1.2.0 => 1.2.12
@tarojs/taro-swan: ^1.2.0 => 1.2.12
@tarojs/taro-tt: ^1.2.0 => 1.2.12
@tarojs/taro-weapp: ^1.2.0 => 1.2.12
@tarojs/webpack-runner: ^1.2.0 => 1.2.12
eslint-config-taro: ^1.2.0 => 1.2.12
eslint-plugin-taro: ^1.2.0 => 1.2.12
nervjs: ^1.3.9 => 1.3.12

@wswax CLI 和依赖要保持一致,不然又可能出现其它问题。你现在分别是 1.2.13 和 1.2.12

@Chen-jj 嗯,多谢提醒。
刚重新安装了一下。
Taro v1.2.13

Taro CLI 1.2.13 environment info:
System:
OS: macOS High Sierra 10.13.6
Shell: 3.2.57 - /bin/bash
Binaries:
Node: 10.14.2 - /usr/local/bin/node
Yarn: 1.9.4 - /usr/local/bin/yarn
npm: 6.4.1 - /usr/local/bin/npm
npmPackages:
@tarojs/async-await: ^1.2.0 => 1.2.13
@tarojs/components: ^1.2.0 => 1.2.13
@tarojs/plugin-babel: ^1.2.0 => 1.2.13
@tarojs/plugin-csso: ^1.2.0 => 1.2.13
@tarojs/plugin-sass: ^1.2.0 => 1.2.13
@tarojs/plugin-uglifyjs: ^1.2.0 => 1.2.13
@tarojs/redux: ^1.2.0 => 1.2.13
@tarojs/redux-h5: ^1.2.0 => 1.2.13
@tarojs/router: ^1.2.0 => 1.2.13
@tarojs/taro: ^1.2.0 => 1.2.13
@tarojs/taro-alipay: ^1.2.0 => 1.2.13
@tarojs/taro-h5: ^1.2.0 => 1.2.13
@tarojs/taro-swan: ^1.2.0 => 1.2.13
@tarojs/taro-tt: ^1.2.0 => 1.2.13
@tarojs/taro-weapp: ^1.2.0 => 1.2.13
@tarojs/webpack-runner: ^1.2.0 => 1.2.13
eslint-config-taro: ^1.2.0 => 1.2.13
eslint-plugin-taro: ^1.2.0 => 1.2.13
nervjs: ^1.3.9 => 1.3.12

Was this page helpful?
0 / 5 - 0 ratings