gpt4 book ai didi

typescript - vue类组件继承,方法来自基类 "is not a function"

转载 作者:行者123 更新时间:2023-12-04 09:26:02 26 4
gpt4 key购买 nike

我正在尝试制作 class inheritance defining stories for storybook 时避免代码冗余因为我有一堆场景(故事)来涵盖更少/更多的数据相似性。我正在使用带有 vue-property-decorator 的 vue-class-component 方法定义组件。
这是一个例子:

//myStory.stories.ts file
import Vue from 'vue'
import {Component} from 'vue-property-decorator'
import {store} from 'path/to/stores'
import Vuex from 'vuex'

const BaseOptions = {
components: {Component1, Component2},
template: MyTemplate,
store: new Vuex.Store<IRootState>(store)
}

class BaseClass extends Vue {
public method1() {
console.log("call from method 1")
}
// I want to customize many methods here in BaseClass: method2, method3, etc
}

// Story book page
export default {
title: 'My/Page',
// Our exports that end in "Data" are not stories.
excludeStories: /.*Data$/,
}

// Defining storybook stories:
export const ScenarioOne = () => (
@Component<any>(
{
...BaseOptions,
mounted() {
this.method1()
},
}
)
class CurrentClass extends BaseClass {} // This isn't working >> error: _this4.method1 is not a function
)


export const ScenarioTwo = () => (
@Component<any>(
{
...BaseOptions,
mounted() {
this.method1()
},
}
)
class CurrentClass extends BaseClass {
// If I redefine the method1(), it's working
public method1() {
console.log("call from method 1 (redefined)")
}
}
)

为什么我的 scenarioOne无法调用 method1()scenarioTwo可以 ?

最佳答案

您还必须将基类定义为 @Component({}) 以及继承链中的所有类,因此:

import Vue from "vue";
import Component from "vue-class-component";

@Component({})
class BaseClass extends Vue {
}

@Component({})
class SubClass extends BaseClass {
}

关于typescript - vue类组件继承,方法来自基类 "is not a function",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63004313/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com