【问题标题】:Frontend/backend route configuration前端/后端路由配置
【发布时间】:2020-11-07 03:18:03
【问题描述】:
$ tree .
.
├── main.go
└── static
    └── output_of_npm_run_build

我使用 vue.js 和 vue 路由器作为前端,使用 golang 作为后端。我试图完成的事情:

  • 使用 golang(标准库或 gorilla/mux)来处理example.com/api/...

  • vue 路由器负责所有 vue 组件(example.comexample.com/aboutexample.com/login 等)

  • 用于前端和后端的 RESTful API example.com/post/{post_id}

如果我转到example.com 并单击按钮about,vue 路由器将显示about 组件并将url 更改为example.com/about 而无需与后端交谈,这是预期的。但是如果我直接在地址栏输入example.com/about然后回车,请求就会直接到后端。由于文件about不存在,所以返回404。

有没有办法返回 static/index.html 并以某种方式告诉 vue 路由器渲染 xxx 组件,即使 example.com/xxx 是由浏览器直接发送到服务器(example.com/api 除外)?

谁应该负责路由example.com/post/{post_id}

【问题讨论】:

标签: vue.js go vue-router


【解决方案1】:

您需要更新主机配置。以前在使用 React、Vue 等前端 JS 框架时都会出现这个问题。

所以如果你在地址栏手动输入example.com/about,页面会显示“404 Not Found”。

我推荐这个答案来解决你的问题。

Vue router is not working with nginx

【讨论】:

    猜你喜欢
    • 2016-01-29
    • 2018-12-22
    • 2010-12-14
    • 2017-12-20
    • 2021-05-05
    • 2012-12-12
    • 2020-03-15
    • 1970-01-01
    • 2022-01-01
    相关资源
    最近更新 更多