小编Mic*_*Lis的帖子

接口中方法的默认实现

有没有办法在接口中默认实现方法?不幸的是,我无法在基类中实现它。我觉得这是一个非常简单的问题,但在很长一段时间内找不到正确的解决方案。

/edit 在我的例子中,我需要这样的东西:

class A {
  somePropertyA;
  someFunctionA() {
    console.log(this.somePropertyA);
  }
}
class B {
  somePropertyB;
  someFunctionB() {
    console.log(this.somePropertyB);
  }
}
class C {
  // Here we want to have someFunctionA() and someFunctionB()
  // without duplicating code of this implementation.
}
Run Code Online (Sandbox Code Playgroud)

B entends A 和 C extends B 的解决方案对我来说并不是那么理想。

typescript

7
推荐指数
1
解决办法
8780
查看次数

Angular ViewChildren 不会立即看到 ngFor 中的所有子级

我有一个奇怪的行为 @ViewChildren 对应于 ngFor 生成的子组件。@ViewChildren 查询没有看到元素在数组中停留了很长时间。我的所有代码都在Plunker中- 请在打开控制台的情况下查看。

这是我的主要组成部分:

@Component({
    selector: 'my-app',
    template: `
        <button (click)="addInternalComponent()">Add internal component</button>
        <app-internal #internals *ngFor="let i of indexes" [index]="i
(afterViewInit)="onAfterViewInit()"></app-internal>
    `,
})
export class App {
    @ViewChildren('internals') internals: QueryList<InternalComponent>;
    indexes = [];
    addInternalComponent() {
        console.log('adding internal component!');
        this.indexes.push(this.indexes.length);
        console.log('Not complete list', this.internals._results);

    }

    onAfterViewInit() {
        console.log('onAfterViewInit - still not complete list', this.internals._results);
    }
}
Run Code Online (Sandbox Code Playgroud)

我们可以通过单击按钮添加其中的子组件。

在将一个元素添加到索引数组中以生成 ngFor 循环中的所有子元素之后 - 我们没有刚刚添加的那个元素。

我可以理解这种行为 - 因为也许子组件需要一些时间来创建它,并且引擎决定在创建完整的子组件之前控制台日志。

但是,我在子组件中创建了发射器,表示子组件的视图和模型都已初始化。但是......在父组件中处理这个事件 - 我们仍然没有这个项目。这怎么可能?

子组件:

export class InternalComponent implements AfterViewInit { …
Run Code Online (Sandbox Code Playgroud)

ngfor viewchild angular

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

标签 统计

angular ×1

ngfor ×1

typescript ×1

viewchild ×1