无法从 mobx 导入装饰

Rup*_*Sri 6 decorator node.js reactjs mobx

尝试导入错误:“装饰”未从“mobx”导出。我的mobx版本是6.0,我尝试将包从mobx改成mobx-react、mobx-react-lite、mobx-decorate。但是还是无法解决。

提前致谢

截屏

Dan*_*ila 8

decorateAPI 已在 MobX 6 中移除,需要makeObservable在目标类的构造函数中替换。它接受相同的参数。

例子:

import { makeObservable, observable, computed, action } from "mobx"

class Doubler {
    value

    constructor(value) {
        makeObservable(this, {
            value: observable,
            double: computed,
            increment: action
        })
        this.value = value
    }

    get double() {
        return this.value * 2
    }

    increment() {
        this.value++
    }
}
Run Code Online (Sandbox Code Playgroud)

还有新东西makeAutoObservable,你甚至不需要使用装饰器:

import { makeAutoObservable } from "mobx"

class Timer {
    // You don't even need to use decorators anymore
    secondsPassed = 0

    constructor() {
        // Call it here
        makeAutoObservable(this)
    }

    increaseTimer() {
        this.secondsPassed += 1
    }
}
Run Code Online (Sandbox Code Playgroud)

更多信息在这里:

https://mobx.js.org/react-integration.html

https://mobx.js.org/migrating-from-4-or-5.html