vue.js的項(xiàng)目的訪問鏈接如何去掉 # 呢献联?
步驟如下:
1. 在路由定義的腳本里添加mode: 'history'
const router = new VueRouter({
mode: 'history',
routes: [...]
})
2.以上步驟還不夠,因?yàn)轫撁嫠⑿聲霈F(xiàn)404錯誤琅翻,還需要配置服務(wù)器
Apache
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.html [L]
</IfModule>
nginx
location / {
try_files $uri $uri/ /index.html;
}
注:本地測試環(huán)境運(yùn)行npm run dev時是不用配置的悴能,但部署到服務(wù)器上是需要的携悯。