0

我的意图是创建本文详述的打印模块。加载我的 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>

应用组件.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);
  }  
}

打印服务.ts

import { Injectable } from '@angular/core';
import { Router } from '@angular/router';

@Injectable({
    providedIn: 'root'
})
export class PrintService{
    isPrinting = false;

    constructor(private router: Router) {}   
}

样式.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; }
  }

您能否让我知道我必须进行哪些更改才能使其正常工作?

4

1 回答 1

2

您将服务命名printServiveprintService. 因此无法找到该服务,因此在该未定义属性上调用属性会导致此错误。

于 2019-06-14T18:03:25.570 回答