角度将组件注入到属性指令中



Tl;dr:如何提供一个可见的组件作为指令的依赖项?当然,组件必须在指令之前初始化,但它必须是稍后应用跨组件selector运行时显示的相同实例。


详:

我的app.component.html有一个这样的结构:

app.component.html

<app-navigation></app-navigation>
<router-outlet></router-outlet>

顶部有一个导航栏,始终可见。<router-outlet>始终显示当前活动的组件。

我现在想允许在<router-outlet>中呈现的组件修改导航栏的内容,例如显示适合当前活动组件的其他按钮。这应该适用于指令,如下所示:

一些组件.html

<div *appTopBar>
<button>Additional Button</button>
</div>

附加按钮现在应显示在顶部的导航栏中。

appTopBar指令如下所示:

top-bar.directive.ts

import {AfterViewInit, Directive, OnDestroy, TemplateRef} from '@angular/core';
import {AppComponent} from '../navigation/navigation.component';
@Directive({
selector: '[appTopBar]'
})
export class TopBarDirective implements AfterViewInit, OnDestroy {
constructor(private tmpl: TemplateRef<any>,
private nav: NavigationComponent) {
}
ngAfterViewInit(): void {
this.nav.setTopBarContent(this.tmpl);
}
ngOnDestroy(): void {
this.nav.setTopBarContent(null);
}
}

该指令依赖于导航组件,可以通过公开提供的方法将内容传递到导航栏setTopBarContent()

navigation.component.ts

import {Component, EmbeddedViewRef, TemplateRef, ViewChild, ViewContainerRef} from '@angular/core';
@Component({
selector: 'app-navigation',
templateUrl: './navigation.component.html',
styleUrls: ['./navigation.component.scss']
})
export class NavigationComponent {
@ViewChild('topBarContainer',{static: false})
topBar: ViewContainerRef;
topBarContent: EmbeddedViewRef<any>;
constructor() {}
/**
* Set the template to be shown in the top bar
* @param tmpl template, null clears the content
*/
public setTopBarContent(tmpl: TemplateRef<any>) {
if (this.topBarContent) {
this.topBarContent.destroy();
}
if (tmpl) {
this.topBarContent = this.topBar.createEmbeddedView(tmpl);
}
}
}

我遇到的第一个问题是,当 TopBarDirective 初始化时,NavigationComponent依赖项尚不可用。我收到以下错误:

错误

错误: 未捕获 (在承诺中(: 空注入器错误:

StaticInjectorError(AppModule([TopBarDirective -> NavigationComponent]: StaticInjectorError(Platform: core([TopBarDirective -> NavigationComponent]:

NullInjectorError: NavigationComponent 没有提供程序!

所以很明显,该组件在指令之后被初始化并且尚不可用。

我尝试将NavigationComponent添加到AppComponentproviders数组中,并且依赖项注入现在可以工作:

@NgModule({
declarations: [
NavigationComponent,
SomeComponent,
TopBarDirective
],
imports: [
BrowserModule,
CommonModule
],
providers: [NavigationComponent]
})
export class AppModule { }

但是,现在似乎有两个导航组件实例。我通过在NavigationComponentconstructor生成一个随机数并记录下来来检查这一点。该指令肯定有一个与<app-navigation>选择器中显示的实例不同的实例。

现在我知道这种模式以某种方式起作用。我前段时间发现它是由一些 Angular 开发人员介绍的,但不幸的是我不再有源代码了。然而,工作版本在AppComponent中显示内容,因此该指令只有对AppComponent的依赖,这似乎首先被初始化。因此,不会发生整个依赖问题。

如何确保提供给TopBarDirectiveNavigationComponent实例与<app-navigation>选择器中显示的实例相同?

我建议你为此创建一个服务说TopbarService,就像这样。在这里,我们将使用BehaviorSubject来设置模板并发出它的最新值。

@Injectable()
export class TopbarService {
private currentState = new BehaviorSubject<TemplateRef<any> | null>(null);
readonly contents = this.currentState.asObservable();
setContents(ref: TemplateRef<any>): void {
this.currentState.next(ref);
}
clearContents(): void {
this.currentState.next(null);
}
}

现在在指令中注入此服务并调用服务方法。

@Directive({
selector: '[appTopbar]',
})
export class TopbarDirective implements OnInit {
constructor(private topbarService: TopbarService,
private templateRef: TemplateRef<any>) {
}
ngOnInit(): void {
this.topbarService.setContents(this.templateRef);
}
}

NavigationComponent组件中订阅内容行为主题以获取最新值并设置模板。

export class NavigationComponent implements OnInit, AfterViewInit {
_current: EmbeddedViewRef<any> | null = null;
@ViewChild('vcr', { read: ViewContainerRef })
vcr: ViewContainerRef;
constructor(private topbarService: TopbarService,
private cdRef: ChangeDetectorRef) {
}
ngOnInit() {
}
ngAfterViewInit() {
this.topbarService
.contents
.subscribe(ref => {
if (this._current !== null) {
this._current.destroy();
this._current = null;
}
if (!ref) {
return;
}
this._current = this.vcr.createEmbeddedView(ref);
this.cdRef.detectChanges();
});
}
}

此组件的 HTML 将如下所示,您放置模板的位置。

template: `
<div class="full-container topbar">
<ng-container #vcr></ng-container>
<h1>Navbar</h1>
</div>
`,

要将控制器注入其指令,请使用 forwardRef。

组件定义

@Component({
//...,
providers:[
{
provide: MyController,
useExisting: forwardRef(() => MyController)
}]
})
export class MyController {
//...
}

指令定义

@Directive({
//...
})
export class MyDirective {
constructor(private ctlr: MyController) { }
}

该构造函数可能需要一个 @Host((;我还没有测试过这段代码。

最新更新