我有主要方法:
Future<Null> main() async {
final securityService = new SecurityService(new BrowserClient());
await securityService.getObject();
bootstrapStatic<AppComponent>(
AppComponent,
<dynamic>[
provide<SecurityService>(SecurityService,
useFactory: () => securityService, deps: []),
provide<BrowserClient>(BrowserClient,
useFactory: () => new BrowserClient(), deps: []),
routerProvidersHash
],
ng.initReflector as Function);
}
我想重写它以使用bootstrapFactory
。怎么做?我应该在providers: const [MyService]
的组件中重写什么?
答案 0 :(得分:1)
所以,我写了这个:
// ignore: uri_has_not_been_generated
import 'package:budget/app_component.template.dart' as ng_app;
...
// ignore: uri_has_not_been_generated
import 'main.template.dart' as ng;
@GenerateInjector(const [
const ClassProvider<BrowserClient>(BrowserClient),
const FactoryProvider<SecurityService>(SecurityService, getSecurityService),
routerProvidersHash
])
final InjectorFactory appInjector = ng.appInjector$Injector;
SecurityService _securityService;
SecurityService getSecurityService() => _securityService;
Future<Null> main() async {
_securityService = new SecurityService(new BrowserClient());
await _securityService.getObject();
runApp<AppComponent>(
ng_app.AppComponentNgFactory as ComponentFactory<AppComponent>,
createInjector: appInjector);
}
答案 1 :(得分:0)
它也可以这样做:
Client _browserClient;
Client getBrowserClient() => _browserClient;
SecurityService _securityService;
SecurityService getSecurityService() => _securityService;
Future<Null> main() async {
_browserClient = new BrowserClient();
_securityService = new SecurityService(_browserClient);
await _securityService.getObject();
bootstrapStatic(
AppComponent,
[
const FactoryProvider(Client, getBrowserClient),
const FactoryProvider(SecurityService, getSecurityService),
],
ng.initReflector);
}
编辑:上述方法适用于AngularDart 5.0.0-alpha + 9。从5.0.0-alpha + 10版本开始,强烈建议使用runApp
函数,而不是bootstrapStatic