java动态内容通过HTTP客户端调用不加载
我试图从here重新创建spring boot angularjs示例应用程序
当我使用运行应用程序时/mnvw spring boot:运行以下错误显示:
[ERROR] ERROR in src/app/app.component.html(6,18): : Property 'id' does not exist on type '{}'.
[ERROR] src/app/app.component.html(7,23): : Property 'content' does not exist on type '{}'.
我的源代码与上面提供的链接相同,但为了清楚起见,typescript文件:
import { Component } from '@angular/core';
import {HttpClient} from '@angular/common/http';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
title = 'Demo';
data = {};
constructor(private http: HttpClient) {
http.get('resource').subscribe(data => this.data = data);
}
}
html文件:
<div style="text-align:center"class="container">
<h1>
Welcome {{title}}!
</h1>
<div class="container">
<p>Id: <span>{{data.id}}</span></p>
<p>Message: <span>{{data.content}}</span></p>
</div>
</div>
java控制器:
@SpringBootApplication
@Controller
public class AngularApplication {
public static void main(String[] args) {
SpringApplication.run(AngularApplication.class, args);
}
@GetMapping("/resource")
@ResponseBody
public Map<String, Object> home() {
Map<String, Object> model = new HashMap<String, Object>();
model.put("id", UUID.randomUUID().toString());
model.put("content", "Hello World");
return model;
}
}
应用程序。单元ts文件导入HttpClientModule。当我将应用程序服务于localhost:4200时,站点将加载但不加载数据
其思想是为/resource请求提供服务,并为客户端返回一个具有正确密钥的对象,但这些密钥不存在和/或客户端无法识别
如何正确地将生成的id和hello world内容传递给数据对象
# 1 楼答案
包装上有一点不同。导致问题的json。所提供链接中的一个使用“build”:“ng build”,而从CLI生成的默认链接使用“build”:“ng build prod”。在改变了这一点之后,这个例子开始发挥作用