繁体   English   中英

组件内的Angular 2路由不起作用(this.router.navigate不起作用)

[英]Angular 2 routing inside a component not working (this.router.navigate not working)

我试图开发一个angular 2应用程序,一旦满足特定条件,该应用程序将路由到另一个组件,为此,我使用了this.router.navigate方法,但它似乎没有执行,因为它一直显示“无法读取属性'navigate'未定义”错误。 将不胜感激任何帮助:)

具体组成

import { Component, OnInit } from '@angular/core';

import { RouterModule, Routes, Router } from '@angular/router';

@Component({
  selector: 'app-searching',
  templateUrl: './searching.component.html',
  styleUrls: ['./searching.component.css']
})
export class SearchingComponent implements OnInit {

constructor(private router:Router) { }

ngOnInit() {
 var elem = document.getElementById("rightSideMovingProgressBar"); 
 var elem2 = document.getElementById("progressText");
 var height = 100;
 var height2 = 0;
 var id = setInterval(frame, 20);
 function frame() {
    if (height <= 0) {
        clearInterval(id);
        this.router.navigate(['/details']);
    } else {
        height--;
        height2++; 
        elem.style.height = height + 'vh';
        elem2.innerHTML = height2 + '%'; 
    }
  } 
}

}

错误

在此处输入图片说明

这是因为thisframe不再指向组件。 使用以下内容:

 var id = setInterval(()=>{ frame() }, 20);

阅读答案以及答案,以获取更多信息以及使用bindbound class properties其他可能方法。

您可以存储this到一个变量:

var that = this ;

在您可以用作的功能内

that.router.navigate(['/details']);

希望这可以帮助 :)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM