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

vant-weapp 组件属性默认值丢失 #11575

Closed
HyperLife1119 opened this issue Apr 3, 2022 · 9 comments · Fixed by #11830
Closed

vant-weapp 组件属性默认值丢失 #11575

HyperLife1119 opened this issue Apr 3, 2022 · 9 comments · Fixed by #11830
Assignees
Labels
F-vue3 Framework - Vue 3 T-weapp Target - 编译到微信小程序 V-3 Version - 3.x

Comments

@HyperLife1119
Copy link
Contributor

HyperLife1119 commented Apr 3, 2022

相关平台

微信小程序

复现仓库

https://github.com/HyperLife1119/taro-issue
小程序基础库: 2.23.2
使用框架: Vue 3

复现步骤

van-empty 组件的 image 属性默认值为 'default',如果不填的话,默认值是生效的,例如:

<van-empty />

但在这种情况下,属性默认值丢失了:

<van-empty image="xxx" />
<!-- 第二个 van-empty 组件的 image 属性默认值丢失了 -->
<van-empty />

因为这时候 van-empty 组件的 image 属性被收集了,但第二个的 van-empty 组件并没有设置该属性,导致生成模板的时候给 image 属性赋了个空值(undefined)。

期望结果

组件属性默认值不会丢失

实际结果

组件属性默认值在特定情况下丢失了

环境信息

👽 Taro v3.4.1


  Taro CLI 3.4.1 environment info:
    System:
      OS: Windows 10
    Binaries:
      Node: 16.13.2 - C:\Program Files\Nodejs\node.EXE
      npm: 8.1.2 - C:\Program Files\Nodejs\npm.CMD
@taro-bot2 taro-bot2 bot added F-vue3 Framework - Vue 3 T-weapp Target - 编译到微信小程序 V-3 Version - 3.x labels Apr 3, 2022
@HyperLife1119
Copy link
Contributor Author

@Chen-jj 这个 issue 被遗忘了

@Chen-jj Chen-jj self-assigned this Apr 16, 2022
@HyperLife1119
Copy link
Contributor Author

HyperLife1119 commented Apr 23, 2022

应该提供一个配置,让开发者可以自行补全第三方组件的属性与默认值。

@HyperLife1119
Copy link
Contributor Author

类似的问题:
#9541
#8955

@HyperLife1119
Copy link
Contributor Author

https://github.com/HyperLife1119/taro-plugin-vant-weapp

最后还是编写了一个插件来解决此问题。

@Chen-jj
Copy link
Contributor

Chen-jj commented May 21, 2022

Taro v3.4.10 + @tarojs/plugin-inject v1.0.2 开始支持为第三方自定义组件的属性设置默认值。

已补充文档与用法:https://github.com/NervJS/taro-plugin-inject/tree/v1.0.2#6-thirdpartycomponents

@Chen-jj Chen-jj closed this as completed May 21, 2022
@HyperLife1119
Copy link
Contributor Author

HyperLife1119 commented May 21, 2022

还有的就是文档中的这个说法并不正确:

注意:如果组件的某些属性在 vant 文档里写着带有默认值 true,在 Taro 中使用时仍然需要显式设置为 true。

https://taro-docs.jd.com/taro/docs/vant#%E4%BD%BF%E7%94%A8-vant-%E7%BB%84%E4%BB%B6

在此之前,只有遇到这个 issue 描述的情况下,默认值会被置为 undefined,需要显示设置(而不只是默认值为 true 才需要显示设置)。

现在文档应该更新为:推荐使用 @tarojs/plugin-inject 自行补全第三方组件默认值。

@Chen-jj

@Chen-jj
Copy link
Contributor

Chen-jj commented May 21, 2022

那句是面向另一个问题,即 Vue 属性值 true 值如何设置的问题。

.zone 域名的问题已针对此问题更新了文档:https://docs.taro.zone/docs/vant#%E5%A4%84%E7%90%86-vant-%E7%BB%84%E4%BB%B6%E9%BB%98%E8%AE%A4%E5%80%BC%E5%A4%B1%E6%95%88%E7%9A%84%E9%97%AE%E9%A2%98

Chen-jj added a commit that referenced this issue May 23, 2022
* feat(shared): 支持为第三方自定义组件的属性设置默认值,fix #11575

* chore: change use of isString
@maopixin
Copy link

应该提供一个配置,让开发者可以自行补全第三方组件的属性与默认值。

难道所有组件都需要自己添加默认属性吗

@HyperLife1119
Copy link
Contributor Author

应该提供一个配置,让开发者可以自行补全第三方组件的属性与默认值。

难道所有组件都需要自己添加默认属性吗

是的。对于 vant-weapp,我编写了一个插件:https://github.com/HyperLife1119/taro-plugin-vant-weapp

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
F-vue3 Framework - Vue 3 T-weapp Target - 编译到微信小程序 V-3 Version - 3.x
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants