我的意图是创建此article中详细介绍的打印模块。 加载我的app.component.html时出现以下错误:
AppComponent.html:1 ERROR TypeError: Cannot read property 'isPrinting' of undefined
App.component.html
<div [class.isPrinting]= "printService.isPrinting">
<div style="text-align:center">
<h1>
Welcome to {{title}}!
</h1>
<button (click) ="onPrintReport()"> Print Report</button>
<router-outlet name='print"'></router-outlet>
</div>
</div>
App.component.ts
import { Component } from '@angular/core';
import {PrintService} from './print.service';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
title = 'printer';
constructor(public printServive: PrintService) {
//console.log("Print:", this.printServive.isPrinting);
}
}
print.service.ts
import { Injectable } from '@angular/core';
import { Router } from '@angular/router';
@Injectable({
providedIn: 'root'
})
export class PrintService{
isPrinting = false;
constructor(private router: Router) {}
}
Style.css
/* You can add global styles to this file, and also import other style files */
@media print {
.isPrinting > * { display: none; }
.isPrinting app-print-layout { display: block; }
}
能否让我知道要使其生效需要进行哪些更改?
答案 0 :(得分:2)
您将服务命名为printServive
,而不是printService
。因此找不到服务,因此在该未定义属性上调用属性会导致此错误。