Gin加载history模式下打包后的Vue文件,刷新找不到页面404
import ( "io/ioutil" "github.com/gin-contrib/static" "github.com/gin-gonic/gin" ) app := gin.Default() //加载静态资源 app.Use(static.Serve("/", static.LocalFile("dist", true))) app.NoRoute(func(c *gin.Context) { accept := c.Request.Header.Get("Accept") flag := strings.Contains(accept, "text/html") if flag { content, err := ioutil.ReadFile("dist/index.html") if (err) != nil { c.Writer.WriteHeader(404) c.Writer.WriteString("Not Found") return } c.Writer.WriteHeader(200) c.Writer.Header().Add("Accept", "text/html") c.Writer.Write((content)) c.Writer.Flush() } })
亲自测试,可以实现。