【发布时间】:2020-12-22 12:16:42
【问题描述】:
我已将我的应用程序从 Angular 9 升级到 Angular 10。升级过程已成功,我能够运行该应用程序。但是当我发出ng serve 命令时,它会显示以下警告。
WARNING in src\app\auth\guard\auth.guard.ts depends on 'lodash'. CommonJS or AMD dependencies can cause optimization bailouts.
For more info see: https://angular.io/guide/build#configuring-commonjs-dependencies
WARNING in src\app\shared\services\api.service.ts depends on 'rxjs/Observable'. CommonJS or AMD dependencies can cause optimization bailouts.
For more info see: https://angular.io/guide/build#configuring-commonjs-dependencies
WARNING in src\app\auth\guard\auth.guard.ts depends on 'rxjs/observable/fromPromise'. CommonJS or AMD dependencies can cause optimization bailouts.
For more info see: https://angular.io/guide/build#configuring-commonjs-dependencies
WARNING in src\app\shared\services\localforage.service.ts depends on 'rxjs/observable/forkJoin'. CommonJS or AMD dependencies can cause optimization bailouts.
For more info see: https://angular.io/guide/build#configuring-commonjs-dependencies
WARNING in src\app\auth\guard\auth.guard.ts depends on 'rxjs/add/operator/map'. CommonJS or AMD dependencies can cause optimization bailouts.
For more info see: https://angular.io/guide/build#configuring-commonjs-dependencies
有一种解决方法是建议一些人将 commonJS 列入白名单,但它仍然会导致包大小的增加。我怎样才能摆脱这个常见的 JS 问题并仍然获得优化的包大小?
PS:最后的代码用map操作符
import { Router, CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router';
import { switchMap, tap, map } from 'rxjs/operators';
import { Observable, from} from 'rxjs';
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> {
return this.getToken().map((token: any) => {
if ((token !==null) && !this.jwtHelper.isTokenExpired(token.data)) {
// this.proactiveTokenRefresh(token);
return true;
}
// Store the attempted URL for redirecting
this.localForage.setItem('redirectUrl', state.url).then(() => {
// Navigate to the login page
this.router.navigate(['/login']);
return false;
});
});
}
private getToken(): Observable<{}> {
const token = this.localForage.getItem('id_token');
return from(token);
}`
【问题讨论】:
标签: angular typescript rxjs commonjs angular10