Reading data.json with HttpClient on Stackblitz?

后端 未结 3 1970
夕颜
夕颜 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:31

    Stackblitz currently doesn't serve static files except the case when they are in assets folder.

    So you have two options here:

    1) Import json directly as module

    import data from './data.json';
    
    console.log(data) // => {title: "Simulating HTTP Requsts", content: "This is off the hook!!"}
    

    For more details See other answers

    2) Move that json in assets folder(Note: you have to reload stackblitz to make it working):

    http.get('/assets/data.json')
    

    Forked Stackblitz

提交回复
热议问题