vue 实现PC端适配 lib |
您所在的位置:网站首页 › vue项目pc端适配 › vue 实现PC端适配 lib |
依赖
项目基础配置使用 vue-cli 生成 自适应方案核心: 阿里可伸缩布局方案 lib-flexible px转rem:px2rem,它有webpack的loader px2rem 开始先使用vue脚手架初始化一个项目 vue create 项目名项目初始化好了之后,进入项目目录中 (cd 项目名) 安装 lib-flexible 和 px2rem-loader npm i lib-flexible -S npm i px2rem-loader -D安装好了之后还需要在项目的入口文件 main.js 里引入 lib-flexible // main.js import 'lib-flexible'接下来为了验证 lib-flexible 是否生效,可以将app.vue中的内容改成: * { margin: 0; padding: 0; } .wrapper div { height: 1rem; } .box1 { width: 2rem; background-color: coral; } .box2 { width: 4rem; background-color: skyblue; } .box3 { width: 6rem; background-color: palegreen; } .box4 { width: 8rem; background-color: wheat; } .box5 { width: 10rem; background-color: darkred; }
在 vue.config.js(没有的话就新建一个)中配置如下: module.exports = { chainWebpack: config => { config.module .rule("css") .test(/\.css$/) .oneOf("vue") .resourceQuery(/\?vue/) .use("px2rem") .loader("px2rem-loader") .options({ remUnit: 75 }); } };remUnit的值请自行修改。 修改配置以后 重启服务器,将原来app.vue文件中的样式改成: .wrapper div { height: 1rem; } .box1 { width: 150px; background-color: coral; } .box2 { width: 300px; background-color: skyblue; } .box3 { width: 450px; background-color: palegreen; } .box4 { width: 600px; background-color: wheat; } .box5 { width: 750px; background-color: darkred; }但是有一个问题,我明明设置的宽度是按1920来的,为什么计算出来1rem=54px?
找到node_modules下的lib-flexible文件夹 现在再重启项目,看一下运行结果: |
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |