我正在努力弄清楚如何从 DynamicModule 向常规模块提供服务。伪代码如下:
应用程序模块.ts
@Global()
@Module({
imports: [
DynamicModule.forRoot(config),
RegularModule,
],
providers: [],
exports: [],
})
export class AppModule {}
Run Code Online (Sandbox Code Playgroud)
动态模块.ts
@Module({})
export class DynamicModule implements OnModuleInit, OnModuleDestroy {
constructor(private dynamicService: dynamicService) {}
static forRoot(config: Config): DynamicModule {
return {
module: DynamicModule,
imports: [],
providers: [
{
provide: CONFIG_TOKEN,
useValue: config,
},
DynamicService,
],
exports: [
DynamicService,
],
};
}
}
Run Code Online (Sandbox Code Playgroud)
动态服务.ts
@Injectable()
export class DynamicService {
constructor(
@Inject(CONFIG_TOKEN) private readonly config: Config,
) {}
}
Run Code Online (Sandbox Code Playgroud)
常规模块.ts
@Module({
imports: [], …Run Code Online (Sandbox Code Playgroud) 我的类.cpp文件中有2个方法:
getDoctorAt() 在该位置获取int ant return元素
Doctor Hospital::getDoctorAt(const int pos) const {
if ((pos >= 0) && (pos < implHospital->doctors.size()))
return implHospital->doctors[pos];
else
throw out_of_range("Index out of bounds");
}
Run Code Online (Sandbox Code Playgroud)
而getLastPatient()这应该返回或最后添加到载体中最后一个元素
Patient Hospital::getLastPatient() {
//int pos = implHospital->patients.size()-1;
return implHospital->patients.back();
}
Run Code Online (Sandbox Code Playgroud)
但是这getLastPatient()不起作用,如果执行程序崩溃.
cout << h.getDoctorAt(2) << endl; // ok!
cout << h.getLastPatient() << endl; // crashes program
Run Code Online (Sandbox Code Playgroud)
有什么想法吗?不想发布完整代码,很长时间.