Load json from local file with http.get() in angular 2

前端 未结 7 1147
情歌与酒
情歌与酒 2020-11-29 00:38

I\'m trying to load a local json with http.get() in angular 2. I tried something, that I found here on stack. It looks like this:

this is my app.m

7条回答
  •  被撕碎了的回忆
    2020-11-29 01:33

    I you want to put the response of the request in the navItems. Because http.get() return an observable you will have to subscribe to it.

    Look at this example:

    // version without map
    this.http.get("../data/navItems.json")
        .subscribe((success) => {
          this.navItems = success.json();          
        });
    
    // with map
    import 'rxjs/add/operator/map'
    this.http.get("../data/navItems.json")
        .map((data) => {
          return data.json();
        })
        .subscribe((success) => {
          this.navItems = success;          
        });

提交回复
热议问题