使用 Vant Weapp

Taro3 支持使用 Vant Weapp 组件库进行开发,示例仓库:taro3-vant-sample

注意:使用原生第三方组件库进行开发的项目,不再具有多端转换的能力

如何使用

配置忽略 vant 的样式尺寸转换

如果直接使用 vant 组件,会发现样式偏小的情况,这是因为 Taro 默认将 vant 的样式尺寸从 px 转换为了 rpx,可以通过配置 selectorblacklist 来禁止转换。

配置方法:

  1. // config/index.js
  2. const config = {
  3. // ...
  4. mini: {
  5. postcss: {
  6. pxtransform: {
  7. enable: true,
  8. config: {
  9. selectorBlackList: [/van-/]
  10. }
  11. }
  12. }
  13. },
  14. }

配置 copy 小程序原生文件

vant 组件中包含一些小程序原生文件的依赖,目前 Taro 没有对这些依赖进行分析。因此需要配置 copy 把这些依赖移动到 dist 目录中,例如需要 copy wxs 和样式文件,部分配置如下

  1. // config/index.js
  2. const config = {
  3. // ...
  4. copy: {
  5. patterns: [
  6. { from: 'src/components/vant-weapp/dist/wxs', to: 'dist/components/vant-weapp/dist/wxs' },
  7. { from: 'src/components/vant-weapp/dist/common/style', to: 'dist/components/vant-weapp/dist/common/style' },
  8. { from: 'src/components/vant-weapp/dist/common/index.wxss', to: 'dist/components/vant-weapp/dist/common/index.wxss' },
  9. { from: 'src/components/vant-weapp/dist/calendar/index.wxs', to: 'dist/components/vant-weapp/dist/calendar/index.wxs' },
  10. { from: 'src/components/vant-weapp/dist/calendar/utils.wxs', to: 'dist/components/vant-weapp/dist/calendar/utils.wxs' },
  11. { from: 'src/components/vant-weapp/dist/calendar/calendar.wxml', to: 'dist/components/vant-weapp/dist/calendar/calendar.wxml' },
  12. { from: 'src/components/vant-weapp/dist/calendar/components/month/index.wxs', to: 'dist/components/vant-weapp/dist/calendar/components/month/index.wxs' },
  13. ],
  14. options: {
  15. }
  16. },
  17. }

引用 vant 组件

首先需要在 app 的 config页面的 config 文件中配置 usingComponents 字段,如:

  1. export default {
  2. navigationBarTitleText: '首页',
  3. usingComponents: {
  4. 'van-button': '../../components/vant-weapp/dist/button/index'
  5. }
  6. }

然后在页面中便可以直接使用。

使用 vant 组件

1. 绑定属性

  • 使用 Vant Weapp - 图1React
  • 使用 Vant Weapp - 图2Vue
  1. import React, { Component } from 'react'
  2. import { View } from '@tarojs/components'
  3. export default class Index extends Component {
  4. render () {
  5. return (
  6. <View>
  7. <van-button type='primary' loading loading-text='ing'>Button</van-button>
  8. </View>
  9. )
  10. }
  11. }
  1. <template>
  2. <view>
  3. <van-button type='primary' :loading='true' loading-text='ing'>Button</van-button>
  4. </view>
  5. </template>
  6. <script>
  7. export default {
  8. name: 'index'
  9. }
  10. </script>

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

2. 绑定事件

  • 使用 Vant Weapp - 图3React
  • 使用 Vant Weapp - 图4Vue
  1. import React, { Component } from 'react'
  2. import { View } from '@tarojs/components'
  3. export default class Index extends Component {
  4. handleClick = () => {
  5. console.log('click')
  6. }
  7. onAfterRead = res => {
  8. console.log(res)
  9. }
  10. render () {
  11. return (
  12. <View>
  13. // 对应 bind:click 事件
  14. <van-button type='primary' onClick={this.handleClick} >Button</van-button>
  15. // 对应 bind:after-read 事件
  16. <van-uploader fileList={[]} onAfterRead={this.onAfterRead} />
  17. </View>
  18. )
  19. }
  20. }
  1. <template>
  2. <view>
  3. <van-button type='primary' @click='handleClick'>Button</van-button>
  4. <van-uploader :fileList='[]' @after-read='onAfterRead' />
  5. </view>
  6. </template>
  7. <script>
  8. export default {
  9. methods: {
  10. handleClick () {
  11. console.log('click')
  12. },
  13. onAfterRead (res) {
  14. console.log(res)
  15. }
  16. }
  17. }
  18. </script>

3. Slot

  • 使用 Vant Weapp - 图5React
  • 使用 Vant Weapp - 图6Vue
  1. import React, { Component } from 'react'
  2. import { View, Slot } from '@tarojs/components'
  3. export default class Index extends Component {
  4. render () {
  5. return (
  6. <View>
  7. <van-calendar poppable show>
  8. <Slot name='title'>
  9. <View>Hello world</View>
  10. </Slot>
  11. </van-calendar>
  12. </View>
  13. )
  14. }
  15. }
  1. <template>
  2. <view>
  3. <van-calendar :poppable='true' :show='true'>
  4. <slot-view :name='"title"'>
  5. <view>Hello world</view>
  6. </slot-view>
  7. </van-calendar>
  8. </view>
  9. </template>
  10. <script>
  11. export default {
  12. name: 'index'
  13. }
  14. </script>