vue cli3 创建vw移动端适配项目
一直用rem做移动端的适配,其原理就是根据html的font-size为100单位rem的大小,flexible的解决方案大致是根据设计稿的大小动态改变不同屏幕下html的font-size的值,得到一个设计稿和rem的转化比例。具体实现可以看一下大漠老师的《使用Flexible实现手淘H5页面的终端适配》这篇文章之后在vw的兼容性得到解决,大漠老师又出了新的移动端适配方案并和vue cli2..
一直用rem做移动端的适配,其原理就是根据html的font-size为100单位rem的大小,flexible的解决方案大致是根据设计稿的大小动态改变不同屏幕下html的font-size的值,得到一个设计稿和rem的转化比例。具体实现可以看一下大漠老师的《使用Flexible实现手淘H5页面的终端适配》这篇文章
之后在vw的兼容性得到解决,大漠老师又出了新的移动端适配方案并和vue cli2做了详细的安装步骤,具体请看原文章《如何在Vue项目中使用vw实现移动端适配》。
对于vue-cli3,官方对目录的改动还是比较大的。
cli3的config和build目录都不见了,让我怎么配置一些打包配置
网上搜了一下需要在根路径下建一个vue.config.js。基本配置设置如下
module.exports = {
baseUrl: process.env.NODE_ENV === 'production'
? './'
: '/',
outputDir: '../dist',
assetsDir: 'static',
filenameHashing: true,
// When building in multi-pages mode, the webpack config will contain different plugins
// (there will be multiple instances of html-webpack-plugin and preload-webpack-plugin).
// Make sure to run vue inspect if you are trying to modify the options for those plugins.
pages: {
index: {
// entry for the pages
entry: 'src/main.js',
// the source template
template: 'public/index.html',
// output as dist/index.html
filename: 'index.html',
// when using title option,
// template title tag needs to be <title><%= htmlWebpackPlugin.options.title %></title>
title: '首页',
// chunks to include on this pages, by default includes
// extracted common chunks and vendor chunks.
chunks: ['chunk-vendors', 'chunk-common', 'index']
}
// when using the entry-only string format,
// template is inferred to be `public/subpage.html`
// and falls back to `public/index.html` if not found.
// Output filename is inferred to be `subpage.html`.
// subpage: ''
},
// eslint-loader 是否在保存的时候检查
lintOnSave: true,
// 是否使用包含运行时编译器的Vue核心的构建
runtimeCompiler: false,
// 默认情况下 babel-loader 忽略其中的所有文件 node_modules
transpileDependencies: [],
// 生产环境 sourceMap
productionSourceMap: false,
// cors 相关 https://jakearchibald.com/2017/es-modules-in-browsers/#always-cors
// corsUseCredentials: false,
// webpack 配置,键值对象时会合并配置,为方法时会改写配置
// https://cli.vuejs.org/guide/webpack.html#simple-configuration
configureWebpack: (config) => {
},
// webpack 链接 API,用于生成和修改 webapck 配置
// https://github.com/mozilla-neutrino/webpack-chain
chainWebpack: (config) => {
// 因为是多页面,所以取消 chunks,每个页面只对应一个单独的 JS / CSS
config.optimization
.splitChunks({
cacheGroups: {}
});
// 'src/lib' 目录下为外部库文件,不参与 eslint 检测
config.module
.rule('eslint')
.exclude
.add('/Users/maybexia/Downloads/FE/community_built-in/src/lib')
.end()
},
// 配置高于chainWebpack中关于 css loader 的配置
css: {
// 是否开启支持 foo.module.css 样式
modules: false,
// 是否使用 css 分离插件 ExtractTextPlugin,采用独立样式文件载入,不采用 <style> 方式内联至 html 文件中
extract: true,
// 是否构建样式地图,false 将提高构建速度
sourceMap: false,
// css预设器配置项
loaderOptions: {
css: {
// options here will be passed to css-loader
},
postcss: {
// options here will be passed to postcss-loader
}
}
},
// All options for webpack-dev-server are supported
// https://webpack.js.org/configuration/dev-server/
devServer: {
open: true,
host: '127.0.0.1',
port: 3000,
https: false,
hotOnly: false,
proxy: null,
before: app => {
}
},
// 构建时开启多进程处理 babel 编译
parallel: require('os').cpus().length > 1,
// https://github.com/vuejs/vue-cli/tree/dev/packages/%40vue/cli-plugin-pwa
pwa: {},
// 第三方插件配置
pluginOptions: {}
};
基本配置也就不说明,现在可以npm run serve 启动项目了。
完成启动接下来就是配置vw布局了
1.安装需要的插件(由于cli3的一些配置有过修改,npm 的库也相应多了)
npm i postcss-aspect-ratio-mini postcss-px-to-viewport postcss-write-svg postcss-cssnext postcss-viewport-units cssnano postcss-import postcss-url --S
2.在根路径下的postcss.config.js配置如下
module.exports = {
"plugins": {
"postcss-import": {},
"postcss-url": {},
"postcss-aspect-ratio-mini": {},
"postcss-write-svg": {
utf8: false
},
"postcss-cssnext": {},
"postcss-px-to-viewport": {
viewportWidth: 750, // (Number) The width of the viewport.
viewportHeight: 1334, // (Number) The height of the viewport.
unitPrecision: 3, // (Number) The decimal numbers to allow the REM units to grow to.
viewportUnit: 'vw', // (String) Expected units.
selectorBlackList: ['.ignore', '.hairlines'], // (Array) The selectors to ignore and leave as px.
minPixelValue: 1, // (Number) Set the minimum pixel value to replace.
mediaQuery: false // (Boolean) Allow px to be converted in media queries.
},
"postcss-viewport-units":{},
"cssnano": {
preset: "advanced",
autoprefixer: false,
"postcss-zindex": false
}
}
}
3.启动npm run serve会发现报 Cannot load preset "advanced". Please check your configuration for errors and try again错
这是就需要在安装一个cssnano-preset-advanced插件
npm i cssnano-preset-advanced --save-dev
4.再次启动,所有的css的px都变为vw单位
5.引入兼容性方案
①引入viewport-units-buggyfill.js
和viewport-units-buggyfill.hacks.js
两个js https://github.com/rodneyrehm/viewport-units-buggyfill
②调用
<script>
window.onload = function () {
window.viewportUnitsBuggyfill.init({
hacks: window.viewportUnitsBuggyfillHacks
});
}
</script>
cli3的vw配置到此也就结束了。如果在项目中仍然需要使用px可以加class为.ignore
或.hairlines
(.hairlines
一般用于设置border-width:0.5px
的元素中) 还有更多内容 还是看原文《如何在Vue项目中使用vw实现移动端适配》。
现在大漠老师的文章要收费了?,给你们个网友转载的链接 https://www.jianshu.com/p/1f1b23f8348f
更多推荐
所有评论(0)