【问题标题】:Typescript: ReferenceError: Cannot access 'Store' before initialization打字稿:ReferenceError:初始化前无法访问“存储”
【发布时间】:2019-10-18 04:51:48
【问题描述】:

我有一个类 Store,它封装了 State(使用 mobx)。

export class Store<State> {
    @observable
    public state: State;

    constructor(protected rootStore: RootStore, state: State) {
        this.state = state || ({} as State);
    }

    @action
    setState(state: State) {
        this.state = {
            ...this.state,
            ...state
        };
    }
}

我正在尝试实现一个类UserState

interface UserState {
    authorised?: boolean;
    loading?: boolean;
    name?: string;
    balance?: number;
}

export class UserStore extends Store<UserState> {
    constructor(rootStore: RootStore) {
        super(rootStore, {
            authorised: false,
            loading: true,
            name: ''
        })
    }
}

一切似乎都适合我,但我有一个错误:

ReferenceError: Cannot access 'Store' before initialization

我只是尝试在商店中设置一些默认值,它似乎在 Store 中,它位于构造函数中,因此显然已初始化。

【问题讨论】:

    标签: typescript oop inheritance


    【解决方案1】:

    问题已通过将Store 类移动到独立文件中解决,然后它与全局存储位于同一文件中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-12
      • 2020-12-29
      • 2021-09-10
      相关资源
      最近更新 更多