Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/32.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 角度4-无法读取属性';订阅';当我申请考试时_Angular - Fatal编程技术网

Angular 角度4-无法读取属性';订阅';当我申请考试时

Angular 角度4-无法读取属性';订阅';当我申请考试时,angular,Angular,我在测试应用程序中使用茉莉花 当我运行它时: ng test -sm=false 我得到下一个错误: TypeError: Cannot read property 'subscribe' of undefined at MenuComponent.ngOnInit (webpack:///C:/Desarrollo/Angular/Testing/src/app/menu/menu.component.ts?:21:25) 这是文件代码menu.component.ts compl

我在测试应用程序中使用茉莉花

当我运行它时:

ng test -sm=false
我得到下一个错误:

TypeError: Cannot read property 'subscribe' of undefined
    at MenuComponent.ngOnInit (webpack:///C:/Desarrollo/Angular/Testing/src/app/menu/menu.component.ts?:21:25)
这是文件代码menu.component.ts complete:

import { Component, OnInit } from '@angular/core';
import { ActivatedRoute, Router } from "@angular/router";

@Component({
  selector: 'menu',
  templateUrl: './menu.component.html'
})
export class MenuComponent implements OnInit {

  public titleSup:string = "TITTLE DEFAULT";
  public sub:any;

  constructor(private route: ActivatedRoute) { }

  ngOnInit() {
     this.sub = this.route.data.subscribe(data => {
       let title = data['titleSup'];
       this.titleSup = title;
     });

  }

  setTitle (title:string) {
    this.titleSup = title;
  }

}
内容文件app-routing.module.ts(json):

这是文件代码menu.component.html:

<div><b class="title" id="titleSup">{{titleSup}}</b></div>
{{titlessup}
我将base中的标题更改为“routes”对象的“data”属性。该应用程序工作正常,但当我运行测试时,它会给我以前评论过的错误


我怎样才能解决这个错误?谢谢

请尝试在
之后删除
数据
。路线

  public titleSup:string = "TITTLE DEFAULT";
  public sub:any;

  constructor(private route: ActivatedRoute) { }

  ngOnInit() {
    this.sub = this.route.subscribe(data => {
      let title = data['titleSup'];
      this.titleSup:string = title;
    });
  }

尝试在
之后删除
数据
。路径

  public titleSup:string = "TITTLE DEFAULT";
  public sub:any;

  constructor(private route: ActivatedRoute) { }

  ngOnInit() {
    this.sub = this.route.subscribe(data => {
      let title = data['titleSup'];
      this.titleSup:string = title;
    });
  }

这样,它会启动下一个错误:“ActivatedRoute”类型上不存在属性“subscribe”:您是否包括
rxjs/observable
是的,我测试过,但没有:(这样,它会启动下一个错误:“ActivatedRoute”类型上不存在属性“subscribe”您是否包括
rxjs/observable
是,我测试过,但没有:(