0

I am trying to recreate the spring-boot angularjs example application from here.

When I run the application using ./mnvw spring-boot:run the following error shows:

[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 '{}'.

My source code is the same as the link provided above but for clarity, the typescript file:

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);
  }
}

The html file:

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

And the java controller:

@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;
    }
}

The app.module.ts file imports HttpClientModule. When I ng serve the application to localhost:4200 the site loads but without data.

The idea is to service the /resource request and return an object with the right keys for the client, but the keys do not exist and/or are not being recognized by the client.

How do I correctly pass the generated id and hello world content to the data object?

georgeawg
  • 48,608
  • 13
  • 72
  • 95
Sander_M
  • 1,109
  • 2
  • 18
  • 36
  • you should give the `data` property the correct type (object with id and content) for the compiler warnings to go away. – toskv Dec 24 '17 at 21:08
  • The working example code from the link provided doesn't define the data object with its types either and I am trying to keep the code as close to that example as possible. – Sander_M Dec 24 '17 at 21:19
  • well. the error you posted 1st is complaining about that. Otherwise I would suggest you look at what URL the UI is trying to call and make sure it matches the one for the backend. Ceck for missing / or different port numbers first. :) – toskv Dec 24 '17 at 21:24
  • thank you for your suggestions and fast response, I will try them out :) – Sander_M Dec 24 '17 at 21:34

1 Answers1

0

There was a tiny difference in package.json that was causing the issue. The one from the provided link uses "build": "ng build" and the default generated one from CLI uses "build": "ng build --prod". After changing this the example is working.

Sander_M
  • 1,109
  • 2
  • 18
  • 36
  • 1
    --prod adds the --aot flag. this post has more info on that. https://stackoverflow.com/questions/41450226/angular-2-just-in-time-jit-vs-ahead-of-time-aot-compilation – toskv Dec 24 '17 at 23:02