如何导入JS文件并在打字稿中覆盖其功能之一

问题描述 投票:0回答:1

我最近开始使用Angular和Typescript。我的问题类似于:Overriding methods from JavaScript function-based classes in TypeScript

OP明确了要重写的实现和方法。就我而言,我正在使用一个第三方库,该库的文件dropdown.js与我在Angular项目中使用的DropDown组件有关。他们的js代码有一个错误。我已经多次报告给他们,但似乎他们根本不在乎。有什么办法可以覆盖我的打字稿中的方法并修复该错误。这是their的实现:

dropdown.js

"use strict";
var __extends = (this && this.__extends) || (function() {
    var extendStatics = Object.setPrototypeOf ||
        ({ __proto__: [] }
            instanceof Array && function(d, b) { d.__proto__ = b; }) ||
        function(d, b) {
            for (var p in b)
                if (b.hasOwnProperty(p)) d[p] = b[p];
        };
    return function(d, b) {
        extendStatics(d, b);

        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
var dropdown_base_1 = require("./dropdown.base");
require("./dropdown.scss");
var DropDownComponent = /** @class */ (function(_super) {
    __extends(DropDownComponent, _super);

    function DropDownComponent(_control, popup, _renderer, element) {
        ...
    }

    // ******* THIS METHOD I WANT TO OVERRIDE*********
    DropDownComponent.prototype.writeValue = function(value) {
        if (this.options) {
            this.selectOptionByValue(value);
        }
    };

    return DropDownComponent;
}(dropdown_base_1.DropDownBase));
exports.DropDownComponent = DropDownComponent;

而且我的打字稿文件非常简单:

mymenu.component.ts

import { Component, OnInit } from '@angular/core';
import {DropDowncomponent} from './../lirbray/dropdown'

@Component({
    selector: 'app-mymenu',
    templateUrl: './mymenu.component.html'
})
export class MyMenuComponent implements OnInit {
    constructor() {}

    ngOnInit(): void {
      this.translateModes();
    }
}

如何导入他们的JavaScript并覆盖此方法:

    DropDownComponent.prototype.writeValue = function(value) {
        if (this.options) {
            this.selectOptionByValue(value);
        }
    };

我只需要向该代码添加一行:this.clearSelectedOptions();

这是否可行,或者我在问愚蠢的问题。请更正我,因为我来自C,C ++背景,并且是javascript新手。自1个月以来,我已因为此错误而被阻止。请帮助我。

javascript typescript ecmascript-6 ecmascript-5
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.