如何删除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?
例子:
不是:# !/家庭
但是:/家庭
谢谢!
当前回答
只需将router.js文件中的createWebHashHistory替换为createWebHistory
其他回答
上面的几个很好的描述把我带进了兔子洞,直到我意识到“createWebHistory”取代了“createWebHashHistory”存在于router/index.js文件的两个地方。一次是在文件末尾的常量定义中,一次是在文件顶部的vue-router导入中。
在router/index.js文件的末尾找到
const router = createRouter({
mode: 'history',
history: createWebHistory(),
// history: createWebHashHistory(),
routes
})
router/index.js文件的第一行
import { createRouter, createWebHistory } from 'vue-router'
// import { createRouter, createWebHashHistory } from 'vue-router'
现在它就像一个魅力,感谢上面所有的人指引我走上这条成功之路!
散列是vue-router模式的默认设置,之所以设置它是因为使用散列,应用程序不需要连接服务器来提供url。要改变它,你应该配置你的服务器,并将模式设置为HTML5 History API模式。
对于服务器配置,这是帮助你设置Apache, Nginx和Node.js服务器的链接:
https://router.vuejs.org/guide/essentials/history-mode.html
然后你应该确保,vue路由器模式设置如下:
Vue-router version 2.x
const router = new VueRouter({
mode: 'history',
routes: [...]
})
需要明确的是,这些都是你可以选择的vue-router模式:"hash" | "history" | "abstract"。
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路由器文档
const router = new VueRouter({
mode: 'history',
routes: [...]
})
如果您正在使用AWS amplify,请查看关于如何配置服务器的本文:Vue路由器的历史模式和AWS amplify
对于vue.js 2,使用以下方法:
const router = new VueRouter({
mode: 'history'
})