admin管理员组文章数量:1241124
I have no idea why, but it was suddenly stopped working... The import
is correct but it cannot see navigate()
method.. any ideas?
Every answer for this is about missing import
but there is a correct import
here. navigate()
stopped working in every part of my code LOL it is only example here. Any ideas?
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';
}
}
I have no idea why, but it was suddenly stopped working... The import
is correct but it cannot see navigate()
method.. any ideas?
Every answer for this is about missing import
but there is a correct import
here. navigate()
stopped working in every part of my code LOL it is only example here. Any ideas?
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';
}
}
Share
Improve this question
edited Sep 30, 2021 at 21:33
Guerric P
31.8k6 gold badges58 silver badges105 bronze badges
asked Apr 15, 2019 at 4:34
Mateusz GebroskiMateusz Gebroski
1,3345 gold badges30 silver badges61 bronze badges
4
- console log the URL. there might be an issue that url is not available. – TheParam Commented Apr 15, 2019 at 5:00
-
Console log the
this.router
too. It seems it may have been overriden somehow in the dependency injection or sth. – Apokralipsa Commented Apr 15, 2019 at 5:06 -
Any chances of context change of
this
.. try logging and check. – Sachin Gupta Commented Apr 15, 2019 at 5:19 - what does it mean and how could it be overriden? – Mateusz Gebroski Commented Apr 15, 2019 at 9:04
3 Answers
Reset to default 7This is a Typescript issue. Logging as suggested in the ments won't help here because the problem is at pilation time.
Try (<any>this.router).navigate([`/${url}`]);
and if that fixes your problem, then you probably have an issue with your dependencies versions. Create a new project from scratch and use the generated package.json
to update your project's one.
This issue will be raised because of your imports with "@angular/router" like this:
import { Router } from '@angular/router';
Sometimes when the typescript automatically adds the "import {} from ''" it uses different modules. Let me explain with an example:
I wrote the following to add dependency.
constructor(private router: Router){}
The typescript added:
import {Router} from "express"
instead of:
import { Router } from '@angular/router'
So the piler gave me the same error that you faced and rightly so. I change the first import statement to the second one and the error was resolved. Keep an eye on these automatic imports to verify.
版权声明:本文标题:javascript - Property 'navigate' does not exist on type 'Router'. Did you mean 'navigate 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1740095320a2224192.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论