我尝试重复此代码:code
但是不能,因为返回的错误是这样的:
src / app / page / page.component.html:1:28-错误NG2339:类型“对象”上不存在属性“图像”。 1
src / app / page / page.component.ts:8:16
8 templateUrl:'./page.component.html',
组件PageComponent的模板中发生错误。
import { Component, OnInit } from '@angular/core';
import { ContentService } from '../shared/services/content.service';
import { ActivatedRoute } from '@angular/router';
@Component({
selector: 'app-page',
templateUrl: './page.component.html',
styleUrls: ['./page.component.scss']
})
export class PageComponent implements OnInit {
page: Object;
constructor(private route: ActivatedRoute,
private contentService: ContentService) { }
ngOnInit() {
const pageData = this.route.snapshot.data['page'];
this.page = this.contentService.pages[pageData];
}
}
我不知道如何解决。请帮助
答案 0 :(得分:0)
您遇到html错误,但没有显示html吗?
不要键入变量,因为不建议使用对象,请键入任意内容。
尝试:
import { Component, OnInit } from '@angular/core';
import { ContentService } from '../shared/services/content.service';
import { ActivatedRoute } from '@angular/router';
@Component({
selector: 'app-page',
templateUrl: './page.component.html',
styleUrls: ['./page.component.scss']
})
export class PageComponent implements OnInit {
page: any;
constructor(private route: ActivatedRoute,
private contentService: ContentService) { }
ngOnInit() {
const pageData = this.route.snapshot.data['page'];
this.page = this.contentService.pages[pageData];
}
}
然后在您的html中显示您的变量,请尝试以下操作:
{{page?.image}}
不客气,如果可以的话! :)