我正在玩 Mobx 状态树。
在我建立的一个简单模型中,我收到了这个错误“Uncaught TypeError: Cannot read property 'name' of undefined”。
我正在粘贴所有代码块。
索引.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'));
应用程序.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;
项目列表.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)
},
我不确定为什么会发生此错误?