vue + vant 移動端

rem+less 配置自動rem換算

vant 安裝

# 通過 npm 安裝
npm i vant -S

postcss 配置,

module.exports = {
  plugins: {
    autoprefixer: {
      browsers: ['Android >= 4.0', 'iOS >= 8'],
    },
    'postcss-pxtorem': {
      rootValue: 37.5, // vant的根比例
      propList: ['*'],
    },
  },
};

01--vant組件的引入方式
main.js 全局導入所有組件

import 'vant/lib/index.css'

Vue.use(Vant)

按需使用vant組件

import {Row, Col} from 'vant' 
Vue.use(Row).use.(Col)

安裝babel-plugin-import,它可以讓我們按需引入組件模塊
.babelrc中配置plugins

"plugins": [
    "transform-vue-jsx", 
    "transform-runtime",
    ["import",{"libraryName":"vant","style":true}]
  ]

01--vant 主題配置
官網的給的案例

// vue.config.js
module.exports = {
  css: {
    loaderOptions: {
      less: {
        // 若使用 less-loader@5,請移除 lessOptions 這一級,直接配置選項。
        lessOptions: {
          modifyVars: {
            // 直接覆蓋變量
            'text-color': '#111',
            'border-color': '#eee',
            // 或者可以通過 less 文件覆蓋(文件路徑爲絕對路徑)
            hack: `true; @import "your-less-file-path.less";`,
          },
        },
      },
    },
  },
};

安照配置,照貓畫虎 vue.config.js

// const myTheme = path.resolve(__dirname, 'src/styles/style.less') // less 覆蓋
  css: {
    extract: IS_PROD,
    requireModuleExtension: true, // 去掉文件名中的 .module
    loaderOptions: {
      // 給 less-loader 傳遞 Less.js 相關選項
      less: {
        // `globalVars` 定義全局對象,可加入全局變量
        modifyVars: {
          // hack: `true; @import "${myTheme}";`
          // hack: `true; @import "your-less-file-path.less";`,
          hack: `true; @import "${resolve('src/styles/style.less')}";`
        }
      }
    }
  },

有小夥伴沒有配置vue.config.js添加內容 ,以下是沒有這個vue.config.js文件新建文件方式

// const path = require("path");
// const myTheme = path.resolve(__dirname, "src/assets/style/myTheme.less");
// module.exports = {
//     css: {
//         loaderOptions: {
//             less: {
//                 // 若使用 less-loader@5,請移除 lessOptions 這一級,直接配置選項。
//                 lessOptions: {
//                     modifyVars: {
//                         // 直接覆蓋變量
//                         'text-color': '#111',
//                         'border-color': '#eee',
//                         // 或者可以通過 less 文件覆蓋(文件路徑爲絕對路徑)
//                         hack: `true; @import "your-less-file-path.less";`,
//                     },
//                 },
//             },
//         },
//     },
//     pluginOptions: {
//         'style-resources-loader': {
//             preProcessor: 'less',
//             patterns: [
//                 path.resolve(__dirname, './src/styles/style.less'),
//             ],
//         },
//     },
// };

更新需要vue.config.js文件

// vue.config.js
const path = require('path')

const CompressionWebpackPlugin = require('compression-webpack-plugin') // 開啓gzip壓縮, 按需引用
const productionGzipExtensions = /\.(js|css|json|txt|html|ico|svg)(\?.*)?$/i // 開啓gzip壓縮, 按需寫入
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin // 打包分析
// const myTheme = path.resolve(__dirname, 'src/styles/style.less') // less 覆蓋

const IS_PROD = ['production', 'prod'].includes(process.env.NODE_ENV)
const resolve = (dir) => path.join(__dirname, dir)
module.exports = {
  publicPath: process.env.NODE_ENV === 'production' ? '/site/vue-demo/' : '/', // 公共路徑
  indexPath: 'index.html', // 相對於打包路徑index.html的路徑
  outputDir: process.env.outputDir || 'dist', // 'dist', 生產環境構建文件的目錄
  assetsDir: 'static', // 相對於outputDir的靜態資源(js、css、img、fonts)目錄
  lintOnSave: false, // 是否在開發環境下通過 eslint-loader 在每次保存時 lint 代碼
  runtimeCompiler: true, // 是否使用包含運行時編譯器的 Vue 構建版本
  productionSourceMap: !IS_PROD, // 生產環境的 source map
  parallel: require('os').cpus().length > 1, // 是否爲 Babel 或 TypeScript 使用 thread-loader。該選項在系統的 CPU 有多於一個內核時自動啓用,僅作用於生產構建。
  pwa: {}, // 向 PWA 插件傳遞選項。
  chainWebpack: config => {
    config.resolve.symlinks(true) // 修復熱更新失效
    // 如果使用多頁面打包,使用vue inspect --plugins查看html是否在結果數組中
    config.plugin('html').tap(args => {
      // 修復 Lazy loading routes Error
      args[0].chunksSortMode = 'none'
      return args
    })
    config.resolve.alias // 添加別名
      .set('@', resolve('src'))
      .set('@assets', resolve('src/assets'))
      .set('@components', resolve('src/components'))
      .set('@views', resolve('src/views'))
      .set('@store', resolve('src/store'))
    // 壓縮圖片
    // 需要 npm i -D image-webpack-loader
    config.module
      .rule('images')
      .use('image-webpack-loader')
      .loader('image-webpack-loader')
      .options({
        mozjpeg: { progressive: true, quality: 65 },
        optipng: { enabled: false },
        pngquant: { quality: [0.65, 0.9], speed: 4 },
        gifsicle: { interlaced: false },
        webp: { quality: 75 }
      })
    // 打包分析
    // 打包之後自動生成一個名叫report.html文件(可忽視)
    if (IS_PROD) {
      config.plugin('webpack-report').use(BundleAnalyzerPlugin, [
        {
          analyzerMode: 'static'
        }
      ])
    }
  },
  configureWebpack: config => {
    // 開啓 gzip 壓縮
    // 需要 npm i -D compression-webpack-plugin
    const plugins = []
    if (IS_PROD) {
      plugins.push(
        new CompressionWebpackPlugin({
          filename: '[path].gz[query]',
          algorithm: 'gzip',
          test: productionGzipExtensions,
          threshold: 10240,
          minRatio: 0.8
        })
      )
    }
    config.plugins = [...config.plugins, ...plugins]
  },
  css: {
    extract: IS_PROD,
    requireModuleExtension: true, // 去掉文件名中的 .module
    loaderOptions: {
      // 給 less-loader 傳遞 Less.js 相關選項
      less: {
        // `globalVars` 定義全局對象,可加入全局變量
        modifyVars: {
          // hack: `true; @import "${myTheme}";`
          // hack: `true; @import "your-less-file-path.less";`,
          hack: `true; @import "${resolve('src/styles/style.less')}";`
        }
      }
    }
  },
  pluginOptions: {
    'style-resources-loader': {
      preProcessor: 'less',
      patterns: [
        path.resolve(__dirname, './src/styles/MyStyle.less')
        // 'D:\\code\\我的倉庫\\blog\\src\\styles\\MyStyle.less'
      ]
    }
  },
  devServer: {
    overlay: { // 讓瀏覽器 overlay 同時顯示警告和錯誤
      warnings: true,
      errors: true
    },
    host: 'localhost',
    port: 8080, // 端口號
    https: false, // https:{type:Boolean}
    open: false, // 配置自動啓動瀏覽器
    hotOnly: true, // 熱更新
    // proxy: 'http://localhost:8080'   // 配置跨域處理,只有一個代理
    proxy: { // 配置多個跨域
      '/api': {
        target: 'http://172.11.11.11:7071',
        changeOrigin: true,
        // ws: true,//websocket支持
        secure: false,
        pathRewrite: {
          '^/api': '/'
        }
      },
      '/api2': {
        target: 'http://172.12.12.12:2018',
        changeOrigin: true,
        // ws: true,//websocket支持
        secure: false,
        pathRewrite: {
          '^/api2': '/'
        }
      }
    }
  }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章