如何删除hashbang #!从url ?
我在vue路由器文档(http://vuejs.github.io/vue-router/en/options.html)中找到了禁用hashbang的选项,但这个选项删除了#!然后输入#
有没有办法有干净的url?
例子:
不是:# !/家庭
但是:/家庭
谢谢!
如何删除hashbang #!从url ?
我在vue路由器文档(http://vuejs.github.io/vue-router/en/options.html)中找到了禁用hashbang的选项,但这个选项删除了#!然后输入#
有没有办法有干净的url?
例子:
不是:# !/家庭
但是:/家庭
谢谢!
当前回答
对于vue.js 2,使用以下方法:
const router = new VueRouter({
mode: 'history'
})
其他回答
window.router = new VueRouter({
hashbang: false,
//abstract: true,
history: true,
mode: 'html5',
linkActiveClass: 'active',
transitionOnLoad: true,
root: '/'
});
服务器配置正确 在apache你应该写url重写
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.html [L]
</IfModule>
vue-router的默认模式是哈希模式——它使用URL哈希来模拟完整的URL,这样当URL更改时页面就不会被重新加载。 为了摆脱散列,我们可以使用路由器的历史模式,它利用了历史。pushState API实现URL导航,而不需要重新加载页面:
import {routes} from './routes'; //import the routes from routes.js
const router = new VueRouter({
routes,
mode: "history",
});
new Vue({
el: '#app',
router,
render: h => h(App)
});
routes.js
import ComponentName from './ComponentName';
export const routes = [
{
path:'/your-path'
component:ComponentName
}
]
参考
只需将router.js文件中的createWebHashHistory替换为createWebHistory
打开src->router->index.js文件
在这个文件的底部:
const router = new VueRouter({
mode: "history",
routes,
});
vue-router使用哈希模式,简单地说,它是您通常期望从这样的achor标记中得到的东西。
<a href="#some_section">link<a>
使散列消失
const routes = [
{
path: '/',
name: 'Home',
component: Home,
},
] // Routes Array
const router = new VueRouter({
mode: 'history', // Add this line
routes
})
警告:如果您没有正确配置服务器,或者您正在使用客户端SPA用户,可能会得到404错误 如果他们试图直接从浏览器访问https://website.com/posts/3。 Vue路由器文档