本文整理汇总了TypeScript中@angular/core.Injector.create方法的典型用法代码示例。如果您正苦于以下问题:TypeScript Injector.create方法的具体用法?TypeScript Injector.create怎么用?TypeScript Injector.create使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类@angular/core.Injector
的用法示例。
在下文中一共展示了Injector.create方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: useInjector
export function useInjector() {
let injector: Injector;
// #enddocregion injector
/*
// #docregion injector-no-new
// Cannot instantiate an Injector like this!
let injector = new Injector([
{ provide: Car, deps: [Engine, Tires] },
{ provide: Engine, deps: [] },
{ provide: Tires, deps: [] }
]);
// #enddocregion injector-no-new
*/
// #docregion injector, injector-create-and-call
injector = Injector.create({
providers: [
{ provide: Car, deps: [Engine, Tires] },
{ provide: Engine, deps: [] },
{ provide: Tires, deps: [] }
]
});
// #docregion injector-call
let car = injector.get(Car);
// #enddocregion injector-call, injector-create-and-call
car.description = 'Injector';
injector = Injector.create({
providers: [{ provide: Logger, deps: [] }]
});
let logger = injector.get(Logger);
logger.log('Injector car.drive() said: ' + car.drive());
return car;
}
示例2: createRenderer
export function createRenderer(formula: Formula, parentInjector: Injector): FormulaRenderer {
const DIFFER = provideDiffer(formula, parentInjector)
const RENDERER = provideRenderer(formula)
const injector = Injector.create({
parent: parentInjector,
providers: [
RENDERER,
DIFFER,
{
provide: FORMULA,
useValue: formula,
},
],
})
if (formula.type === FormulaType.ARRAY) {
return injector.get(FormulaArrayRenderer)
}
if (formula.type === FormulaType.GROUP) {
return injector.get(FormulaGroupRenderer)
}
if (formula.type === FormulaType.CONTAINER) {
return injector.get(FormulaContainerRenderer)
}
return injector.get(FormulaDefaultRenderer)
}
示例3: return
return (editor: EditorService) => {
const pluginState: CodeBlockState = pluginKey.getState(editor.state)
const origin = pluginState.element
if (!pluginState.toolbarVisible) {
overlayRef.detach()
return
}
const toolbar = new FloatingToolbar(editor, overlayRef)
const injector = Injector.create({
parent: parentInjector,
providers: [
{
provide: FloatingToolbar,
useValue: toolbar,
},
{
provide: EditorToolbar,
useValue: toolbar,
},
],
})
const linkPanelPortal = new ComponentPortal(CodePanelComponent, null, injector)
const positions = [
new ConnectionPositionPair(
{ originX: "center", originY: "bottom" },
{ overlayX: "center", overlayY: "top" },
),
]
if (previousOrigin !== origin) {
overlayRef.detach()
}
if (!overlayRef.hasAttached()) {
overlayRef.attach(linkPanelPortal)
const strategy = overlay
.position()
.flexibleConnectedTo(origin)
.withFlexibleDimensions(false)
.withPush(false)
.withPositions(positions)
const scrollStrategy = overlay.scrollStrategies.reposition()
strategy.attach(overlayRef)
strategy.apply()
scrollStrategy.attach(overlayRef)
scrollStrategy.enable()
overlayRef.updatePositionStrategy(strategy)
previousOrigin = origin
}
}
示例4: render
render(node: FormulaArrayNode) {
const parent = this.injector.get(FORMULA_NODE, null)
const injector = Injector.create({
parent: this.injector,
providers: [
{
provide: FormulaContext,
useValue: {
model: node.model,
data: node.formula.data,
resolve: {},
},
},
{
provide: FORMULA,
useValue: node.formula,
},
{
provide: FORMULA_NODE,
useValue: node,
},
],
})
if (node.formula.component && node !== parent) {
const factory = this.cfr.resolveComponentFactory(node.formula.component)
const component = this.vcr.createComponent(factory, null, injector)
if (node.formula.class) {
node.formula.class
.split(" ")
.forEach(className =>
this.renderer.addClass(component.location.nativeElement, className),
)
}
this.refs.set(node, component)
} else {
node.model.valueChanges.pipe(startWith(null)).subscribe(() => {
const diff = this.differ.diff(node.children)
if (diff) {
diff.forEachRemovedItem(record => {
this.removeChild(record.item)
})
diff.forEachMovedItem(record => {
this.moveChild(record.item, record.currentIndex)
})
diff.forEachAddedItem(record => {
this.addChild(record.item)
})
}
})
}
}
示例5: it
it('works', () => {
// #docregion Injector
const injector: Injector =
Injector.create({providers: [{provide: 'validToken', useValue: 'Value'}]});
expect(injector.get('validToken')).toEqual('Value');
expect(() => injector.get('invalidToken')).toThrowError();
expect(injector.get('invalidToken', 'notFound')).toEqual('notFound');
// #enddocregion
});
示例6: it
it('should use [ndcDynamicInjector] if provided', () => {
fixture.componentInstance.component = InjectedComponent;
fixture.componentInstance.injector = Injector.create([
{ provide: token, useValue: tokenValue }
], fixture.componentRef.injector);
fixture.detectChanges();
const injectedElem = fixture.debugElement.query(By.directive(InjectedComponent));
expect(injectedElem.injector.get(token)).toBe(tokenValue);
});
示例7: createComponent
public createComponent() {
const injector = Injector.create({
parent: this.parentInjector,
providers: [
{
provide: EditorNodeViewContext,
useValue: new EditorNodeViewContext(this.node, this.view, this.getPos),
},
],
})
this.componentRef = this.componentFactory.create(injector, null, this.node, this.ngModule)
}
示例8: beforeEach
beforeEach(() => {
const injector = Injector.create([
{provide: ResponseOptions, useClass: BaseResponseOptions, deps: []},
{provide: MockBackend, deps: []}
]);
backend = injector.get(MockBackend);
const base = new BaseRequestOptions();
sampleRequest1 =
new Request(base.merge(new RequestOptions({url: 'https://google.com'})) as any);
sampleResponse1 = new Response(new ResponseOptions({body: 'response1'}));
sampleRequest2 =
new Request(base.merge(new RequestOptions({url: 'https://google.com'})) as any);
sampleResponse2 = new Response(new ResponseOptions({body: 'response2'}));
});