Angular -添加动态内容

gcxthw6b  于 2021-07-15  发布在  Java
关注(0)|答案(1)|浏览(339)

我刚开始使用spring和angular,并遵循以下教程:https://spring.io/guides/tutorials/spring-security-and-angular-js/
我已经到了“添加动态内容”的地步,再也没有进展了。当我使用下面的代码时,我总是得到一个错误,即对象问候语的属性id和内容没有解析。我该怎么修?
应用程序组件.html

<div style="text-align:center" class="container">
  <h1>
    Welcome {{title}}!
  </h1>
  <div class="container">
    <p>Id: <span>{{greeting.id}}</span></p>
    <p>Message: <span>{{greeting.content}}!</span></p>
  </div>
</div>

应用组件.ts

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';
  greeting = {};
  constructor(private http: HttpClient) {
    http.get('resource').subscribe(data => this.greeting = data);
  }
}

应用模块ts

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';

import { AppComponent } from './app.component';
import { HttpClientModule } from '@angular/common/http';

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    HttpClientModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

演示应用程序.java

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@SpringBootApplication
@RestController
public class DemoApplication {

  @RequestMapping("/resource")
  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;
  }

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}

提前感谢您的帮助!当做

lsmd5eda

lsmd5eda1#

正如@andrei在评论中提到的 greeting: any = {} 解决了问题。”这一行将告诉typescript编译器跳过“greeting”字段的类型检查,错误应该消失了“-andrei”

相关问题