慕田峪9158850
2021-12-20 15:49:58
我使用本教程使用 GoLang、Angular2 和 Dart 制作 Web 應(yīng)用程序,但是當(dāng)我通過(guò)控制臺(tái)命令“backend”啟動(dòng)后端,并在瀏覽器中路由到“l(fā)ocalhost:8080/”時(shí),它必須從 Dart 的類“Hello”中調(diào)用方法,但是它沒(méi)有調(diào)用,我收到 404 錯(cuò)誤。我從教程中得到的所有代碼都沒(méi)有改變?nèi)魏螙|西。而且我找不到任何其他教程。你能解釋一下我做錯(cuò)了什么嗎?高朗代碼:func main() { http.Handle("/", http.FileServer(http.Dir("./app/web/"))) fmt.Println("Text") http.HandleFunc("/api/hello", helloWorld) http.ListenAndServe(":8080", nil)}func helloWorld(w http.ResponseWriter, r *http.Request) { data := struct { Message string }{ "Hello, World", } if err := json.NewEncoder(w).Encode(data); err != nil { log.Println(err) }}和角度飛鏢代碼:class AppComponent { Hello hello = new Hello();}class Hello{ String message; Hello(){ HttpRequest.getString('/api/hello') .then((String content) { Map parsedMap = JSON.decode(content); message = parsedMap["Message"]; }) .catchError((Error error) { print(error.toString()); }); }}我使用本教程使用 GoLang、Angular2 和 Dart 制作 Web 應(yīng)用程序,但是當(dāng)我通過(guò)控制臺(tái)命令“backend”啟動(dòng)后端,并在瀏覽器中路由到“l(fā)ocalhost:8080/”時(shí),它必須從 Dart 的類“Hello”中調(diào)用方法,但是它沒(méi)有調(diào)用,我收到 404 錯(cuò)誤。我從教程中得到的所有代碼都沒(méi)有改變?nèi)魏螙|西。而且我找不到任何其他教程。你能解釋一下我做錯(cuò)了什么嗎?高朗代碼:func main() {
http.Handle("/", http.FileServer(http.Dir("./app/web/")))
fmt.Println("Text")
http.HandleFunc("/api/hello", helloWorld)
http.ListenAndServe(":8080", nil)}func helloWorld(w http.ResponseWriter, r *http.Request) {
data := struct {
Message string
}{
"Hello, World",
}
if err := json.NewEncoder(w).Encode(data); err != nil {
log.Println(err)
}}和角度飛鏢代碼:class AppComponent {
Hello hello = new Hello();}class Hello{
String message;
Hello(){
HttpRequest.getString('/api/hello')
.then((String content) { Map parsedMap = JSON.decode(content);
message = parsedMap["Message"];
})
.catchError((Error error) { print(error.toString());
});
}}和項(xiàng)目結(jié)構(gòu):
1 回答

qq_遁去的一_1
TA貢獻(xiàn)1725條經(jīng)驗(yàn) 獲得超8個(gè)贊
教程已經(jīng)很老了。您需要切換到HashLocationStrategy(據(jù)我所知,這是當(dāng)時(shí)的默認(rèn)設(shè)置)。
見(jiàn)https://angular.io/docs/ts/latest/api/router/HashLocationStrategy-class.html
改變
bootstrap(AppComponent);
到
bootstrap(AppComponent, [
ROUTER_PROVIDERS,
provide(LocationStrategy, {useClass: HashLocationStrategy})
]);
您還需要添加一些額外的導(dǎo)入
import 'package:angular2/router.dart'
show
HashLocationStrategy,
LocationStrategy,
ROUTER_PROVIDERS;
添加回答
舉報(bào)
0/150
提交
取消