vue新手,刚搭建了一个vue项目,记录下使用的相关小知识点,方便自己后期使用
1. build构建hash文件名修改
默认的vue项目中,构建后的的文件名带有hash值,可将webpack.prod.conf.js、webpack.base.conf.js
文件中的hash值改成自定义值。
eg:
自定义函数(年-月-日)
function getVersion() {
let d = new Date()
return (d.getFullYear()) + '-' + (d.getMonth() + 1) + '-' + d.getDate()
}
css
// extract css into its own file
new ExtractTextPlugin({
// filename: utils.assetsPath('css/[name].[contenthash].css'),
filename: utils.assetsPath('css/[name].' + getVersion() + '.css'),
allChunks: true,
}),
fonts
{
test: /\.(woff2?|eot|ttf|otf)(\?.*)?$/,
loader: 'url-loader',
options: {
limit: 10000,
// name: utils.assetsPath('fonts/[name].[hash:7].[ext]')
name: utils.assetsPath('fonts/[name].' + getVersion() + '.[ext]')
}
},
2. vue-router HTML5 History 模式
vue-router 默认hash
模式 —— 使用 URL 的 hash 来模拟一个完整的 URL,于是当 URL 改变时,页面不会重新加载,但是该模式下URL会带#
前缀。如果想要URL不带#
,使用 history
模式。
eg:
export default new Router({
mode: 'history',
routes: [
{
path: '/',
name: 'HelloWorld',
component: HelloWorld
}
]
})
具体说明参考文档:HTML5 History 模式
3. 自定义上下文地址方法
自定义domain.js文件:GetContext
方法获取地址
export function GetContext() {
this["ctx"] = "http://localhost:8080";// 这里只是测试例子,可根据不同环境进行区分环境配置
}
src/main.js:将$ctx
绑定到全局
import {GetContext} from './domain'
let domain = new GetContext();
// $ctx绑定到全局
Vue.prototype.$ctx = domain.ctx
页面使用举例:
this.$axios.post(this.$ctx + '/login.do', {
userName: this.userName,
userPwd: this.userPwd
}).then(response => {
***
});