angular - 如何从我的应用程序内的项目中获取资产文件?
问题描述
我在主项目中有一个应用程序(称为 http-app),我正在按照本指南使用 HTTP 测试角度通信服务。
在 assets 文件夹中有一个 config.json 文件。
在 config.service.ts 我试图获取 config.json 文件:
configUrl = 'assets/config.json'
constructor(private http: HttpClient) { }
getConfig(): Observable<Config> {
return this.http.get<Config>(this.configUrl)
.pipe(
retry(3),
catchError(this.handleError)
);
}
但是没有找到该文件。结果:
有关文件夹结构的更多说明:
main-project
projects
http-app
assets
config.json <----
src
app
config.service.ts
...
src
app
...
那么如何在项目资产文件夹中获取 config.json 文件呢?
解决方案
您相对于根目录的路径是"projects/http-app/assets/config.json"
否则你可以尝试两个目录"../../assets/config.json"
你能记录这个变量的值吗?如果您使用节点模块,我不确定您的应用程序结构,您可以尝试导入
import config from "projects/http-app/assets/config.json"
推荐阅读
- jquery - 如何在 Yii2 2amigos CKEditor 中禁用工具提示
- javascript - 如何在 DyanmoDb API 的 FilterExpression 中使用 equals
- html - 如何根据文本框中的文本量自动调整一个元素的高度
- sql - 具有 > 14 个表的 Oracle SQL XOR 条件
- ms-access - 我想用关系设置初始化 ACCESS 表的自动编号
- c++ - C++:如何正确地将 deque::front() 返回的变量从函数中传递出去?
- html - 垂直对齐容器中的一行文本
- python - 在 Python 中使用哨兵退出 while 循环
- java - 如何以同步方式包装黑盒异步调用?
- javascript - Webpack 没有正确引用 woff2 文件