vue-cli2 版本中默认使用了 ExtractTextPlugin 插件,来合并各个 vue 文件的 css。
build/utils.js:
if (options.extract) {
return ExtractTextPlugin.extract({
use: loaders,
publicPath: "../../", // 注意配置这一部分,根据目录结构自由调整
fallback: "vue-style-loader"
});
} else {
return ["vue-style-loader"].concat(loaders);
}
webpack.base.conf.js
plugins: [
new ExtractTextPlugin({filename: "main.css", allChunks: true}), //抽离成一个单独的css
new webpack.ProvidePlugin({
$: "jquery",
jQuery: "jquery",
"windows.jQuery": "jquery"
})
],
...
{
test: /\.vue$/,
loader: 'vue-loader',
options: {loaders:{
css: ExtractTextPlugin.extract({
use: 'css-loader',
fallback: 'vue-style-loader'
})
}}
},
方法一:绝对路径
将静态路径放在最外面的 static 文件夹中,使用绝对路径访问。如:img src="/static/images/xxx.png" 参考链接: link
方法二:相对路径 build/utils.js
if (options.extract) {
return ExtractTextPlugin.extract({
use: loaders,
publicPath: "../../", //此处修改 注意配置这一部分,根据目录结构自由调整
fallback: "vue-style-loader"
});
} else {
return ["vue-style-loader"].concat(loaders);
}
config/index.js
build: {
// Template for index.html
index: path.resolve(__dirname, '../dist/index.html'),
// Paths
assetsRoot: path.resolve(__dirname, '../dist'),
assetsSubDirectory: 'static',
assetsPublicPath: './',//此处修改
...
}
manifest.45b56c55bdc8c29560d3.js:1 Error: Loading chunk 22 failed.
at HTMLScriptElement.t (manifest.45b56c55bdc8c29560d3.js:1)
在 index.html 的标签中加入 第一种方法:
<!DOCTYPE html>
<html>
<head>
<base href="/" /> <!-- 加上这行 -->
<!-- ... -->
</head>
<body>
<!-- ... -->
</body>
</html>
第二种方法:(未测试)
.webpackConfig({ output: { filename: '[name].js', chunkFilename: 'js/[name].app.js', publicPath: '/' } });
参考链接:laravel-mix/laravel-mix#164
webapck.prod.conf.js
原始:
new webpack.DefinePlugin({
'process.env': env,
$ : 'jquery',
jQuery : 'jquery'
}),
改为:
new webpack.ProvidePlugin({
$: "jquery",
jQuery: "jquery"
}),
<img src="/logo.png" :onerror="defaultImg">
data() {
return {
defaultImg: 'this.src="' + require('../../assets/img/timg.jpg') + '"'
}
}
https://juejin.im/post/5a115df9f265da432240caaf
vue-cli 中已经默认安装了 webpack-merge
import merge from 'webpack-merge';
修改原有参数
this.$router.push({
query:merge(this.$route.query,{'maxPrice':'630'})
})
新增一个参数:
this.$router.push({
query:merge(this.$route.query,{'addParams':'我是新增的一个参数,哈哈哈哈'})
})
替换所有参数:
this.$router.push({
query:merge({},{'maxPrice':'630'})
考虑使用 this.$forceUpdate()进行强制刷新。
先安装 promise.prototype.finally 包
npm i promise.prototype.finally
引入包
require('promise.prototype.finally').shim();
axios.get()
.then()
.catch()
.finally()
// 由于webpack已经配置了js和css的hash值,所以静态资源不会被缓存
// 所以将问题定位在index.html上
// 对html文件,返回时增加头:Cache-Control,必须每次来服务端校验,根据etag返回200或者304
服务器nginx配置如下:
location ~* \.(html)$ {
root /xxx/dist;
etag on;
expires 30d;
index index.html;
}
location ~* \.(css|js|jpg|jpeg|gif|png|ico|cur|gz|svg|svgz|map|mp4|ogg|ogv|webm|htc)$ {
root /xxx/dist;
index index.html;
expires 1M;
access_log off;
add_header Cache-Control "public";
}
https://juejin.im/post/5a115df9f265da432240caaf
通过路由的钩子函数beforeEach
和meta
进行控制其路由参数的keepAlive
。
参考连接:另辟蹊径:vue 单页面,多路由,前进刷新,后退不刷新
参考连接:阮一峰详解 cors 如何解决 axios 两次请求问题
注意全局安装完eslint
后,要安装eslint-plugin-html
对文件进行检查。如果是vue-cli
脚手架搭建,需要在devDependencies
里面再安装一个eslint-plugin-html
依赖。
1.嵌套的子路由保存父路由路径。
// 在子路由的path中,最前面不要加/。路由地址将拼接上父路由的地址,结果:localhost:8080/#/parent/child
{
path: '/parent',
component: () => import('@/pages/views/parent'),
children: [{
path: 'child',
name: 'child',
component: () => import('@/pages/views/child')
}]
}
2.重置路由的路径
// 在子路由的path中,前面加上/。路由地址将从根地址进行改写,结果:localhost:8080/#/child
{
path: '/parent',
component: () => import('@/pages/views/parent'),
children: [{
path: '/child',
name: 'child',
component: () => import('@/pages/views/child')
}]
}
使用beforeRouteUpdate
这个hook,可以帮助我们判断上一个路由的信息,并且在当前hook中,可以直接调用vue
实例。
示例:
beforeRouteUpdate(to, from) {
if (to.path === from.path && to.query !== from.query) {
this.init();
}
}
mounted() {
this.init();
}