类型“路由器”上不存在属性“导航”。您是说“导航”吗?

时间:2019-04-15 04:34:48

标签: angular routing angular2-routing

我不知道为什么,但是它突然停止工作... import是正确的,但是看不到navigate()方法。
每个答案都是关于丢失import,但是这里有一个正确的importnavigate()停止在代码大声笑的每个部分中工作,这仅是示例。有什么想法吗?

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

import { Actions, Effect, ofType } from '@ngrx/effects';
import * as  AuthActions from './auth.actions'
import * as UserDetailsActions from '../user/user-store/user.actions';
import * as StudentActions from '../../student/student-store/student.actions';

import { map, mergeMap, switchMap, switchMapTo, concatMapTo, withLatestFrom } from 'rxjs/operators';

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

@Injectable()
export class AuthEffects {
    constructor(private actions$: Actions,
                private router: Router) { }

    @Effect()
    authSignin = this.actions$
        .pipe(
            ofType(AuthActions.TRY_SIGNIN),
            map((action: AuthActions.TrySignin) => {
                return action.payload;
            }),
            switchMap((authData: any) => {

                //here should be request to backend server with JWT
                //set token and and username or user id
                const userRetunedFromRequest = {
                    id: '5',
                    username: authData.username,
                    role: authData.role
                }

                localStorage.setItem('currentUser', JSON.stringify(userRetunedFromRequest));
                //----------------------------------------------------

                return [
                    new AuthActions.SigninUser,
                    new UserDetailsActions.GetUserDetailsById
                ]
            })
        );

    @Effect({ dispatch: false })
    loginRedirect = this.actions$
        .pipe(
            ofType(AuthActions.SIGNIN_USER),
            map((action: AuthActions.SigninUser) => {
                let url = this.navigateByUserRole();
                this.router.navigate([`/${url}`]);
            })
        );


    private navigateByUserRole(): string {
        return JSON.parse(localStorage.getItem('currentUser')).role === 'PARENT' ? 'student' : 'teacher';
    }

}

1 个答案:

答案 0 :(得分:0)

这是打字稿问题。注释中建议的日志在此处无济于事,因为问题出在编译时。

尝试(<any>this.router).navigate([`/${url}`]);,如果这可以解决您的问题,则您的依赖项版本可能存在问题。从头开始创建一个新项目,并使用生成的package.json更新您的项目。