1、Webpack 搭建 Vue.js 項目
-
安裝 node 環境
node.js 官網下載地址:https://nodejs.org/zh-cn/
-
安裝 Vue
npm install vue
復制
npm install --global vue-cli
復制
npm install --global @vue/cli-init
復制
不安裝 vue/cli-init 執行 vue init webpack webpack-vue-demo 創建 webpack 項目,報錯如下:
Command vue init requires a global addon to be installed.
Please run yarn global add @vue/cli-init
npm install --global webpack
復制
npm install --global webpack-dev-server
復制
vue init webpack webpack-vue-demo
復制
cd webpack-vue-demo
npm run dev
復制
-
瀏覽器訪問:http://localhost:8080,即可打開搭建的 Vue.js 項目。
-
項目目錄結構如下:

2、不同 PC 端屏幕適配
npm install lib-flexible --save
復制
npm install px2rem-loader --save
復制
-
在項目入口文件 main.js 中引入 lib-flexible
import 'lib-flexible'
復制
-
在 build/utils.js 中添加如下代碼:
exports.cssLoaders = function (options) {
options = options || {}
const cssLoader = {
loader: 'css-loader',
options: {
sourceMap: options.sourceMap
}
}
const px2remLoader = { // 添加的代碼
loader: 'px2rem-loader',
options: {
remUnit: 60
}
}
const postcssLoader = {
loader: 'postcss-loader',
options: {
sourceMap: options.sourceMap
}
}
復制
-
將 px2remLoader 放入 loaders 數組中
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
})
})
}
復制
3、解決 webpack-dev-server 無法自動編譯熱更新
-
WebStorm 中關閉 safe write(安全寫入)功能:

4、配置中文標題
如果項目每個頁面的標題不同,在 router/index.js 中配置:
import Vue from 'vue'
import Router from 'vue-router'
import Login from '@/views/Login'
import Index from '@/views/Index'
import {notification} from "ant-design-vue";
Vue.use(Router)
const router = new Router({
mode: 'history',
routes: [
{
path: '/',
redirect: '/index'
},
{
path: '/login',
name: 'Login',
component: Login,
meta: {
title: '登錄'
}
},
{
path: '/index',
name: 'Index',
component: Index,
meta: {
title: '首頁'
}
}
]
})
router.beforeEach((to, from, next) => {
if (to.meta.title) {
document.title = to.meta.title
}
if (to.path == '/login' || localStorage.getItem("token")) {
next()
} else {
notification.open({
message: '登錄提示',
description: '您還未登錄或Token已過期,請重新登錄'
})
return next("/login")
}
})
export default router
復制
如果項目每個頁面的標題都相同,則在項目入口頁面 index.html 中設置 title 為項目標題:
## index.html
信息管理平臺
全免费A级毛片免费看无码,亚洲色大成网站WWW久久九九,永久三级网站在线观看,95Pao国产成视频免费
<蜘蛛词>|
<蜘蛛词>|
<蜘蛛词>|
<蜘蛛词>|
<蜘蛛词>|
<蜘蛛词>|
<蜘蛛词>|
<蜘蛛词>|
<蜘蛛词>|
<蜘蛛词>|
<蜘蛛词>|
<蜘蛛词>|
<蜘蛛词>|
<蜘蛛词>|
<蜘蛛词>|
<蜘蛛词>|
<蜘蛛词>|
<蜘蛛词>|
<蜘蛛词>|
<蜘蛛词>|
<蜘蛛词>|
<蜘蛛词>|
<蜘蛛词>|
<蜘蛛词>|
<蜘蛛词>|
<蜘蛛词>|
<蜘蛛词>|
<蜘蛛词>|
<蜘蛛词>|
<蜘蛛词>|
<蜘蛛词>|
<蜘蛛词>|
<蜘蛛词>|
<蜘蛛词>|
<蜘蛛词>|
<蜘蛛词>|
<蜘蛛词>|
<蜘蛛词>|
<蜘蛛词>|
<蜘蛛词>|
<蜘蛛词>|
<文本链>
<文本链>
<文本链>
<文本链>
<文本链>
<文本链>
復制5、代碼示例
## module1.js
import module2 from "./module2.js"
export default {
f1: function () {
console.log(1);
},
obj: {
a: 123,
b: mode2
}
}
復制
## module2.js
export default 4554;
復制
## module3.js
/*export function f2() {
console.log('f2')
}
export function f3() {
console.log('f3')
}
export function f4() {
console.log('f4')
}*/
function class1() {
}
class1.prototype.f2 = function () {
console.log('f2')
}
class1.prototype.f3 = function () {
console.log('f3')
}
class1.prototype.f4 = function () {
console.log('f4')
}
module.exports = class1
復制
## app.js
import mode1 from "./module/mode1.js"
import class1 from "./module/module3.js"
import $ from "jquery";
import icon from "./img/icon.png"
import d from "./img/d.png"
import ewm from "./img/ewm.jpg"
$.extend({ a: 1 })
var iconImage = new Image();
iconImage.src = icon
var dImage = new Image();
dImage.src = d
var ewmImage = new Image();
ewmImage.src = ewm
class1.f2();
import "./css/test.css"
import "./css/test1.less"
const a = 444;
var b = 999;
console.log(a);
setInterval(() => {
console.log(b++)
}, 500)
mode1.f1();
console.log(mode1.obj.b)
復制
## app2.js
import mode1 from "./module/mode1.js"
import _ from "lodash"
_.add(123, 10);
mode1.f1();
console.log(mode1.obj.b)
復制
## package.json
{
"name": "code",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error:a no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"@babel/core": "^7.14.6",
"@babel/preset-env": "^7.14.7",
"babel-loader": "^8.2.2",
"css-loader": "^6.1.0",
"css-minimizer-webpack-plugin": "^3.0.2",
"file-loader": "^6.2.0",
"html-webpack-plugin": "^5.3.2",
"image-webpack-loader": "^7.0.1",
"imagemin": "^8.0.0",
"imagemin-gifsicle": "^7.0.0",
"imagemin-mozjpeg": "^9.0.0",
"imagemin-pngquant": "^9.0.2",
"img-loader": "^4.0.0",
"jquery": "^3.6.0",
"less": "^4.1.1",
"less-loader": "^10.0.1",
"lodash": "^4.17.21",
"mini-css-extract-plugin": "^2.1.0",
"style-loader": "^3.1.0",
"url-loader": "^4.1.1",
"webpack": "^5.45.1",
"webpack-cli": "^4.7.2",
"webpack-dev-server": "^3.11.2"
},
"devDependencies": {
"extract-text-webpack-plugin": "^4.0.0-beta.0"
}
}
復制
## webpack.config.js
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const webpack = require('webpack');
const HtmlWebpackPlugin = require('html-webpack-plugin')
const cssMinimizerWebpackPlugin = require("css-minimizer-webpack-plugin");
// optimize-css-assets-webpack-plugin 舊版webpack使用的CSS壓縮工具
module.exports = {
mode: "development",
entry: {
app: "./app.js",
app2: "./app2.js"
},
optimization: {
splitChunks: {
// async表示只從異步加載得模塊(動態加載import())里面進行拆分,initial表示只從入口模塊進行拆分,all表示以上兩者都包括
chunks: 'all',
minSize: 0,
cacheGroups: {
vendors: {
test: /[\\/]node_modules[\\/]/,
priority: -10
}
}
}
},
output: {
path: __dirname + "/dist/",
filename: "[name].js"
},
devServer: {
port: 3000,
hot: true,
hotOnly: true,
proxy: {
'/api': 'http://localhost:3000',
},
/*proxy: {
'/api': {
target: 'http://localhost:3000',
pathRewrite: { '^/api': '' },
},
},*/
},
module: {
rules: [
/* {
test: /\.css$/,
use: extractPlugin.extract(
{
fallback: {
loader: "style-loader"
},
use: [
{
loader: "css-loader"
}
]
}
)
}*/
{
test: /\.js$/,
use: [{
loader: "babel-loader",
options: {
presets: [
[
"@babel/preset-env", {
targets: {
browsers: [">1%", "ie>=7"]
}
}
]
],
plugins: []
}
}]
},
{
test: /\.css$/,
use: [MiniCssExtractPlugin.loader, "css-loader"],
},
{
test: /\.less$/,
use: [
MiniCssExtractPlugin.loader,
"css-loader",
"less-loader"
]
},
{
test: /\.(jpg|png|jpeg|gif)$/,
use: [{
loader: 'url-loader',
options: {
limit: 5000,
name: '[name]-[hash:10].[ext]',
}
},
{
loader: 'image-webpack-loader',
options: {
mozjpeg: {
quality: 65
},
pngquant: {
speed: 4
},
}
},
{
loader: "img-loader",
options: {
plugins: [
require("imagemin-pngquant")({
// 壓縮 png 插件,取值范圍 1-11,值越大壓縮率越小,值越小壓縮生成的文件越小,默認為4
speed: 4,
}),
require("imagemin-mozjpeg")({
// 壓縮 jpg 插件,取值范圍1-100,值越大壓縮率越小,值越小壓縮生成的文件越小
quality: 50,
}),
],
},
},
]
},
]
},
plugins: [
new MiniCssExtractPlugin({
filename: "test.css"
}),
new HtmlWebpackPlugin({
template: "./index.html",
filename: "index.html",
minify: {
removeComments: true,
collapseWhitespace: true,
removeAttributeQuotes: true
}
}),
new webpack.DllReferencePlugin({
manifest: require("./dll/vendor-manifest.json")
}),
]
}
復制
## webpack.dll.js
const webpack = require('webpack');
module.exports = {
entry: {
vendor: ['jquery', 'lodash'],
},
output: {
path: __dirname + "/dll",
filename: '[name].dll.js',
library: '[name]_library'
},
plugins: [
new webpack.DllPlugin({
path: __dirname + "/dll/[name]-manifest.json",
name: '[name]_library'
})
]
}
復制