gpt4 book ai didi

angular - 不能在 Angular 6 的子模块中使用在应用程序模块内声明的指令

转载 作者:太空狗 更新时间:2023-10-29 17:45:01 25 4
gpt4 key购买 nike

我正在使用 Angular 6

我通过扩展 NbgPopover 声明了一个指令 StickyPopover 并在 app.module.tsdeclaration 中添加了>

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';

import { AppComponent } from './app.component';
import { AppRoutingModule } from './app-routing.module';
import { AuthLayoutComponent } from './layouts/auth-layout/auth-layout.component';
import {AdminLayoutComponent} from './layouts/admin-layout/admin-layout.component';
import {FormsModule} from '@angular/forms';
import {RouterModule} from '@angular/router';
import {BrowserAnimationsModule} from '@angular/platform-browser/animations';
import {NgbModule} from '@ng-bootstrap/ng-bootstrap';
import {StickyPopoverDirective} from './sticky-popover.directive';


@NgModule({
declarations: [
AppComponent,
AuthLayoutComponent,
AdminLayoutComponent,
StickyPopoverDirective // custom created directive
],
imports: [
BrowserModule,
AppRoutingModule,
BrowserAnimationsModule,
FormsModule,
HttpClientModule,
RouterModule,
NgbModule.forRoot(),
],
providers: [],
bootstrap: [AppComponent],
})
export class AppModule { }

并且该指令的位置与app.module.ts文件的位置相同。

sticky-popover.directive.ts 的内容是

import {
ElementRef,
Directive, Input, TemplateRef,
EventEmitter,
Renderer2,
Injector,
ComponentFactoryResolver,
ViewContainerRef,
NgZone, OnInit, OnDestroy
} from '@angular/core';

import { NgbPopover, NgbPopoverConfig } from '@ng-bootstrap/ng-bootstrap';

@Directive({
selector: '[appStickyPopover]',
exportAs: 'stickyPopover'
})
export class StickyPopoverDirective extends NgbPopover implements OnInit, OnDestroy {
@Input() stickyPopover: TemplateRef<any>;

popoverTitle: string;

placement: 'auto' | 'top' | 'bottom' | 'left' | 'right' | 'top-left' | 'top-right' | 'bottom-left' |
'bottom-right' | 'left-top' | 'left-bottom' | 'right-top' | 'right-bottom' | ('auto' | 'top' | 'bottom' |
'left' | 'right' | 'top-left' | 'top-right' | 'bottom-left' | 'bottom-right' | 'left-top' | 'left-bottom' |
'right-top' | 'right-bottom')[];

triggers: string;

container: string;

shown: EventEmitter<{}>;

hidden: EventEmitter<{}>;

ngpPopover: TemplateRef<any>;

canClosePopover: boolean;

toggle(): void {
super.toggle();
}

isOpen(): boolean {
return super.isOpen();
}



constructor(
private _elRef: ElementRef,
private _render: Renderer2,
injector: Injector,
componentFactoryResolver: ComponentFactoryResolver,
private viewContainerRef: ViewContainerRef,
config: NgbPopoverConfig,
ngZone: NgZone
) {
super(_elRef, _render, injector, componentFactoryResolver, viewContainerRef, config, ngZone, document);
this.triggers = 'manual';
this.popoverTitle = 'Permissions';
this.container = 'body';
}

ngOnInit(): void {
super.ngOnInit();
this.ngbPopover = this.stickyPopover;

this._render.listen(this._elRef.nativeElement, 'mouseenter', () => {
this.canClosePopover = true;
this.open();
});

this._render.listen(this._elRef.nativeElement, 'mouseleave', (event: Event) => {
setTimeout(() => { if (this.canClosePopover) { this.close(); } }, 100);

});

this._render.listen(this._elRef.nativeElement, 'click', () => {
this.close();
});
}

ngOnDestroy(): void {
super.ngOnDestroy();
}

open() {
super.open();
const popover = window.document.querySelector('.popover');
this._render.listen(popover, 'mouseover', () => {
this.canClosePopover = false;
});

this._render.listen(popover, 'mouseout', () => {
this.canClosePopover = true;
setTimeout(() => { if (this.canClosePopover) { this.close(); } }, 0);
});
}

close() {
super.close();
}

}

我有一个模块 SavedSearches,它在 AdminModule 中导入,在 app.module.ts

中进一步声明

当我在SavedSearches 模块的模板中使用StickyPopover 指令时

<i class="fa fa-plus-circle" aria-hidden="true" appStickyPopover [popoverTitle]="additional" [autoClose]="true" data-placement="right"></i>

它给出错误为

can't bind to 'popoverTitle' since it isn't a known property of i

当我将指令移入 SavedSearches 模块并将其包含在 saved-searches.module.ts declaration 中时,它工作正常没有任何错误。

但是我不能在另一个模块中使用它,在其他模块中使用它会给出

StickyPopovoerDirective is a part of the declaration of 2 modules. Move it in the upper module which imports these two modules.

最佳答案

该指令仅对 AppModule 可见,如果您想将它用于其他模块,您可以创建一个 SharedModule。然后将 StickyPopoverDirective 添加到声明和导出中。

@NgModule({
declarations: [StickyPopoverDirective],
exports: [StickyPopoverDirective]
})
export class SharedModule { }

在此之后,您可以将 SharedModule 导入另一个模块并在那里使用您的指令。

关于angular - 不能在 Angular 6 的子模块中使用在应用程序模块内声明的指令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52640419/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com