刚接触mobx,给我报个错,Support for the experimental syntax ‘decorators-legacy’ isn’t currently ,竟然不支持这个语法,后来查了资料解决了问题
第一步:
npm install @babel/plugin-proposal-decorators
npm install @babel/plugin-proposal-class-properties
第二步:
package.json中添加代码,(看注释)
"plugins": [
//添加代码开始
[
"@babel/plugin-proposal-decorators",
{
"legacy":true
}
],
[
"@babel/plugin-proposal-class-properties",
{
"loose":true
}
],
//添加代码结束
[
"import",
{
"libraryName": "antd",
"style": "css"
}
]
]
组件代码
// 歌手列表页面
import React , { Component } from 'react';
import { observable } from 'mobx';
import { observer } from 'mobx-react';
//注意装饰器位置
export default @observer class SingerPage extends Component {
@observable isLoading = true;
@observable LetterList = [];
@observable singerList = [];
componentDidMount(){
}
//省略逻辑代码....
render(){
return(
<>
{
this.props.isLoading
? <Loading/>
: <SingerView singerList={ this.singerList } LetterList = { this.LetterList} />
}
</>
)
}
}
亲测可用