您的位置:首页 > Web前端 > React

GO语言引用React+Antd(脚手架使用dva@2,使用umi进行build)遇到问题,并解决后的记录

2018-03-30 13:47 1556 查看
前提:React+Antd开发使用dva作为脚手架,dva版本为2.0版本(1.0用的是roadhog编译),使用umi进行编译。生成的文件目录如下:
--index.html(会引用./static/umi.XX.css以及./static/umi.XX.js)
--service-worker.js
--static
------static(内容为需要使用的图片)
------umi.XX.css
------umi.XX.js
------若干个src__pages__XX.js(对应为路由页面)
------XX.async.js
------index.html

   我在自己开发时,网上并没有找到对应的GO引用React案例,我自己在引用时,好久都没有成功。最后终于自己找到了问题,在此记录希望对大家有用。
 在使用React+Antd调试代码没有问题后,进行编译并将包导出使用。在使用GO语言做静态服务:
func RouteListen() {
h.Handle("/static/", h.StripPrefix("/static/", h.FileServer(h.Dir("static"))))
h.HandleFunc("/", ReactPage)
err := h.ListenAndServe(":4444", nil)
if err != nil {
log.Fatal("ListenAndServe:", err)
}

}
做React对应的界面。
func ReactPage(w h.ResponseWriter, r *h.Request) {
f.Println("&ReactPage启动~~~~")
if r.Method == "GET" {
t, _ := template.ParseFiles("index.html")
t.Execute(w, nil)
f.Println("ReatPage Get")
} else {
f.Println("ReactPage Post")
}

}
自动生成的根目录index.html如下:
<!doctype html><html><head><meta charset="utf-8"><meta name="apple-mobile-web-app-capable" content="yes"><meta name="apple-mobile-web-app-status-bar-style" content="black"><meta name="format-detection" content="telephone=no"><meta name="format-detection" content="email=no"><meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,minimum-scale=1,user-scalable=no"><title></title></head><body><div id="root"></div><link rel="stylesheet" href="/static/static/umi.11e86cfc.css"><script>window.routerBase = '/';

  window.resourceBaseUrl = location.origin + window.routerBase + './static/static/';</script><script src="/static/static/umi.21c8036d.js"></script></body></html>

经过多次修改使用一直不正确,最后发现:路由基本目录static后的“/”一定要加!!!因为此处决定了加载js页面的访问路径,否则会出现F12调试时XX/staticsrc__pages*.js类似错误,应该为XX/static/src__pages*.js

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  GO语言 React Antd umi dva
相关文章推荐