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: {}
}