在Stackblitz上使用HttpClient读取data.json吗? [英] Reading data.json with HttpClient on Stackblitz?

查看:116
本文介绍了在Stackblitz上使用HttpClient读取data.json吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个很小的演示,并尝试使用Angular HttpClient读取app/data.json.

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

const post$:Observable<Post> = <Observable<Post>> http.get('./data.json');

但是HttpClient响应说:

解析过程中出现故障...

Failure during parsing ...

有想法吗?

推荐答案

Stackblitz当前不提供静态文件,除非它们位于assets文件夹中.

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

所以您在这里有两个选择:

So you have two options here:

1)直接将json作为模块导入

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)将json移动到资产文件夹中( 注意:您必须将stackblitz重新加载到 使它起作用):

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

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

分叉Stackblitz

Forked Stackblitz

这篇关于在Stackblitz上使用HttpClient读取data.json吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆