简体   繁体   English

如何使用 typescript 在外部 json 文件中推送数据?

[英]How to push data in external json file using typescript?

enter image description here actually I have push data external JSON file在此处输入图像描述实际上我有推送数据外部 JSON 文件

First your jSON file in stored inside the assets folder.首先,您的 jSON 文件存储在assets文件夹中。

src/assets/demo.json

JSON: JSON:

[
    {
        "Name": "Senthil",

    },
{
    "Name" : "Periyas"
}
...
]

next, create file in your root folder and name it njson-typings.d.ts and then paste below code.接下来,在根文件夹中创建文件并将其命名为njson-typings.d.ts ,然后粘贴以下代码。

declare module "*.json" {
  const value: any;
  export default value;
}

go to app component folder open the app.component.ts file and import the demo file to be displayed. go 到app组件文件夹打开app.component.ts文件并导入要显示的demo文件。

 import { Component } from '@angular/core';
 import namedata from '../assets/demo.json';

    @Component({
      selector: 'app-root',
      templateUrl: './app.component.html',
      styleUrls: ['./app.component.scss']
    })
    export class AppComponent {
      title = 'json-demo';
      Names: any = namedata;
    }

then open the app.component.html paste the below code.然后打开app.component.html粘贴下面的代码。

<ul>
  <li *ngFor="let data of Names">
   <div>{{data.Name}}</div>
  </li>
</ul>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM