Javascript 如何从AppModule级别的共享库中获取提供的服务?有棱角的
我正在尝试创建一个保护服务来验证我的路由。但是我的警卫服务在一个共享的图书馆里 这是我的警卫服务:Javascript 如何从AppModule级别的共享库中获取提供的服务?有棱角的,javascript,angular,angular-ui-router,ng-modules,Javascript,Angular,Angular Ui Router,Ng Modules,我正在尝试创建一个保护服务来验证我的路由。但是我的警卫服务在一个共享的图书馆里 这是我的警卫服务: @Injectable({ providedIn: 'root' }) export class RouteGuard implements CanActivate { canActivate( route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean { return true; }
@Injectable({
providedIn: 'root'
})
export class RouteGuard implements CanActivate {
canActivate(
route: ActivatedRouteSnapshot,
state: RouterStateSnapshot): boolean {
return true;
}
}
在共享模块上提供:
import {RouteGuard} from './auth/guard/route.guard';
@NgModule({
imports: [CommonModule, MaterialModule],
exports: [MaterialModule],
providers: [RouteGuard]
})
export class SharedModule {}
现在我需要在应用程序模块上获得此提供的服务。我该怎么做
import {SharedModule} from '@acn-collections-ws/shared';
@NgModule({
declarations: [AppComponent, AuthComponent, LoggedComponent, HomeComponent, LoginComponent, SigninComponent],
imports: [BrowserModule, BrowserAnimationsModule, AppRoutingModule, SharedModule],
providers: [],
bootstrap: [AppComponent],
exports: [
AuthComponent,
LoggedComponent,
HomeComponent,
LoginComponent,
SigninComponent
],
})
export class AppModule {}
我需要在我的应用程序路由模块中导入此服务,并与canActivate route validator一起使用。像这样:
// i cant found RouteGuard at acn-collections-ws/shared
import { RouteGuard } from '@acn-collections-ws/shared';
const routes: Routes = [
{
path: '',
component: LoggedComponent,
children: [
{path: '', component: HomeComponent}
],
canActivate: [RouteGuard]
},
{
path: '',
component: AuthComponent,
children: [
{path: '', redirectTo: 'login', pathMatch: 'full'},
{path: 'login', component: LoginComponent},
{path: 'signin', component: SigninComponent}
]
}
];
好的,我找到了
@NgModule({
imports: [CommonModule, MaterialModule],
exports: [MaterialModule],
providers: [RouteGuard]
})
export class SharedModule {}
从共享库的index.ts中,我需要导出guard.module。(我没有导出防护模块)
我知道这很有效
// Service guard
import {RouteGuard} from '@acn-collections-ws/shared';
为什么要在
@acn collections ws/shared'
中查找RouteGuard
?我不是来自某个路径/auth/guard/route.guard'?我的守卫位于共享库@acn collections ws/shared
im,使用@nwrl库--预设=角度。有了这个,我可以用@导入外部lib,你能创建一个最小的可验证示例吗?我今天会尝试,稍后再做。谢谢兄弟!
// Service guard
import {RouteGuard} from '@acn-collections-ws/shared';