简体   繁体   中英

TS2304: Cannot find name 'Router'

I'm having some trouble redirecting to a certain page in my Angular 2 Ionic app. I have followed the following example: https://embed.plnkr.co/?show=preview . Yet I cannot seem to get it working as I keep getting this error: 'TS2304 Cannot find name 'Router''

My IDE shows the error on the 'Router' keyword in the constructor() line 12.

My code:

import {Component} from '@angular/core';
import 'rxjs/Rx';
import {BackandService} from '@backand/angular2-sdk';
import { Router } from '@angular/router';

@Component({
  templateUrl: 'user_auth.html',
  selector: 'page-user-auth',
})
export class UserAuthPage {

  constructor(private backand: BackandService, public router: Router) {
    this.backand.user.getUserDetails().then(
      (res: any) => {
        if (res.data) {
          console.log(res.data);
          this.router.navigate('/login');
        }
      },
      (err: any) => {
        null;
      }
    );
  }

Any ideas?

Thanks in advance.

EDIT: Too late I realized; if you're working with Ionic framework, you should use the Ionic navigation instead of Angular 2 routing. This helped me further eventually.

Issue :

 this.router.navigate('/login');

should be

 this.router.navigate(['/login']);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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