无法从Mobx导入装饰

问题描述

尝试的导入错误:“装饰”未从“ mobx”导出。 我的mobx版本是6.0,我试图将软件包从mobx更改为mobx-react,mobx-react-lite,mobx-decorate。但是仍然无法解决

谢谢enter image description here

解决方法

decorate API已在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++
    }
}

还有新事物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
    }
}

此处有更多信息:https://mobx.js.org/react-integration.html

,

我一直在使用mobx,可以导入装饰。我的mobx版本是5.9.4