Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

map 渲染微信自定义组件时,无法绑定柯里化方法或匿名函数为自定义事件 #3338

Closed
dizys opened this issue Jun 5, 2019 · 2 comments
Assignees

Comments

@dizys
Copy link

dizys commented Jun 5, 2019

问题描述

tsx(jsx) 中使用 {array.map(...)} 语法渲染列表时,若列表项为原生微信小程序自定义组件(即混写小程序组件),自定义事件的绑定只能为该 Taro.Component 类的非柯里化的方法。当试图为自定义组件绑定柯里化的方法或者匿名函数(/lambda函数)时,会找不到方法控制台输出警告而绑定失败。

复现步骤

  • 原生自定义组件

test-component.wxml

<view bind:tap="innerSoundTap">test</view>

test-component.ts

Component({
    methods: {
        innerSoundTap() {
            this.triggerEvent('soundTap');
        }
    },
});
  • Taro 组件

learn.tsx

class Learn extends Component {
    config: Config = {
        usingComponents: {
            'test-component': './test-component',
        },
    };

    render() {
        return (
            <View class="wrapper">
                {[1, 2].map(val => (
                    <test-component onSoundTap={this.onTestSoundTap(val)} />
                ))}
            </View>
        );
    }

    private onTestSoundTap = (val: number) => () => {
        console.log(val);
    }
}

期望行为

事件成功绑定

报错信息

警告信息:

VM1962:1 Component "pages/learn/learn" does not have a method "anonymousFunc1.bind(this, value._$indexKey2)" to handle event "soundTap".

系统信息

Taro CLI 1.3.0-beta.3 environment info:
System:
OS: Windows 10
Binaries:
Node: 10.13.0 - D:\Dev\Env\NodeJs\node.EXE
Yarn: 1.16.0 - ~\AppData\Roaming\npm\yarn.CMD
npm: 6.4.1 - D:\Dev\Env\NodeJs\npm.CMD

补充信息
使用<View>作为列表项绑定onClick事件的wxml部分编译结果(行为正常):

<view 
    bindtap="anonymousFunc1" 
    wx:for="{{loopArray39}}" 
    wx:for-item="value" 
    wx:for-index="__index1" 
    data-e-tap-so="this" 
    data-e-tap-a-a="{{value._$indexKey2}}"
>
    {{value.$original}}
</view>

使用自定义原生微信小程序组件绑定自定义事件对应的wxml编译结果(行为不正常):

<test-component 
    bindsoundTap="anonymousFunc1.bind(this, value._$indexKey2)" 
    wx:for="{{loopArray35}}" 
    wx:for-item="value" 
    wx:for-index="__index1" 
    __fn_onSoundTap="{{true}}"
></test-component>

可以推得Taro转译器的正确编译结果应为:

已验证的希望Taro能得到的正确编译结果(已验证:行为正常):

<test-component  
    bindsoundTap="anonymousFunc1" 
    wx:for="{{loopArray35}}" 
    wx:for-item="value" 
    wx:for-index="__index1" 
    __fn_onSoundTap="{{true}}" 
    data-e-soundTap-so="this" 
    data-e-soundTap-a-a="{{value._$indexKey2}}"
></test-component>
@taro-bot
Copy link

taro-bot bot commented Jun 5, 2019

欢迎提交 Issue~

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

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

Good luck and happy coding~

@dizys dizys changed the title map 渲染的原生组件列表设定自定义事件时无法使用柯里化函数或者lambda函数 map 渲染微信自定义组件时,无法绑定柯里化方法或匿名函数为自定义事件 Jun 5, 2019
@yuche
Copy link
Contributor

yuche commented Jun 6, 2019

第三方组件不打算搞那么多花式方法了,给编译期徒增压力。准备在文档事件处理中写明第三方组件不能这么写。

@yuche yuche added answered question Further information is requested and removed answered question Further information is requested labels Jun 6, 2019
@yuche yuche self-assigned this Jun 6, 2019
@yuche yuche closed this as completed in 2f9ac44 Jun 6, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants