Reading data.json with HttpClient on Stackblitz?

后端 未结 3 1939
夕颜
夕颜 2021-01-20 06:30

I have a tiny demo and it attempts to read app/data.json using the Angular HttpClient.

const post$:Observable = 

        
3条回答
  •  隐瞒了意图╮
    2021-01-20 07:26

    Currently, you can't get the JSON directly over HTTP, but you can import it instead

    data.json

    it returns resource of index.html instead of the data you expected


    online example

    import { of } from 'rxjs';
    import data from './data.json';
    
    export class AppComponent  {
      constructor(http:HttpClient) {
    
      }
    
      ngOnInit(){
        const post$ = of(data);
        post$.subscribe(console.log);
      }
    }
    

提交回复
热议问题