Taro(V2.0.5)中引入Vant Weapp,(需下载资源到项目中:大小约322KB)
需如下几步:
1.在/src/components下新建文件夹vant-weapp
2.在github上找到Vant-weapp下载文件包,将对应的dist文件夹下内容
复制到新建的vant-weapp文件夹
下。
(下载vant-weapp方式,编辑器终端输入:git clone https://github.com/youzan/vant-weapp.git )
3.在Pages或components
对应文件的config.usingComponents
中,配置每个页面所需要的组件。(注:无法在app.js
中进行全局注册组件。)
例:
config = {
usingComponents: {
"van-icon": "../../components/vant-weapp/icon/index"
}
}
4.在使用Vant-weapp
组件后,taro
会自动将相应的组件复制一份到dist/components
下,而Vant-weapp
的组件还依赖工具库/src/components/vant-weapp/wxs
,该工具库taro
不会自动复制到dist
中。所以,我们需要在/config/index.js
文件下的config配置中新增copy.patterns
,让其在编译时,自动复制到dist
对应目录下。
copy: {
patterns: [
{
from: 'src/components/vant-weapp/wxs/',
to: 'dist/components/vant-weapp/wxs/'
}
],
options: {}
}
5.对Taro进行重新编译(例:npm run build:weapp)
*由于Vant-weapp
的样式使用的单位是px
,所以会被taro
编译成rpx。
可以通过修改/config/index.js
文件中的config.
mini.
postcss.pxtransform.selectorBlackList
不让其单位转换。
pxtransform: {
enable: true,
config: {},
selectorBlackList: [
/^.van-.*?$/,
]
}
注:Taro
编译器无法自动将用到组件的.wxs
文件移动到/dist
相应目录下(**开发者工具会报错**)。
需在config配置中新增copy.patterns
配置
例子:
copy: {
patterns: [
{
from: 'src/components/vant-weapp/wxs/',
to: 'dist/components/vant-weapp/wxs/'
},
{
from: 'src/components/vant-weapp/sticky',
to: 'dist/components/vant-weapp/sticky'
},
{
from: 'src/components/vant-weapp/tabs',
to: 'dist/components/vant-weapp/tabs'
}
],
options: {}
}