我不知道为什么,但它突然停止工作...
import
是正确的,但它看不到 navigate()
方法.. 有什么想法吗? import
,但这里有一个正确的 import
。 navigate()
停止在我的代码的每个部分工作,哈哈,这只是这里的示例。有什么想法吗?
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';
}
}
这是一个 Typescript 问题。按照评论中的建议进行记录在这里没有帮助,因为问题出现在编译时。
尝试
(<any>this.router).navigate([`/${url}`]);
,如果这解决了您的问题,那么您的依赖项版本可能有问题。从头开始创建一个新项目并使用生成的 package.json
更新您的项目。
这个问题会因为像这样的“@angular/router”导入而出现 从 '@angular/router' 导入 { Router };