首页 > 解决方案 > 如何从我的应用程序内的项目中获取资产文件?

问题描述

我在主项目中有一个应用程序(称为 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 文件呢?

标签: angularangular-httpclientangular-http

解决方案


您相对于根目录的路径是"projects/http-app/assets/config.json"

否则你可以尝试两个目录"../../assets/config.json"

你能记录这个变量的值吗?如果您使用节点模块,我不确定您的应用程序结构,您可以尝试导入

import config from "projects/http-app/assets/config.json"

推荐阅读