如何重定向到ngOnInit()内的特定页面,以angular [重复]

sample.component.ts

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

@Component({
  selector: 'app-sample',
  templateUrl: './sample.component.html',
  styleUrls: ['./sample.component.scss']
})
export class Sample implements OnInit {
  closeResult: string;
  focuscount:number=0;
  fcount:any;

  constructor(private router: Router) {
    ngOnInit() {
    function final(){
    setTimeout(() => {
      this.router.navigate(['pattern']);
  }, 120000);
}
  **Checking check screen focus**  
  function onBlur() {
    document.body.className = 'blurred';
    console.log("not focused");
    focuscount+=focuscount
    this.fCount=focuscount;
    console.log("Fcount:::::",this.fCount);
    if(this.fCount>=3){
    console.log("Inside focuscount");
    final();

}
};
}
}

满足条件时,我试图重定向到模式页面。我收到类型错误。请帮我修复它。

提前致谢

enter image description here

评论
  • Stanf
    Stanf 回复

    You can not use this inside arrow function change to

     ngOnInit() {
        let self = this;
        function final(){
        setTimeout(() => {
          self.router.navigate(['pattern']);
      }, 120000);