使用angular cli时访问放置在本地文件夹中的JSON [英] Access JSON placed in local folder when using angular cli

查看:86
本文介绍了使用angular cli时访问放置在本地文件夹中的JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在项目中使用angular cli.我在下面的结构中放置了一个JSON

I am using angular cli for my projects. I have placed a JSON in structure like below

src/app/data/data.json.我正在尝试从http.get方法

src/app/data/data.json. I am trying to access it from http.get method

 return this._http.get('../data/data.json',
                              { headers: this.getHeaders })
            .map((response: Response) => response.json())
            .catch(this.handleError);

它说找不到json.但是如果我将JSON放在Assets文件夹中也可以使用.我不希望将JSON放置在资产文件夹中.如果我将json保留在应用程序文件夹中可以使用吗?

It say it cannot find the json. But same if i place JSON in assets folder it works. I dont want JSON to be placed in assets folder. Will it work if i keep the json in app folder?

推荐答案

您需要使用.angular-cli.json "assets"部分.这样,您将通过角度cli获得静态资产的输出,例如:

You need to use .angular-cli.json "assets" section. This way you will get your static assets output by angular cli e.g:

"assets": [
    "assets",
    "favicon.ico",
    "app/data/data.json"
 ],

通过在assets cli中指定app/data/data.json,将仅将一个文件data.json输出到app/data/文件夹中.如果要输出app/data文件夹中的所有资产,请执行以下操作:

By specifying app/data/data.json in assets cli will output just a single file data.json in to app/data/ folder. If you want to output all assets inside your app/data folder then you do this:

"assets": [
        "assets",
        "favicon.ico",
        "app/data"
     ],

您始终可以检查cli是否输出了您的static资产 运行ng build并检查dist文件夹的内容.

You can always check if your static assets getting outputed by cli by running ng build and checking the content of the dist folder.

您可以将自定义资产放入assets中,例如 assets/data/data.json,因此您无需修改 默认情况下,.angular-cli.json作为assets文件夹已经存在

You can place you custom assets in to assets like assets/data/data.json so this way you do not have to modify .angular-cli.json as assets folder is already there by default

这篇关于使用angular cli时访问放置在本地文件夹中的JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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