相关疑难解决方法(0)

使用基类装饰器扩展组件装饰器

我有几个组件装饰器声明,我在每个组件上重复,例如:

@Component({
    moduleId: module.id,
    directives: [BootstrapInputDirective]
})
Run Code Online (Sandbox Code Playgroud)

如何将这些声明应用于我的所有组件?我试图用这个装饰器创建一个基类,并用它扩展其他类,但基类装饰似乎不适用于派生类.

typescript angular

19
推荐指数
4
解决办法
2万
查看次数

Angular2 @HostListener不适用于派生组件

我有一个基于Angular2的客户端应用程序.我有一个基类:

abstract class BaseClass {
    @HostListener('window:beforeunload') beforeUnloadHandler() {
        console.log('bla');
    }
}
Run Code Online (Sandbox Code Playgroud)

和两个非常相似的派生类:

@Component({
    selector:  'derived-one',
    templateUrl:  './templates/app/+derived-one/derived-one.component.html'
})
export class DerivedOne extends BaseClass {
}

@Component({
    selector:  'derived-two',
    templateUrl:  './templates/app/+derived-two/derived-two.component.html'
})
export class DerivedTwo extends BaseClass {
}
Run Code Online (Sandbox Code Playgroud)

问题在于,例如,DerivedOne beforeUnloadHandler工作正常,而在DerivedTwo其中根本不接收电话.

我知道很难找到为什么只是查看上面的信息,但也许有人可能会怀疑可能导致这种奇怪行为的原因.

还有一些说明:

如果我使用以下内容:

abstract class BaseClass
    constructor(){
        window.onbeforeunload = function(){
            console.log('bla');
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

一切正常,但我仍然想找到一个基于Angular2的解决方案;

如果我写

abstract class BaseClass {
    beforeUnloadHandler() {
        console.log('bla');
    }
}
Run Code Online (Sandbox Code Playgroud)

并在 derived-two.component.html

<div (window.beforeunload)="beforeUnloadHandler()"></div>
Run Code Online (Sandbox Code Playgroud)

一切都很好,但它看起来像一个丑陋的黑客;

如果我写的话

abstract class BaseClass {
    beforeUnloadHandler() …
Run Code Online (Sandbox Code Playgroud)

typescript angular2-template angular

6
推荐指数
1
解决办法
3122
查看次数

继承的角度2分量中的输入属性

我试图通过继承一些具有一些Input属性的基类来实现angular 2的组件(我已使用此示例作为实现angular 2组件的继承的指南)。

简化示例

export class ComponentBase {
  @Input() text: string = "base";
}

@Component({
  selector: "derived-comp",
  templateUrl: "derived-comp.template.html",
  providers: [
    { provide: ComponentBase, useExisting: forwardRef(() => DerivedComponent) }
  ]
})
export class DerivedComponent extends ComponentBase {
  @Input() dummy: number;
}
Run Code Online (Sandbox Code Playgroud)

一切都按预期工作,直到我在派生组件中添加了一个新的Input属性(虚拟,在提供的示例中)。当我这样做时,基类中定义的所有Input属性在运行时都变得不可见(尽管在编译时一切正常),并且浏览器控制台中出现错误,指出这些输入不存在:

无法绑定到“文本”,因为它不是“ derived-comp”的已知属性。

有没有人在角度2中有类似的组件继承问题?是否有人对如何克服它有建议(除了复制粘贴代码外)?

javascript inheritance typescript angular

5
推荐指数
1
解决办法
942
查看次数