vue-cli3 vant rem适配

1、首先我们要安装vant
yarn add vant

2、安装自动按需引入组件
yarn add babel-plugin-import --save

3、配置文件,在babel.config.js

module.exports = {
  presets: [
    '@vue/cli-plugin-babel/preset'
  ],
  plugins: [
    ['import', {
      libraryName: 'vant',
      libraryDirectory: 'es',
      style: true
    }, 'vant']
  ]
}

4、在组件中使用
<template>
  <div class="home">
    <van-button type="info">信息按钮</van-button>
 </div>
</template>

<script>
import { Button } from 'vant'
export default {
  name: 'Home',
  components: {
    [Button.name]: Button
  },
  created () {
    console.log(process.env.NODE_ENV)
  }
}
</script>

5、接着我们解决vant和rem的适配问题
Vant 中的样式默认使用px作为单位,如果需要使用rem单位,需要安装已下两个插件
postcss-pxtorem 是一款 postcss 插件,用于将单位转化为 rem
lib-flexible 用于设置 rem 基准值

yarn add postcss-pxtorem --save
yarn add amfe-flexible --save

安装完成之后需要全局配置
首先在main.js引入amfe-flexible
import 'amfe-flexible/index.js'


还要在vue.config.js中添加配置

// 先引入
const autoprefixer = require('autoprefixer')
const pxtorem = require('postcss-pxtorem')

在moudle.exports中添加一下配置
css: {
    loaderOptions: {
      postcss: {
        plugins: [
          autoprefixer({
            overrideBrowserslist: [
              'Android 4.1',
              'iOS 7.1',
              'Chrome > 31',
              'ff > 31',
              'ie >= 8'
            ]
          }),
          pxtorem({
            rootValue: 37.5,
            propList: ['*']
          })
        ]
      }
    }
  }



 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值