如何在 Angular 2 中获取 JSON 文件 [英] How to fetch JSON file in Angular 2

查看:15
本文介绍了如何在 Angular 2 中获取 JSON 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于我是 Angular 的新手,任何人都可以提供一个使用 angular 2 加载 JSON 文件数据的简单解决方案.

我的代码如下

索引.html

<头><title>Angular 2 快速入门</title><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1"><link rel="stylesheet" href="css/styles.css"><!-- 1. 加载库--><!-- 旧浏览器的 Polyfill(s) --><script src="node_modules/core-js/client/shim.min.js"></script><script src="node_modules/zone.js/dist/zone.js"></script><script src="node_modules/reflect-metadata/Reflect.js"></script><script src="node_modules/systemjs/dist/system.src.js"></script><!-- 2.配置SystemJS--><script src="systemjs.config.js"></script><脚本>System.import('app').catch(function(err){ console.error(err); });<!-- 3. 显示应用程序--><身体><my-app>正在加载...</my-app></html>

app.component.ts

import {Component} from '@angular/core';@成分({选择器:'我的应用',模板:`<div id="main">主部门<div id = "标题"></div><div id = "内容"><ul class="游戏"><li>

`})导出类 AppComponent {}

games.json

{游戏":[{"title":"主要操作",启用":真},{"title":"精选游戏",启用":假}]}

我想在 app.component.ts 中将 games.json 中的所有游戏提取到 li 中请详细指教.

解决方案

这里是我解析JSON的部分代码,可能对你有帮助:

import { Component, Input } from '@angular/core';从@angular/core"导入{可注射};从@angular/http"导入 { Http, Response, Headers, RequestOptions };从 'rxjs/Rx' 导入 {Observable};导入 'rxjs/add/operator/map';导入 'rxjs/add/operator/catch';@Injectable()导出类 AppServices{构造函数(私有http:Http){变量对象;this.getJSON().subscribe(data => obj=data, error => console.log(error));}public getJSON(): Observable{返回 this.http.get("./file.json").map((res:any) => res.json()).catch((error:any) => console.log(error));}}

as I am new to the Angular, can anyone please give a simple solution on loading the JSON file data using angular 2.

My code is like below

Index.html

<html>
  <head>
    <title>Angular 2 QuickStart</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="css/styles.css">
    <!-- 1. Load libraries -->
     <!-- Polyfill(s) for older browsers -->
    <script src="node_modules/core-js/client/shim.min.js"></script>
    <script src="node_modules/zone.js/dist/zone.js"></script>
    <script src="node_modules/reflect-metadata/Reflect.js"></script>
    <script src="node_modules/systemjs/dist/system.src.js"></script>
    <!-- 2. Configure SystemJS -->
    <script src="systemjs.config.js"></script>
    <script>
      System.import('app').catch(function(err){ console.error(err); });
    </script>
  </head>
  <!-- 3. Display the application -->
  <body>
    <my-app>Loading...</my-app>
  </body>
</html>

app.component.ts

import {Component} from '@angular/core';
  
@Component({
  selector: 'my-app',
  template: `
          <div id="main">
            Main Div
               <div id = "header"></div>
               <div id = "content">
                  <ul class="games">
                      <li>
											
                      </li>
                  </ul>
               </div>
          </div>
  		 `
})
export class AppComponent {
 }

games.json

{
	"games":[
		{
			"title":"Primary Operations",
			"enabled":true
		},
		{
			"title":"Curated Games",
			"enabled":false
		}
	]
}

I want to fetch all games from games.json into li at app.component.ts Please advise in detail.

解决方案

Here is a part of my code that parse JSON, it may be helpful for you:

import { Component, Input } from '@angular/core';
import { Injectable }     from '@angular/core';
import { Http, Response, Headers, RequestOptions } from '@angular/http';
import {Observable} from 'rxjs/Rx';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/catch';

@Injectable()
export class AppServices{

    constructor(private http: Http) {
         var obj;
         this.getJSON().subscribe(data => obj=data, error => console.log(error));
    }

    public getJSON(): Observable<any> {
         return this.http.get("./file.json")
                         .map((res:any) => res.json())
                         .catch((error:any) => console.log(error));

     }
}

这篇关于如何在 Angular 2 中获取 JSON 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
其他开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆