vue.js - 前端/后端路由配置
问题描述
$ tree .
.
├── main.go
└── static
└── output_of_npm_run_build
我使用 vue.js 和 vue 路由器作为前端,使用 golang 作为后端。我试图完成的事情:
使用 golang(标准库或 gorilla/mux)来处理
example.com/api/...
vue 路由器处理所有 vue 组件(
example.com
、、、example.com/about
等example.com/login
)example.com/post/{post_id}
用于前端和后端的RESTful API
如果我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}
?
解决方案
您需要更新托管配置。以前在使用 React、Vue 等前端 JS 框架时都会出现这个问题。
因此,如果您example.com/about
在地址栏上手动输入,页面将显示“404 Not Found”。
我推荐这个答案来解决你的问题。
推荐阅读
- c# - 不能为 IDictionary 使用扩展方法
- python - 使用 Chunked 数组(Akwkard 库)进行精美的索引或屏蔽
- c++ - 将 while + if else 转换为更简单的 std::remove_if
- entity-framework - 按日期范围划分的 Entity Framework Core 组
- python - 在回复板中制作内联键盘
- sas - 删除宏变量值列表中的单引号
- visual-studio-code - 更改 VSCode 状态栏的图标顺序
- java - 将 java spring-boot app *.jar 与根文件夹中的其他文件一起部署到 heroku
- firebase - 在每个屏幕中获取当前 Firebase 用户的正确方法是什么?
- android - Android ViewModel 参考 Fragment