VueJS 路由/导航问题

Mat*_*leo 0 javascript vue.js vue-router netlify

使用 VueJS 时遇到问题(第一次使用)

  • 我在 doc root 的 index.html 文件中有 90% 的页面模板
  • 我有 3 个组件(每个组件都包含每个“页面”的主要内容正文)

我的路由器:

export default new Router({
  mode: 'history',
  hash: false,
  routes: [
    {
      path: '/',
      name: 'Home',
      component: Home
    },
    {
      path: '/gallery',
      name: 'Gallery',
      component: Gallery
    },
    {
      path: '/contact',
      name: 'Contact',
      component: Contact
    }
  ]
})
Run Code Online (Sandbox Code Playgroud)

我无法开始<router-link :to="{ name: 'Gallery' }">Gallery</router-link>工作 - 我的网站没有将它们呈现为我的 index.html 中的锚标记(我可能不明白如何/在哪里可以使用 Vue) - 所以我使用的是标准链接,例如<a class="nav-link" href="/gallery">Gallery</a>

问题:

虽然所有这些代码在我的本地机器上运行良好,但它在我上传代码的任何地方都不起作用(我想让它在 Netlify 上运行)。Netlify 和其他站点会覆盖我删除哈希的尝试,因此我的链接然后链接到例如

https://examplesite.com/#/ => https://examplesite.com/gallery#/

小智 5

hash不是一个Router选择。尝试删除它。要在 Netlify 上使用历史模式,您必须将一个_redirects文件添加到您的public目录中。将此添加到文件中:

/* / 200 这将确保所有路径都由 vue-router

  • 嗨 James - 我没有 `public` 目录,但是我已经将此 `_redirects` 文件添加到我的 `dist` 文件夹中,现在该站点似乎可以在 Netlify 上运行,谢谢:) (3认同)