我正在关注文档,mobx-react-router但是尝试运行我的应用程序时,在浏览器中出现以下错误:
Uncaught TypeError: An element descriptor's .kind property must be either "method" or "field", but a decorator created an element descriptor with .kind "undefined"
at _toElementDescriptor (app.js:49988)
at _toElementFinisherExtras (app.js:49990)
at _decorateElement (app.js:49980)
at app.js:49976
at Array.forEach (<anonymous>)
at _decorateClass (app.js:49976)
at _decorate (app.js:49958)
at Module../src/App/UserStore.js (app.js:50012)
at __webpack_require__ (bootstrap:19)
at Module../src/index.js (index.js:1)
Run Code Online (Sandbox Code Playgroud)
这是我初始化的方式:
const appContainer = document.getElementById('app');
if(appContainer) {
const browserHistory = createBrowserHistory()
const routingStore = new RouterStore();
const stores = {
users: userStore,
routing: routingStore
}
const history = syncHistoryWithStore(browserHistory, routingStore);
ReactDOM.render(
(
<Provider {...stores}>
<Router history={history}>
< App />
</Router>
</Provider>
),
appContainer);
}
Run Code Online (Sandbox Code Playgroud)
这就是我的用法:
@inject('routing')
@inject('users')
@observer
class App extends Component { ...
Run Code Online (Sandbox Code Playgroud)
我的UserStore:
import { observable, action, computed } from "mobx"
class UserStore {
@observable users = [];
@action addUser = (user) => {
this.users.push(user)
}
@computed get userCount () {
return this.users.length
}
}
const store = new UserStore();
export default store;
Run Code Online (Sandbox Code Playgroud)
我曾尝试向Google发送此错误,但未返回任何有用的结果。有什么想法我做错了吗?
如果您使用的是Babel 7,则对装饰器安装支持:
npm i -D\
@babel/plugin-proposal-class-properties\
@babel/plugin-proposal-decorators
Run Code Online (Sandbox Code Playgroud)
然后在您的.babelrc或webpack.config.js文件中启用它:
{
"plugins": [
["@babel/plugin-proposal-decorators", { "legacy": true}],
["@babel/plugin-proposal-class-properties", { "loose": true}]
]
}
Run Code Online (Sandbox Code Playgroud)
请注意,传统模式很重要(将装饰器提案放在首位)。非旧版模式是WIP。
参考:https : //mobx.js.org/best/decorators.html
| 归档时间: |
|
| 查看次数: |
1345 次 |
| 最近记录: |