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

fix: native component event support #248

Open
wants to merge 9 commits into
base: master
Choose a base branch
from

Conversation

StKelen
Copy link

@StKelen StKelen commented Nov 24, 2021

在原生小程序自定义组件中,如果想在这个组件的标签上挂一个事件,并在组件内通过triggerEvent来进行出发。
<NativeComp onClick={event} />,这里的NativeComp是一个npm原生小程序自定义组件。在组件内通过triggetEvent('onClick', e)的方式进行触发。这个组件应该被编译为<native-comp bindonClick="_e0" />这种形式,即对应的事件前应该有bind以允许出发该事件。
而目前的编译结果仍然为<native-comp onClick="_e0" />,编译结果与期望不一致。故支持通过判断该组件是否为原生组件,如果是则会将on开头的属性修改为bindon的格式,以支持事件绑定。

/**
* Judge whether the component has native compiled component
*/
if (pkg && pkg.miniappConfig && pkg.miniappConfig[`main:${platform}`]) node.name.isNative = true;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

支付宝小程序的字段应该直接是 main

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

另外这句是不是直接写成赋值语句就好了

*/
module.exports = function isBaseComponent(node) {
// Rax base components and native base components are recognized as base components
return baseComponents.indexOf(node.name) > -1 || node && node.isNative;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

前面已经用到 node.name 了,这里不需要 node && node.isNative

@SoloJiang
Copy link
Contributor

另外,几个内部变量最好写一下分别什么场景用的,现在确实有点乱了

@StKelen
Copy link
Author

StKelen commented Nov 24, 2021

已更新~

* Judge whether the component has native compiled component
* If it has, the property isNative should true, otherwise it would be false.
*/
node.name.isNative = !!getCustomComponentPath(pkg, platform) ? true : false;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

!!getCustomComponentPath(pkg, platform) 本身就是布尔值

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

已更新~!!!

*/
const platformIdentifier = platform === 'ali' ? 'main' : `main:${platform}`
return pkg && pkg.miniappConfig && pkg.miniappConfig[platformIdentifier] || null
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个方法是不是有地方用到过了我印象里

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

Successfully merging this pull request may close these issues.

2 participants