【问题标题】:ExtJS inherit configExtJS 继承配置
【发布时间】:2021-11-01 01:56:45
【问题描述】:

在 ExtJS 6.2 中,子类是否继承父配置?

它似乎对我不起作用。

Ext.define('test1', {
    config: {
        test1: {
            test1: ''
        }
    },

    constructor(config) {
        Ext.apply(this, config);

        console.log(config);
    },
});

Ext.define('test2', {
    extend: 'test1',

    config: {
        test2: ''
    },

    constructor(config) {
        this.callParent(config);

        console.log(this.config);
    },
});

Ext.create('test2', {
    test1: {
        test1: 'test1'
     },
     test2: 'test2'
});

似乎在父构造函数上configundefined

我希望在父类配置中定义的所有内容也可以在子类中使用。并且可以在创建子类时设置父配置。

小提琴:https://fiddle.sencha.com/#view/editor&fiddle/3fud

【问题讨论】:

    标签: javascript inheritance extjs constructor subclass


    【解决方案1】:

    您必须通过 initConfig 获得 ExtJS 的典型配置。您可以在 test2 中获取所有配置。

    Ext.define('test1', {
        config: {
            test1: null
        },
    
        constructor(config) {
            this.initConfig(config);
    
            return this;
        }
    });
    
    Ext.define('test2', {
        extend: 'test1',
    
        config: {
            test2: ''
        },
    
        constructor(config) {
    // == > config
    
            this.callParent([config]);
    
    // ==> her you can even use getTest1(), setTest1() (or test2)
        }
    });
    
    Ext.create('test2', {
        test1: {
            test1: 'test1'
        },
        test2: 'test2'
    });
    

    【讨论】:

    • 但是我如何保持默认父定义的配置?如果我创建test2时没有通过test1配置怎么办?
    • 我希望 test1 中的所有内容都在 test2 中,包括配置。
    • 如果Fiddle 回答了您的问题,请告诉我
    • 答案中的更新代码
    • 是的,确实如此。我记得现在有问题,因为 config 参数必须是一个数组,这就是为什么它在 test1 构造函数上是 NULL
    【解决方案2】:

    一种解决方法是致电Ext.apply(this, config); 不确定这是否是正确的解决方案。

    Ext.define('test2', {
        extend: 'test1',
    
        config: {
            test2: ''
        },
    
        constructor(config) {
            this.callParent(config);
    
            Ext.apply(this, config);  // <-
    
            console.log(this.config);
    
            console.log(this.getTest1());
            console.log(this.getTest2());
        },
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-02-14
      • 1970-01-01
      • 2011-03-09
      • 1970-01-01
      • 1970-01-01
      • 2018-02-09
      • 2018-11-04
      相关资源
      最近更新 更多