VantWeapp小程序蹲坑之使用checkbox组件-创新互联
引子
checkbox-group及checkbox组件是开发小程序中频繁使用的组件,当然在vant weapp组件库中对应的分别是van-checkbox-group和van-checkbox。遗憾的是,官方提供的仅是组件在原生微信小程序框架下的用法,但我们所关注的是如何把这些组件改写到mpvue框架中。有关此二组件常用属性,在文后引用地址(官方)都有详细介绍,在此省略。

象州ssl适用于网站、小程序/APP、API接口等需要进行数据传输应用场景,ssl证书未来市场广阔!成为创新互联的ssl证书销售渠道,可以享受市场价格4-6折优惠!如果有意向欢迎电话联系或者加微信:13518219792(备注:SSL证书合作)期待与您的合作!
问题1:数据绑定与模板字符串问题
这个问题困绕了我很长时间,搜索网络,包括google英文搜索,几乎没有找到相应的答案。首先,请看下面官方提供的组件在原生微信小程序框架下的用法:
请注意上面van-cell组件的title组件与van-checkbox组件的class属性表达方式,在转换成mpvue框架下的表达时,我相当然地表达成下面这样:
结果编译都通过不了!
解答:
通过美团开源官方上问题集处(引用地址2)得到初步答案提示,说:
“目前是直接把 template 转译成 wxml ,wxml 不支持 ``,所以目前暂无法支持”
即由于微信小程序官方(时间是2018年8月,至今仍然不支持)的wxml语法中不支持ES6的模板字符串表达方式,所以mpvue官方(因为其最终也是走转换成wxml的路子)也宣布不支持,只是没有在醒目的文档说明中提到(这真正是一个“坑”啊)。那么,如何修改上面非常明确的常用需求呢?经过反复试验,方式如下(只写关键部分):
:title="'复选框'+item"
:class="'checkboxes-'+item"
有上述需求的同学可要好好观察一下了!这里没有使用到模板字符串中反向单引号,在双引号的里面可以归纳成由单引号字符串组成的通过加号连接的字符串加法,只不过参与加法的一些部分是字符串变量而已。
问题2:selectComponent方法的使用
官方资料中提到:selectComponent用于微信小程序开发中获取自定义子组件,详情见引用资料3。那么,官方示例中提到的方法(如下)如何改写呢?
toggle(event) {
const { name } = event.currentTarget.dataset;
const checkbox = this.selectComponent(`.checkboxes-${name}`);
checkbox.toggle();
}
请看下面的答案。
# 解答:
toggle(event) {
const {name} = event.mp.currentTarget.dataset;
const box =this.$mp.page.selectComponent(`.checkboxes-${name}`)
box.toggle();
}
注意,上面使用了标准的ES6中模板字符串表达方式,因为这里不是wxml文件中,而是vue文件中的
基本
文件
流程
错误
SQL
调试
- 请求信息 : 2026-05-20 15:30:32 HTTP/1.1 GET : /article/dopdih.html
- 运行时间 : 1.6396s ( Load:0.0065s Init:0.9701s Exec:0.6533s Template:0.0096s )
- 吞吐率 : 0.61req/s
- 内存开销 : 2,231.97 kb
- 查询信息 : 12 queries 5 writes
- 文件加载 : 36
- 缓存信息 : 0 gets 0 writes
- 配置加载 : 130
- 会话信息 : SESSION_ID=657rjeenmnadvj8bmfhrchlr44
- /www/wwwroot/tsicrk.com/index.php ( 1.09 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/ThinkPHP.php ( 4.61 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Library/Think/Think.class.php ( 12.26 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Library/Think/Storage.class.php ( 1.37 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Library/Think/Storage/Driver/File.class.php ( 3.52 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Mode/common.php ( 2.82 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Common/functions.php ( 53.56 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Library/Think/Hook.class.php ( 4.01 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Library/Think/App.class.php ( 13.49 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Library/Think/Dispatcher.class.php ( 14.79 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Library/Think/Route.class.php ( 13.36 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Library/Think/Controller.class.php ( 11.23 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Library/Think/View.class.php ( 7.59 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Library/Behavior/BuildLiteBehavior.class.php ( 3.68 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Library/Behavior/ParseTemplateBehavior.class.php ( 3.88 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Library/Behavior/ContentReplaceBehavior.class.php ( 1.91 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Conf/convention.php ( 11.15 KB )
- /www/wwwroot/tsicrk.com/App/Common/Conf/config.php ( 2.14 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Lang/zh-cn.php ( 2.55 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Conf/debug.php ( 1.49 KB )
- /www/wwwroot/tsicrk.com/App/Home/Conf/config.php ( 0.31 KB )
- /www/wwwroot/tsicrk.com/App/Home/Common/function.php ( 3.33 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Library/Behavior/ReadHtmlCacheBehavior.class.php ( 5.62 KB )
- /www/wwwroot/tsicrk.com/App/Home/Controller/ArticleController.class.php ( 6.02 KB )
- /www/wwwroot/tsicrk.com/App/Home/Controller/CommController.class.php ( 1.60 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Library/Think/Model.class.php ( 60.11 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Library/Think/Db.class.php ( 32.43 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Library/Think/Db/Driver/Pdo.class.php ( 16.74 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Library/Think/Cache.class.php ( 3.83 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Library/Think/Cache/Driver/File.class.php ( 5.87 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Library/Think/Template.class.php ( 28.16 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Library/Think/Template/TagLib/Cx.class.php ( 22.40 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Library/Think/Template/TagLib.class.php ( 9.16 KB )
- /www/wwwroot/tsicrk.com/App/Runtime/Cache/Home/7540f392f42b28b481b30614275e4e55.php ( 17.71 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Library/Behavior/WriteHtmlCacheBehavior.class.php ( 0.97 KB )
- /www/wwwroot/tsicrk.com/ThinkPHP/Library/Behavior/ShowPageTraceBehavior.class.php ( 5.24 KB )
- [ app_init ] --START--
- Run Behavior\BuildLiteBehavior [ RunTime:0.000005s ]
- [ app_init ] --END-- [ RunTime:0.000030s ]
- [ app_begin ] --START--
- Run Behavior\ReadHtmlCacheBehavior [ RunTime:0.000289s ]
- [ app_begin ] --END-- [ RunTime:0.000313s ]
- [ view_parse ] --START--
- [ template_filter ] --START--
- Run Behavior\ContentReplaceBehavior [ RunTime:0.000068s ]
- [ template_filter ] --END-- [ RunTime:0.000089s ]
- Run Behavior\ParseTemplateBehavior [ RunTime:0.006702s ]
- [ view_parse ] --END-- [ RunTime:0.006732s ]
- [ view_filter ] --START--
- Run Behavior\WriteHtmlCacheBehavior [ RunTime:0.000157s ]
- [ view_filter ] --END-- [ RunTime:0.000172s ]
- [ app_end ] --START--
- 1064:You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ') LIMIT 1' at line 1
[ SQL语句 ] : SELECT `id`,`pid`,`navname` FROM `cx_nav` WHERE ( id= ) LIMIT 1
- 1064:You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ') LIMIT 1' at line 1
[ SQL语句 ] : SELECT `id`,`navname` FROM `cx_nav` WHERE ( id= ) LIMIT 1
- 1064:You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ')' at line 1
[ SQL语句 ] : SELECT `id`,`navname` FROM `cx_nav` WHERE ( pid= )
- [8] Undefined index: pid /www/wwwroot/tsicrk.com/App/Home/Controller/ArticleController.class.php 第 47 行.
- [8] Undefined index: db_host /www/wwwroot/tsicrk.com/ThinkPHP/Library/Think/Db.class.php 第 120 行.
- [8] Undefined index: db_port /www/wwwroot/tsicrk.com/ThinkPHP/Library/Think/Db.class.php 第 121 行.
- [8] Undefined index: db_name /www/wwwroot/tsicrk.com/ThinkPHP/Library/Think/Db.class.php 第 122 行.

1.6396s
