我正在嵌入我的 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) }}文件夾結(jié)構如圖所示:但是運行時,我收到以下錯誤: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條經(jīng)驗 獲得超1個贊
我通過將static
路線固定為:
http.Handle("/webUI/", http.StripPrefix("/webUI/", http.FileServer(http.FS(resources.WebUI))))
和文件都css
調(diào)用js
為:
<link rel="stylesheet" type="text/css" href="/webUI/public/onsen/css/onsenui.css"/>
那是: 'The static path [/webUI] + the original route [public/onsen/...]
- 1 回答
- 0 關注
- 114 瀏覽
添加回答
舉報
0/150
提交
取消