ChatGPT解决这个技术问题 Extra ChatGPT

Vue.js - 如何删除 hashbang #!从网址?

如何从 url 中删除 hashbang #!

我在 vue 路由器文档 ( http://vuejs.github.io/vue-router/en/options.html ) 中找到了禁用 hashbang 的选项,但是这个选项删除了 #! 并只放了 #

有什么办法可以让网址干净吗?

例子:

不是:#!/home

但是:/home

谢谢!


B
Bill Criswell

Vue 3 中,您希望将 createWebHistory 用于 history 选项。

import { createRouter, createWebHashHistory } from 'vue-router'

const router = createRouter({
  history: createWebHashHistory(),
  // ...
})

Vue 2 中,您需要将 mode 设置为 'history'

const router = new VueRouter({
  mode: 'history',
  // ...
})

不过,请确保您的服务器已配置为处理这些链接。 https://router.vuejs.org/guide/essentials/history-mode.html


谢谢比尔在这里你也可以删除 hashbang false 这里是代码:const router = new VueRouter({ history: true })
我正在玩 github.com/vuejs/vue-hackernews 并为首页添加 {history: true} 作品,但其余路线都失败了。
您的意思是当您在其他路线上重新加载应用程序时?如果是这样,您需要正确配置您的服务器。
请将 vue.js 2 信息放在答案的开头
这段代码是放在 Router 文件夹的 index.js 中还是放在主 App.vue 中?
k
kissu

对于 vue.js 2,请使用以下内容:

const router = new VueRouter({
 mode: 'history'
})

这个答案和这里接受的答案有什么区别?
在意识到这是解决方案后编辑了已接受的答案,您可以查看已接受答案的编辑日志。
P
Paul

对于 Vue 3,改变这个:

const router = createRouter({
    history: createWebHashHistory(),
    routes,
});

对此:

const router = createRouter({
    history: createWebHistory(),
    routes,
});

来源:https://next.router.vuejs.org/guide/essentials/history-mode.html#hash-mode


...而这个 import { createRouter, createWebHistory } from 'vue-router'
k
kissu

Hash 是一个默认的 vue-router 模式设置,设置它是因为有了 hash,应用程序不需要连接服务器来提供 url。要更改它,您应该配置您的服务器并将模式设置为 HTML5 History API 模式。

对于服务器配置,这是帮助您设置 Apache、Nginx 和 Node.js 服务器的链接:

https://router.vuejs.org/guide/essentials/history-mode.html

然后你应该确保 vue 路由器模式设置如下:

vue 路由器版本 2.x

const router = new VueRouter({
  mode: 'history',
  routes: [...]
})

需要明确的是,这些都是你可以选择的 vue-router 模式: “历史” | “抽象的”。


k
kissu

对于 Vuejs 2.5 和 vue-router 3.0,上面没有任何东西对我有用,但是在玩了一点之后,以下似乎工作:

export default new Router({
  mode: 'history',
  hash: false,
  routes: [
  ...
    ,
    { path: '*', redirect: '/' }, // catch all use case
  ],
})

请注意,您还需要添加包罗万象的路径。


k
kissu

引用文档。

vue-router 的默认模式是哈希模式——它使用 URL 哈希来模拟一个完整的 URL,这样当 URL 发生变化时页面就不会重新加载。为了摆脱散列,我们可以使用路由器的历史模式,它利用 history.pushState API 来实现 URL 导航,而无需重新加载页面:

const router = new VueRouter({
  mode: 'history',
  routes: [...]
})

使用历史模式时,URL 看起来“正常”,例如 http://oursite.com/user/id。美丽的!但是,这里有一个问题:由于我们的应用程序是单页客户端应用程序,没有适当的服务器配置,如果用户直接在浏览器中访问 http://oursite.com/user/id,将会收到 404 错误。现在这很难看。不用担心:要解决此问题,您需要做的就是向您的服务器添加一个简单的包罗万象的后备路由。如果 URL 不匹配任何静态资源,它应该提供与您的应用程序相同的 index.html 页面。再次漂亮!


k
kissu
window.router = new VueRouter({
   hashbang: false,
   //abstract: true,
  history: true,
    mode: 'html5',
  linkActiveClass: 'active',
  transitionOnLoad: true,
  root: '/'
});

并且服务器已正确配置在 apache 中,您应该编写 url rewrite

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteRule ^index\.html$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.html [L]
 </IfModule>

R
Ritankar Paul

vue-router 使用 hash-mode,简而言之,这是您通常期望从这样的 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
})

Warning:如果您没有正确配置的服务器,或者您使用的是客户端 SPA,用户在尝试直接从浏览器访问 https://website.com/posts/3 时可能会收到 404 ErrorVue Router Docs


H
Harvey Mushman

上面的几个很好的描述让我陷入了兔子洞,直到我意识到在 router/index.js 文件中的两个位置存在替换“createWebHashHistory”的“createWebHistory”。一次在文件末尾定义常量,然后在文件顶部从 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'

现在它就像一个魅力,谢谢上面所有的人带领我走上成功的道路!


k
kissu

您应该向路由器添加模式历史记录,如下所示

export default new Router({
  mode: 'history',
  routes: [
    {
     ...
    }
  ]
})

k
kissu
const router = new VueRouter({
  mode: 'history',
  routes: [...]
})

如果您使用的是 AWS 放大,请查看这篇关于如何配置服务器的文章:Vue router’s history mode and AWS Amplify


S
Shujat Munawar

在 src->router->index.js 打开文件

在这个文件的底部:

const router = new VueRouter({
  mode: "history",
  routes,
});

k
kissu

vue-router 的默认模式是哈希模式——它使用 URL 哈希来模拟一个完整的 URL,这样当 URL 发生变化时页面就不会重新加载。为了摆脱散列,我们可以使用路由器的历史模式,该模式利用 history.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)
});

路由.js

import ComponentName from './ComponentName';

export const routes = [
   {
      path:'/your-path'
      component:ComponentName
   }
]

Reference


虽然此代码可能会为问题提供解决方案,但最好添加有关其工作原理/方式的上下文。这可以帮助未来的用户学习并将这些知识应用到他们自己的代码中。在解释代码时,您也可能会以赞成票的形式从用户那里获得积极的反馈。
I
IGF

只需在 router.js 文件中将 createWebHashHistory 替换为 createWebHistory