为了账号安全,请及时绑定邮箱和手机立即绑定

无法调用嵌入 css/js 文件

无法调用嵌入 css/js 文件

Go
鸿蒙传说 2022-10-10 15:44:25
我正在嵌入我的 css 和 js 文件,如下所示:package resourcesimport (    "embed")// WebUI is our static web ui from onsen ui.//go:embed public/onsenvar WebUI embed.FS// Views is our static web server layouts, views with dynamic content and partials content that is a static view.//go:embed templates/layouts templates/views templates/partialsvar Views embed.FS并尝试将其定义WebUI为static我的主要功能中的文件夹:package mainimport (    "fmt"    "log"    "html/template"    "net/http"    "onsen/resources")var view *template.Templatevar err errorfunc init() {    fmt.Println("Starting up.")    view = template.Must(template.ParseFS(resources.Views, "templates/layouts/*.html", "templates/views/*.html", "templates/partials/*.html"))    if err != nil {        log.Fatal("Error loading templates:" + err.Error())    }}func main() {    http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.FS(resources.WebUI))))    //http.Handle("/static/", http.FileServer(http.FS(resources.WebUI)))    http.HandleFunc("/index", index)    server := http.Server{        Addr: "127.0.0.1:8070",    }    server.ListenAndServe()}func index(w http.ResponseWriter, r *http.Request) {    err = view.ExecuteTemplate(w, "index.html", nil)    if err != nil {        log.Fatalln(err)    }}文件夹结构如图所示:但是运行时,我收到以下错误:CSS files: Refused to apply style from 'http://127.0.0.1:8070/static/css/onsenui.css' because its MIME type ('text/plain') is not a supported stylesheet MIME type, and strict MIME checking is enabled.JS files: Failed to load resource: the server responded with a status of 404 (Not Found)
查看完整描述

1 回答

?
冉冉说

TA贡献1877条经验 获得超1个赞

我通过将static路线固定为:

http.Handle("/webUI/", http.StripPrefix("/webUI/", http.FileServer(http.FS(resources.WebUI))))

和文件都css调用js为:

<link rel="stylesheet" type="text/css" href="/webUI/public/onsen/css/onsenui.css"/>

那是: 'The static path [/webUI] + the original route [public/onsen/...]


查看完整回答
反对 回复 2022-10-10
  • 1 回答
  • 0 关注
  • 126 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号