-
Notifications
You must be signed in to change notification settings - Fork 672
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
40 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,49 @@ | ||
import { Inject, Injectable, Optional } from '@angular/core'; | ||
import { ActivatedRouteSnapshot, CanActivate, CanActivateChild, RouterStateSnapshot, UrlTree } from '@angular/router'; | ||
import { Inject, Injectable, Optional, inject } from '@angular/core'; | ||
import { ActivatedRouteSnapshot, CanActivateChildFn, CanActivateFn } from '@angular/router'; | ||
import { Observable, of } from 'rxjs'; | ||
|
||
import { AlainConfigService } from '@delon/util/config'; | ||
|
||
import { AlainI18NService, ALAIN_I18N_TOKEN } from './i18n'; | ||
|
||
@Injectable({ providedIn: 'root' }) | ||
export class AlainI18NGuard implements CanActivate, CanActivateChild { | ||
export class AlainI18NGuardService { | ||
constructor( | ||
@Optional() | ||
@Inject(ALAIN_I18N_TOKEN) | ||
private i18nSrv: AlainI18NService, | ||
private cogSrv: AlainConfigService | ||
) {} | ||
|
||
private resolve(route: ActivatedRouteSnapshot): Observable<boolean> { | ||
process(route: ActivatedRouteSnapshot): Observable<boolean> { | ||
const lang = route.params && route.params[this.cogSrv.get('themeI18n')?.paramNameOfUrlGuard ?? 'i18n']; | ||
if (lang != null) { | ||
this.i18nSrv.use(lang); | ||
} | ||
return of(true); | ||
} | ||
} | ||
|
||
canActivateChild( | ||
childRoute: ActivatedRouteSnapshot, | ||
_: RouterStateSnapshot | ||
): boolean | UrlTree | Observable<boolean | UrlTree> | Promise<boolean | UrlTree> { | ||
return this.resolve(childRoute); | ||
} | ||
/** | ||
* Simple 路由守卫, [ACL Document](https://ng-alain.com/auth/guard). | ||
* | ||
* ```ts | ||
* data: { | ||
* path: 'home', | ||
* canActivate: [ alainI18nCanActivate ] | ||
* } | ||
* ``` | ||
*/ | ||
export const alainI18nCanActivate: CanActivateFn = childRoute => inject(AlainI18NGuardService).process(childRoute); | ||
|
||
canActivate( | ||
route: ActivatedRouteSnapshot, | ||
_: RouterStateSnapshot | ||
): boolean | UrlTree | Observable<boolean | UrlTree> | Promise<boolean | UrlTree> { | ||
return this.resolve(route); | ||
} | ||
} | ||
/** | ||
* Simple 路由守卫, [ACL Document](https://ng-alain.com/auth/guard). | ||
* | ||
* ```ts | ||
* data: { | ||
* path: 'home', | ||
* canActivateChild: [ alainI18nCanActivateChild ] | ||
* } | ||
* ``` | ||
*/ | ||
export const alainI18nCanActivateChild: CanActivateChildFn = route => inject(AlainI18NGuardService).process(route); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters