代码的简化版本:
conds = [df.Quantity > 80, df['Type'] == 'annual']
choices = [0.25,0.5]
# or if you prefer strings formated with %:
# choices = ['25%','50%']
df['discount'] = pd.np.select(conds, choices)
>>> df
UPC Quantity Name Size Color Type discount
0 11313 68 Crocus small purple perennial 0.00
1 42164 51 Sunflower large yellow annual 0.50
2 53890 76 Zinnia small pink annual 0.50
3 54885 85 Daisy medium white perennial 0.25
4 77314 80 Rose large red perennial 0.00
5 79375 62 Mum medium yellow annual 0.50
6 94281 1 Dahlia large orange perennial 0.00
7 95085 41 Marigold small orange annual 0.50
所有这些都无法解决。它失败,并显示以下标准消息:
StaticInjectorError(AppModule)[InjectionToken HTTP_INTERCEPTORS-> ActivatedRouteSnapshot]:StaticInjectorError(平台: 核心)[InjectionToken HTTP_INTERCEPTORS-> ActivatedRouteSnapshot]: NullInjectorError:没有提供ActivatedRouteSnapshot的提供程序!
..将@Injectable()
export class JwtInterceptor implements HttpInterceptor {
constructor(
private readonly router: Router,
private readonly activatedRouteSnapshot: ActivatedRouteSnapshot,
@Inject(AuthServiceFactory) private readonly authServiceFactory: ServiceFactoryBase<IAuthService>,
@Inject(LoggingServiceFactory) private readonly loggingServiceFactory: ServiceFactoryBase<ILoggingService>) {
console.log('router', router);
console.log('activated-route-snapshot', activatedRouteSnapshot);
}
导入应用程序的正确方法是什么?
P.S。我得到了RouterModule
而不是SharedModule
来导出所有 mine 东西,但没有导出Angular的东西:
AppModule
@NgModule({
declarations: any.concat(pipes),
providers: any
.concat(serviceFactories)
.concat(guards)
.concat(otherProviders)
.concat([{
provide: HTTP_INTERCEPTORS,
useClass: JwtInterceptor,
multi: true
}]),
exports: any.concat(pipes)
})
export class SharedModule {
static forRoot(): ModuleWithProviders {
return {
ngModule: SharedModule,
providers: any
.concat(serviceFactories)
.concat(guards)
.concat(otherProviders)
};
}
}
:
AppModule
答案 0 :(得分:2)
当您像使用ClassProvider
一样必须使用HttpInterceptor
时,Angular不能像编译模块本身作为令牌本身那样编译提供程序依赖项。基本上,类型标记在运行时实际上并不存在,并且Angular将这些标记用于依赖项注入-由于ClassProvider
或ValueProvider
在运行时被评估,因此它们没有得到正确的DI处理他们应得的。
您只需要对此声明更多一点即可
{
provide: HTTP_INTERCEPTORS,
useClass: JwtInterceptor,
deps: [Router, ActivatedRoute],
multi: true
}
deps
数组中的标记将在创建时注入到组件中。这里的一件棘手的事情是deps
必须与构造函数参数中的 order 相同。
此外,ActivatedRouteSnapshot
的提供者就是ActivatedRoute
。
通过使用@Inject()
装饰器,您应该能够完成相同的事情,就像您与其他提供程序所做的一样-Angular将在第一个匹配项的依赖树上遍历并注入它(尽管我对此不是100%肯定)。