[英]customized parameters to pass to super in a derived angular class
I am trying to use the ol sidebar from umbe1987/Turbo87 in an angular project.我正在尝试在 angular 项目中使用umbe1987/Turbo87的 ol 侧边栏。
I am extending a class but I need to transform constructor parameters in the derivedclass constructor before passing them to the superclass constructor.我正在扩展 class 但我需要在派生类构造函数中转换构造函数参数,然后再将它们传递给超类构造函数。
I have to catch a dom div node from the current angular component (derivedclass) and pass it as the 'element' argument in the super call.我必须从当前的 angular 组件(派生类)中捕获一个 dom div 节点,并将其作为超级调用中的“元素”参数传递。
If I let the already existing var element = document.getElementById(options.element)
the dom is actually not being rendered by the time of constructor.如果我让已经存在的
var element = document.getElementById(options.element)
dom 实际上没有被构造函数渲染。
If I go var element = this.mySidebarDiv.nativeElement(options.element)
I get the following message: this'super' must be called before accessing 'this' in the constructor of a derived class.ts(17009)
如果我 go
var element = this.mySidebarDiv.nativeElement(options.element)
我收到以下消息: this'super' must be called before accessing 'this' in the constructor of a derived class.ts(17009)
How can overpass the warn when calling the viewChild property (this.mySidebarDiv)?调用 viewChild 属性 (this.mySidebarDiv) 时如何越过警告? This restriction is making me to pass a
null
element parameter.这个限制让我传递一个
null
元素参数。
I know the sidebar is actually will not be rendered until the specific lifecycle hook (ngAfterViewInit), but I need to catch the dom element before, in its constructor, to pass it to super conctructor, so how can I approach this?我知道侧边栏实际上直到特定的生命周期钩子(ngAfterViewInit)才会呈现,但是我需要在其构造函数中捕获 dom 元素之前将其传递给超级构造函数,那么我该如何处理呢?
Thank you谢谢
@Injectable()
export class ProjectInfoSidebarComponent extends Control {
@ViewChild('sidebar') mySidebarDiv: ElementRef;
_container;
_tabitems;
_panes;
_closeButtons;
classList;
_sidebar;
constructor(@Inject(String)opt_options) {
var defaults = {
element: null,
position: 'left'
}, i, child;
var options = Object.assign({}, defaults, opt_options);
//var element = document.getElementById(options.element);
var element = this.mySidebarDiv.nativeElement(options.element)
super({
element: element,
target: options.target
});
// super((() => {
// return {
// element: this.mySidebarDiv.nativeElement.querySelector(options.element),
// target: options.target
// }
// })());
// Attach .sidebar-left/right class
element.classList.add('sidebar-' + options.position);
...
...
...
Instead of trying to combine ol5-sidebar
and a custom Angular component into a single class, you should invert control and create a wrapper Angular component that creates a <div>
and hands it over to an OLSidebar
instance once it's rendered to the DOM.与其尝试将
ol5-sidebar
和自定义 Angular 组件组合到单个 class 中,不如将控制反转并创建一个包装器 Angular 组件,然后将其呈现给创建一个<div>
的OLSidebar
实例。
Copy the ol5-sidebar.js
file into your project directly from the Github repo you linked, then create a separate project-info-sidebar.component.ts
like this:将
ol5-sidebar.js
文件直接从您链接的 Github 存储库复制到您的项目中,然后创建一个单独的project-info-sidebar.component.ts
,如下所示:
import { Component, ViewChild, ElementRef, AfterViewInit, Inject, InjectionToken } from "@angular/core";
import OLSidebar from "./ol5-sidebar.js";
const SIDEBAR_OPTS = new InjectionToken<SidebarOptions>('SIDEBAR_OPTS');
interface SidebarOptions {
/* default: `left` */
position?: string;
target: any; // not sure what the correct type is here
}
@Component({
selector: "sidebar",
template: "<div #sidebar></div>",
})
export class ProjectInfoSidebarComponent implements AfterViewInit {
@ViewChild("sidebar") mySidebarDiv: ElementRef<HTMLDivElement>;
sidebar: OLSidebar | null = null;
constructor(@Inject(SIDEBAR_OPTS) private options: SidebarOptions) {}
ngAfterViewInit() {
// get a reference to the `<div>` we just rendered to the DOM
const element = this.mySidebarDiv.nativeElement;
// create an OLSidebar instance, which handles everything from here
this.sidebar = new OLSidebar({
element,
target: this.options.target,
});
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.