我可以用自定义Getter观察到MOBX吗?

发布于 2025-01-26 03:53:09 字数 317 浏览 3 评论 0原文

我需要一个可观察到的对象,该对象只是将其归还为字符串的任何属性。例如,myobs.description将简单地返回字符串“描述”。使用普通的JS对象,我将使用具有自定义Getter的代理使用,例如:

    new Proxy({}, {
      get: (obj, prop) => {
        return prop.toString();
    }})

我无法使用MOBX进行此操作,因为API使用了无法更新的代理对象。 有没有一种方法可以使用MOBX创建一个可观察到的对象,该对象可以为传递给其的任何属性具有自定义计算值吗?

I have a need for a Mobx observable object which simply returns whichever property is asked of it as a string. For example, myObs.description would simply return the string "description". With a normal JS object, I would use a proxy with a custom getter, like so:

    new Proxy({}, {
      get: (obj, prop) => {
        return prop.toString();
    }})

I can't do this with Mobx as the API uses a Proxy object that I can't update.
Is there a way to use Mobx to create an observable object that can have a custom computed value for any and every property passed to it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文