Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/29.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 向类添加函数_Javascript_Angular_Typescript - Fatal编程技术网

Javascript 向类添加函数

Javascript 向类添加函数,javascript,angular,typescript,Javascript,Angular,Typescript,我有一个基础应用程序,可以安装到不同的客户,并向其中添加他们要求的功能 支持一个具有 component-base.html component-base.ts 现在,一个客户要求我自定义它,我所做的是复制这两个文件: client-base-component1.html client-base-component1.ts 在安圭拉,我告诉他 "fileReplacements": [ { "replace": "sr

我有一个基础应用程序,可以安装到不同的客户,并向其中添加他们要求的功能

支持一个具有

component-base.html
component-base.ts
现在,一个客户要求我自定义它,我所做的是复制这两个文件:

client-base-component1.html
client-base-component1.ts
在安圭拉,我告诉他

          "fileReplacements": [
            {
              "replace": "src / components / component-base.html",
              "with": "src / components / component-base-client1.html"
            },
            {
              "replace": "src / components / component-base.ts",
              "with": "src / components / component-base-client1.ts"
            },
然后我在那里添加了功能,但是由于最终有很多客户端,我复制了很多代码

在html中,我认为这是不可能的,但有没有可能不必复制component-base-client1.ts中的所有函数,导入所有代码,然后将我开发的新函数(并且只使用component-base-client1.html)放入新函数中,或者更好,是否能够用component-base-client1.ts中的函数覆盖component-base.ts中的函数,如果它们不在component-base-client1.ts中,是否使用component-base.ts

我知道在这里很难解释我在寻找什么,但更好地组织我的文件将非常有用


谢谢

您需要使用类继承

// Your base component
export class BaseComponent 
    public Function1(): void {
    }
}

// Your client component extending you BaseComponent (this is the inheritance)
export ClientComponent extends BaseComponent {

    // You can override BaseComponent Function1()
    public Function1(): void {
        // You can still call base Function1 on your overrided Function1
        super.Function1();
    }

    // You can add client specific functions
    public ClientSpecificFunction(): void {
    }
}
如果BaseComponent注入服务,则必须将其注入继承类构造函数中:

export class BaseComponent 

    // Your base component constructor
    constructor(public router: Router) {
    }
}

export ClientComponent extends BaseComponent {

    constructor(public router: Router) {
        super(router);
    }
}

谢谢。这正是我需要的,但我有个问题。我在app.module和app.routes中导入BaseComponent,如果我现在使用ClientComponent,那么我需要为每个客户端复制这些。。。。我需要app.module.customer和app.routes.customer并将其替换为angular.json或其他方式?您可以在
app.module
app.routes.customer中导入
app.routes
并在
angular.json
中替换它们。但这不是实现这一点的唯一方法,它实际上取决于您的应用程序体系结构。另一点是,您的组件/路由/模块文件不符合角度命名约定,我建议您看看这里:不要忘记将我的Answare标记为已接受;)。如果您有关于应用程序架构的更具体问题,我建议您打开一个新问题:)。