【问题标题】:Applying changes for vue application executed by GO为 GO 执行的 vue 应用程序应用更改
【发布时间】:2019-04-08 21:16:02
【问题描述】:

我使用 Go 创建了一个非常简单的 Web 应用程序,只有一个网页。在这个项目中,我使用 Vue CLI 来安装 webpack。如果我通过 npm 运行 vue 应用程序,一切都很好并且工作完美,但是我希望在运行 go web 应用程序时能够使用 vue。为此,我通过 npm 'npm run build' 构建了 vue-project 并获取 dist 目录,之后在 go 应用程序中我编写了如下内容:

func main() {
    http.Handle("/static/", http.FileServer(http.Dir("web-vue/dist")))

    http.HandleFunc("/", HomePage)
    http.ListenAndServe(":8080", nil)
}

func HomePage(w http.ResponseWriter, r *http.Request){
    t, err := template.ParseFiles("web-vue/dist/index.html")
    if err != nil {
        log.Print("template parsing error: ", err)
    }
    err = t.Execute(w,"SerGorn")
    if err != nil {
        log.Print("template executing error: ", err)
    }
}

它工作正常,但有一个例外,当我更改 .vue 文件时,为了应用此更改,需要再次通过 npm 重建应用程序。我该如何解决这个问题?任何想法?或者如果仅在 node.js 中使用 vue 可能是错误的方式和更好的方式?

【问题讨论】:

  • 我不确定 Vue 是如何做到的,但在其他 JS 框架中,build 命令仅在部署到生产之前执行。在本地开发时,您实际上并不需要缩小等...

标签: node.js go vue.js webpack


【解决方案1】:

.vue 源文件必须经过处理才能被 Web 服务器作为 HTML 提供。您不能修改 .vue 文件并期望在针对生产 Web 服务器运行的 Web 浏览器中看到更改。因此,修改后您必须运行npm run build,它将处理.vue 文件并将有效的HTML 文件生成到/dist

现在查看您的代码,我发现您可以通过这样做进一步简化它:

func main() {
    http.Handle("/static", http.FileServer(http.Dir("web-vue/dist/")))
    http.Handle("/", http.FileServer(http.Dir("web-vue/dist/")))
    http.ListenAndServe(":8080", nil)
}

【讨论】:

    【解决方案2】:

    您可以将 vue 与任何后端一起使用。似乎 FileServer 或您的浏览器缓存了您的静态文件。您可以使用 Cache-Control: no-cache 标头来避免缓存。

    【讨论】:

      猜你喜欢
      • 2020-08-13
      • 1970-01-01
      • 2019-03-20
      • 1970-01-01
      • 2023-03-20
      • 2023-03-03
      • 1970-01-01
      • 2021-11-04
      • 1970-01-01
      相关资源
      最近更新 更多