在角度组件继承期间键入错误

时间:2018-06-12 12:00:24

标签: javascript angular typescript inheritance

我在继承过程中遇到了错误,我不知道如何克服这个问题,这是一个简单的例子:

// just a class
abstract class AbstractExample<T> {
  public readonly items$ = this.getItems$();
  protected abstract getItems(): Observable<Array<T>>
}

@Component(...)
class Example extends AbstractExample<User> {
  constructor(protected readonly store: Store) { super(); }
  protected getItems() {
    return this.store.select(...);
  }
}

所以ES5目标上生成的JavaScript代码看起来类似于:

var Example = /** @class */ (function (_super) {
        __extends(Example, _super);
        function Example(store) {
            var _this = _super.call(this) || this;
            _this.store = store;
        return _this;
    }
    Example.prototype.getItems$ = function () {
        return this.store.select(...);
    };
    return Example;
}(shared_feature_1.AbstractExample));

正如您可能会注意到_super.callstore转让this之前TypeError: this.store is undefined导致getItems$方法执行期间const getProducts = (state) => state.products; const getCart = (state) => state.cart; const getProductId = (state, props) => props.productId; const getLineItem = createSelector( [getProducts, getCart, getProductId], (products, cart, productId) => ({ accumulatedPrice: cart[productId].quantity * products[productId].price, id: productId, image: products[productId].image, price: products[productId].price, quantity: cart[productId].quantity || 0 }) )

我怎么能克服这个?

0 个答案:

没有答案