VUE : vue-cli中去掉路由中的井號(hào)#操作
vue-cli項(xiàng)目中,如果想去掉url地址欄中的“#”我們可以用路由的 history 模式,這種模式充分利用 history.pushState API 來(lái)完成 URL 跳轉(zhuǎn)而無(wú)須重新加載頁(yè)面。
只需要在路由表中,加入一行代碼即可。


補(bǔ)充知識(shí):vue 打包部署去掉#注意事項(xiàng)
示例基于vue cli2.0腳手架生成的項(xiàng)目
1.vue項(xiàng)目中config文件下index.js中打包配置:
build: {
// Template for index.html
index: path.resolve(__dirname, '../dist/index.html'),
// Paths
assetsRoot: path.resolve(__dirname, '../dist'),
assetsSubDirectory: 'static',
assetsPublicPath: '/dist/', // 這個(gè)地方使用絕對(duì)路徑 很重要!
/**
* Source Maps
*/
productionSourceMap: true,
// https://webpack.js.org/configuration/devtool/#production
devtool: '#source-map',
// Gzip off by default as many popular static hosts such as
// Surge or Netlify already gzip all static assets for you.
// Before setting to `true`, make sure to:
// npm install --save-dev compression-webpack-plugin
productionGzip: false,
productionGzipExtensions: ['js', 'css'],
// Run the build command with an extra argument to
// View the bundle analyzer report after build finishes:
// `npm run build --report`
// Set to `true` or `false` to always turn it on or off
bundleAnalyzerReport: process.env.npm_config_report
}
2.路由配置:router文件夾下index.js:
export default new Router({
mode: 'history', // 去掉#,需要路由模式改為history
base: '/dist/', // 這個(gè)配置也很重要,否則會(huì)出現(xiàn)頁(yè)面空白情況
scrollBehavior: () => ({ y: 0 }),
routes: [
{
path: '/facebook',
name: 'Facebook',
component: Facebook
},
{
path: '/google',
name: 'Google',
component: Google
}
]
})
3.剩下的不懂 就要找你得運(yùn)維或者后端開(kāi)發(fā)小伙伴了,nginx配置:
server {
listen 8080;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
#打包后的項(xiàng)目目錄,一定記住這個(gè)地方帶有項(xiàng)目名稱(chēng)
root /Users/qiilee/Desktop/vue/dist;
index index.html;
location /dist {
#這個(gè)地方?jīng)]有項(xiàng)目名稱(chēng),因?yàn)檎?qǐng)求的時(shí)候如果請(qǐng)求:http://localhost:8080/dist ,nginx會(huì)查找/Users/qiilee/Desktop/vue/dist/目錄下的數(shù)據(jù)
root /Users/qiilee/Desktop/vue;
try_files $uri $uri/ @router;
index index.html;
}
//try_files $uri $uri/ @router;和下邊部分很重要,沒(méi)有這部分發(fā)布二級(jí)一下的路由會(huì)出現(xiàn)js加載,但是也沒(méi)空白的情況
location @router {
rewrite ^.*$ /index.html last;
}
}
以上這篇VUE : vue-cli中去掉路由中的井號(hào)#操作就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
vue 中url 鏈接左邊的小圖標(biāo)更改問(wèn)題
這篇文章主要介紹了vue 中url 鏈接左邊的小圖標(biāo)更改問(wèn)題,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-12-12
vue + canvas實(shí)現(xiàn)涂鴉面板的示例代碼
這篇文章主要給大家介紹了vue + canvas實(shí)現(xiàn)涂鴉面板的示例,文章通過(guò)代碼示例介紹的非常詳細(xì),感興趣的小伙伴跟著小編一起來(lái)看看吧2023-08-08
vue3.0 CLI - 2.4 - 新組件 Forms.vue 中學(xué)習(xí)表單
這篇文章主要介紹了vue3.0 CLI - 2.4 - 新組件 Forms.vue 中學(xué)習(xí)表單的相關(guān)知識(shí),本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值 ,需要的朋友可以參考下2018-09-09
詳解Vue如何實(shí)現(xiàn)顏色選擇與調(diào)色板功能
顏色選擇和調(diào)色板是Web開(kāi)發(fā)中常用的功能,Vue作為一個(gè)流行的JavaScript框架,可以方便地實(shí)現(xiàn)顏色選擇和調(diào)色板功能,本文講講如何在Vue中進(jìn)行顏色選擇和調(diào)色板吧2023-06-06
Vue3實(shí)現(xiàn)Message消息組件示例
在大多數(shù) web 產(chǎn)品中,全局的 Message 組件占有較大的使用場(chǎng)景,本文主要介紹了Vue3實(shí)現(xiàn)Message消息組件示例,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-06-06

