vue 实现响应式布局,移动pc端通用:amfe-flexible和postcss-pxtorem结合使用

1.amfe-flexible是配置可伸缩布局方案,主要是将1rem设为viewWidth/10。

2.postcss-pxtorem是postcss的插件,用于将像素单元生成rem单位。

3.安装amfe-flexible和postcss-pxtorem:

npm install amfe-flexible -S

npm install postcss-pxtorem -S

npm install amfe-flexible -S
npm install postcss-pxtorem -S

在main.js引入amfe-flexible

import 'amfe-flexible';

在vue.config.js添加配置postcss-pxtorem;(注意:可在vue.config.js、.postcssrc.js、postcss.config.js其中之一配置,权重从左到右降低,没有则新建文件,只需要设置其中一个即可)

module.exports = {
    css:{
       loaderOptions:{
          postcss:{
             plugins:{
                require('postcss-pxtorem')({
                   rootValue:192, // ui设计图/10,可以根据项目的UI图定义
                   propList:['*']
                })
     
  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在vite.config.ts中适配postcss-pxtoremamfe-flexible,需要进行如下配置: 1. 安装依赖 首先,需要安装postcss-pxtoremamfe-flexible这两个库,可以使用npm或yarn进行安装: ``` npm install postcss-pxtorem amfe-flexible --save-dev ``` ``` yarn add postcss-pxtorem amfe-flexible --dev ``` 2. 配置vite.config.ts 在vite.config.ts中,需要添加一个postcss插件,并将amfe-flexible作为其参数传入,同时也需要配置postcss-pxtorem插件,代码如下: ```typescript import { defineConfig } from 'vite'; import vue from '@vitejs/plugin-vue'; import postcssPxToRem from 'postcss-pxtorem'; export default defineConfig({ plugins: [ vue(), { name: 'postcss', async transform(css, id) { if (id.endsWith('.vue') || id.endsWith('.css')) { const { default: postcss } = await import('postcss'); const { default: flexible } = await import('amfe-flexible'); const processor = postcss([postcssPxToRem({ rootValue: 37.5, propList: ['*'], })]); const result = await processor.process(css, { from: id }); return { code: result.css, map: result.map, }; } }, enforce: 'post', }, ], }); ``` 这里使用了vite的postcss插件,该插件可以通过调用postcss的API来处理CSS文件。在transform函数中,首先判断当前文件的类型是否为.vue或.css,然后使用postcss-pxtorem将px转换成rem,其中rootValue为根元素的字体大小,propList为要转换的属性列表,这里设置为所有属性都转换。 同时,将amfe-flexible作为postcss插件的参数传入,这样就可以自适应不同屏幕尺寸。 需要注意的是,由于postcss插件是异步执行的,因此需要使用async/await来等待插件执行完成后再返回结果。 以上就是在vite.config.ts中适配postcss-pxtoremamfe-flexible的方法。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值