Taro引入vant Weapp的方法

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: {}
}
发布了2 篇原创文章 · 获赞 11 · 访问量 195

猜你喜欢

转载自blog.csdn.net/qq_42698109/article/details/104637897