vue.js - 前端/后端路由配置

标签 vue.js go vue-router

$ 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} ?

    最佳答案

    您需要更新托管配置。以前在使用 React、Vue 等前端 JS 框架时都会出现这个问题。
    所以如果你输入 example.com/about手动在地址栏上,页面将显示“404 Not Found”。
    我推荐这个答案来解决你的问题。
    Vue router is not working with nginx

    关于vue.js - 前端/后端路由配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62951897/

    相关文章:

    postgresql - golang 应用程序如何在 aws 服务器中使用 docker 与 postgres 通信?

    html - 如何在 GO、Google App Engine 中解析 HTML 模板

    javascript - 将重复的查询参数推送到 Vue 路由器

    javascript - 如何在加载某些异步数据(在 Vuex 存储中)之前防止任何路由?

    javascript - Vue 3 事件命名约定让我感到困惑

    vue.js - Vuejs 中有条件的 href

    javascript - 语法错误: Cannot use import statement outside a module - vue electron builder plugin

    go - Go 程序的主 goroutine 和派生 goroutine 之间的区别

    javascript - 如何在 JavaScript 中使用日历

    javascript - 无法将 VueJS 2 哈希解析为空字符串