vue:手机端适配

  • 下载lib-flexible

npm i lib-flexible --save

  • 在main.js中引入lib-flexible

import ‘lib-flexible/flexible’

  • 查看index.js有没有添加meta标签(这一步我省略了)
<meta name="viewport" content="width=device-width,initial-scale=1.0">
//注意这两个的区别,建议添加下面的meta,反正点击输入框,页面自动缩放
<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
  • 安装px2rem-loader

npm install px2rem-loader

  • 在build文件中找到util.js,将px2rem-loader添加到cssLoaders中
const cssLoader = {
    loader: 'css-loader',
    options: {
      minimize: process.env.NODE_ENV === 'production',
      sourceMap: options.sourceMap
    }
  }
  const px2remLoader = {
    loader: 'px2rem-loader',
    options: {
      //一般设置75
      remUnit: 35
    }
  }
  • 同时,在generateLoaders方法中添加px2remLoader
 // generate loader string to be used with extract text plugin

  function generateLoaders(loader, loaderOptions) {
    const loaders = options.usePostCSS ? [cssLoader, postcssLoader,px2remLoader] : [cssLoader,px2remLoader]

    if (loader) {
      loaders.push({
        loader: loader + '-loader',
        options: Object.assign({}, loaderOptions, {
          sourceMap: options.sourceMap
        })
      })
    }

    // Extract CSS when that option is specified
    // (which is the case during production build)
    if (options.extract) {
      return ExtractTextPlugin.extract({
        use: loaders,
        fallback: 'vue-style-loader'
      })
    } else {
      return ['vue-style-loader'].concat(loaders)
    }
  }
  • npm run dev重新运行

下载lib-flexible完之后会在vscode终端中出现以下两个报错信息:

  1. ajv-keywords@3.5.2 requires a peer of ajv@^6.9.1 but none is
    installed. You must install peer dependencies yourself。
    翻译:
    ajv关键字@3.5.2需要ajv@^6.9.1对等机,但未安装。您必须自己安装对等依赖项
    解决办法:
    运行npm i ajv之后再下载lib-flexible

    npm i ajv

2.SKIPPING OPTIONAL DEPENDENCY: fsevents@2.1.3 (node_modules\fsevents):
Unsupported platform for fsevents@2.1.3: wanted {“os”:“darwin”,“arch”:“any”} (current: {“os”:“win32”,“arch”:“x64”})
翻译:
跳过可选依赖项:fsevents@2.1.3版(节点\u模块\fsevents):
不支持的平台fsevents@2.1.3版:需要{“os”:“darwin”,“arch”:“any”}(当前:{“os”:“win32”,“arch”:“x64”})
解决办法:
这是warning错误,是因为mac下需要 fsevents,这里是在windows环境,所以可以忽略这个错误。

©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页