将lodash用于MobX商店

时间:2019-02-24 17:35:12

标签: javascript reactjs lodash mobx

我正在使用<template> <div> Test2 <button @click="testFunction">click me</button> </div> </template> <script> export default { data() { return { testFunction: null, value: "Test2" }; }, created() { this.testFunction = this.aFunction.bind(this); }, props: { aFunction: { required: true, type: Function } } }; </script> lodash来检索数据,看起来像这样:

mobx

工作正常,但是在_.get(store, "prop.arr[0].subProp") mobx或为空的情况下,arr会给我警告:

undefined

有什么解决方法吗?我不想添加长度检查,而是希望将其保持为单线。

1 个答案:

答案 0 :(得分:2)

在将store传递到get之前,先由mobx传递toJS实用程序。这应该可以解决。

所以您的班轮号码是-_.get(toJS(store), "prop.arr[0].subProp")

这里toJS的更多信息-https://mobx.js.org/refguide/tojson.html