Vue.js inheritance call parent method

前端 未结 3 1303
生来不讨喜
生来不讨喜 2021-02-05 07:28

is it possible to use method overriding in Vue.js?

var SomeClassA = Vue.extend({
    methods: {
        someFunction: function () {
            // ClassA some st         


        
3条回答
  •  春和景丽
    2021-02-05 08:03

    In case someone asks for a solution here is mine and works fine :

    var SomeClassA = {
        methods: {
            someFunction: function () {
                this.defaultSomeFunction();
            },
            // defaultSomeFunction acts like parent.someFunction() so call it in inheritance
            defaultSomeFunction: function () {
                // ClassA some stuff
            },
        },
    };
    
    var SomeClassB = {
        extends: SomeClassA,
        methods: {
            someFunction: function () {
                // Replace the wanted SomeClassA::someFunction()
                this.defaultSomeFunction();
                // Add custom code here
            },
        },
    };
    

    using juste extends from https://vuejs.org/v2/api/#extends replaces the usage of Vue.extends()

提交回复
热议问题