直接 url 在 vuejs 中不起作用,但路由器推送有效
Direct url not working in vuejs but router push works
当我使用 this.$router.push()
加载组件 localhost:8080/scan
时,url 加载组件但是当我执行 npm run build
然后 http-server dist/
时,url localhost:8080/scan
不起作用,给出 404。
知道为什么它在开发中工作而不是在构建之后工作。
我确实在线检查并发现这是一个 nginx 问题,但我尝试按照此文档添加 try_files $uri $uri/ /index.html;
:https://router.vuejs.org/en/essentials/history-mode.html 但似乎没有任何效果。非常感谢任何帮助。
请 post 问题的完整代码,或 github 存储库。否则很难猜出你的问题是什么。
如果我不得不猜测 - 你的 http-server
找不到 scan.html
因为它不存在。您有几个选择:
- 使用
hash mode
不需要 /scan
url 而是依赖 /index.html#scan
。
- 使用带有定义路由的 node-js 服务器转到您的 html 页面。或者只是将它们全部发送到同一页面。这是一个例子 https://github.com/ubershmekel/redditp/blob/1e9a659bcc34fa2f2dc28deb552b6702a1173cc1/app.js#L12
- 使用 Nuxt 或做一些像 Nuxt 那样的事情——构建所有 html 文件,这样你仍然可以使用像
http-server
这样的静态服务器。请参阅 https://nuxtjs.org/guide#static-generated-pre-rendering- 例如,您可以手动将 index.html
复制到 scan.html
。
当我使用 this.$router.push()
加载组件 localhost:8080/scan
时,url 加载组件但是当我执行 npm run build
然后 http-server dist/
时,url localhost:8080/scan
不起作用,给出 404。
知道为什么它在开发中工作而不是在构建之后工作。
我确实在线检查并发现这是一个 nginx 问题,但我尝试按照此文档添加 try_files $uri $uri/ /index.html;
:https://router.vuejs.org/en/essentials/history-mode.html 但似乎没有任何效果。非常感谢任何帮助。
请 post 问题的完整代码,或 github 存储库。否则很难猜出你的问题是什么。
如果我不得不猜测 - 你的 http-server
找不到 scan.html
因为它不存在。您有几个选择:
- 使用
hash mode
不需要/scan
url 而是依赖/index.html#scan
。 - 使用带有定义路由的 node-js 服务器转到您的 html 页面。或者只是将它们全部发送到同一页面。这是一个例子 https://github.com/ubershmekel/redditp/blob/1e9a659bcc34fa2f2dc28deb552b6702a1173cc1/app.js#L12
- 使用 Nuxt 或做一些像 Nuxt 那样的事情——构建所有 html 文件,这样你仍然可以使用像
http-server
这样的静态服务器。请参阅 https://nuxtjs.org/guide#static-generated-pre-rendering- 例如,您可以手动将index.html
复制到scan.html
。