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

Mat*_*ski 5 javascript routes angular2-routing angular

我不知道为什么,但它突然停止工作......这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';
    }

}
Run Code Online (Sandbox Code Playgroud)

Twi*_*her 6

这是一个打字稿问题。按照评论中的建议进行记录在这里没有帮助,因为问题出现在编译时。

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