无法调用嵌入 css/js 文件

我正在嵌入我的 css 和 js 文件,如下所示:


package resources


import (

    "embed"

)


// WebUI is our static web ui from onsen ui.

//go:embed public/onsen

var 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/partials

var Views embed.FS

并尝试将其定义WebUI为static我的主要功能中的文件夹:


package main


import (

    "fmt"

    "log"

    "html/template"

    "net/http"

    "onsen/resources"

)


var view *template.Template

var err error


func 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)

    }

}

文件夹结构如图所示:

http://img4.mukewang.com/6343cd74000148e319181028.jpg

但是运行时,我收到以下错误:

http://img.mukewang.com/6343cd7f0001099513030434.jpg

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)



鸿蒙传说
浏览 127回答 1
1回答

冉冉说

我通过将static路线固定为:http.Handle("/webUI/",&nbsp;http.StripPrefix("/webUI/",&nbsp;http.FileServer(http.FS(resources.WebUI))))和文件都css调用js为:<link&nbsp;rel="stylesheet"&nbsp;type="text/css"&nbsp;href="/webUI/public/onsen/css/onsenui.css"/>那是:&nbsp;'The static path [/webUI] + the original route [public/onsen/...]
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go