【发布时间】:2018-07-19 11:59:57
【问题描述】:
我在玩 Mobx-state-tree。
在我构建的一个简单模型中,我收到此错误“未捕获的类型错误:无法读取未定义的属性‘名称’”。
我正在粘贴所有代码块。
索引.js
import React,{Component} from "react";
import ReactDOM from 'react-dom';
import App from "./app";
import Invoice from "../models/Invoice"
const invoice = Invoice.create({currency:'EUR'}) ;
ReactDOM.render(<App invoice={invoice}/>,document.getElementById('root'));
app.Js
import React,{Component} from "react";
import {observer} from "mobx-react"
class App extends Component
{
render(){
const {invoice} = this.props;
return(
<div className="container-fluid">Invoicer
<h1>{invoice.status()} </h1>
{!invoice.isPaid && <button onClick={invoice.markPaid}>Pay</button>}
</div>
);
}
}
export default observer(App);
模型/Invoice.js
import {types} from "mobx-state-tree";
import ItemList from "./ItemList";
const Invoice = types.model("Invoice",{
currency : types.string,
isPaid:false,
itemList : types.optional(ItemList,{items:[]})
})
.actions( self => ({
markPaid(){
self.isPaid = true;
}
}))
.views( self => ({
status(){
return self.isPaid ? "Paid,Go Home and Enjoy" :"Not Paid,work more and we will think about it";
}
}));
export default Invoice;
ItemList.js
import {types} from "mobx-state-tree";
import {Item} from "./Item";
const ItemList = types.model("ItemList",{
items: types.array(Item)
});
export default ItemList;
item.js
import {types} from "mobx-state-tree"
const Item = types.model("Item",{
quantity : types.number,
price: types.number,
comodityName: types.string
});
export default Item;
使用 npm 编译时终端没有错误。在 bundle.js 中 错误在这一行
array: function(e) {
return new nt(e.name + "[]",e)
},
我不确定为什么会出现这个错误?
【问题讨论】:
-
您是否在 MobX 4 中使用 MobX 状态树?
-
mobx 的版本:5.0.3 和 mobx-state-tree :3.0.0
-
mobx 和 MobX 有什么区别?
-
没有区别。我打算写 MobX 4。MST 现在似乎已经增加了对 MobX 5 的支持,所以这应该不是问题。
-
我依赖 mobx-react: 5.2.3 。如果安装了 mobx 4 版本,应该安装哪个版本的 mobx-react
标签: javascript reactjs mobx mobx-state-tree