【发布时间】:2019-05-09 15:22:18
【问题描述】:
这应该是最简单的事情了。我有一个组件调用直接导入本地 JSON 的服务(请参阅Import JSON directly with Angular 7) 它可以很好地读取 JSON 内容,但 pages 属性未定义。我想我在该链接中基于 StackBlitz 设置了所有内容,似乎没有太多内容。还没有任何 HTML,这一切都只是通过控制台。它在 app.component.html 中。
读取本地json文件json.service.ts:14
[{…}]0: {name: "Home"}length: 1__proto__: Array(0) json.service.ts:15
未定义的 home.component.ts:31
json.service.ts:
import { Injectable } from '@angular/core';
import SampleJson from '../assets/SampleJson.json';
export interface JsonInterface {
name: any;
}
@Injectable()
export class JsonService {
ngOnInit() {
console.log('Reading local json files');
console.log(SampleJson);
}
}
home.component.ts:
import { JsonService, JsonInterface } from '../json.service';
import { Component, OnInit, Input } from '@angular/core';
@Component({
selector: 'app-home',
templateUrl: './home.component.html',
styleUrls: ['./home.component.scss']
})
export class HomeComponent implements OnInit {
constructor(service: JsonService) {
service.ngOnInit();
};
@Input() pages: JsonInterface;
ngOnInit() {
console.log(this.pages);
}
}
Sample.json
{ "name":"Home" }
【问题讨论】:
-
你能发布html吗?
-
@AkhiAkl 没有。只是 app.component.html 中的
。还有 Home Works!
在 home.component.html
标签: json angular typescript angular7