【发布时间】:2018-10-02 05:26:51
【问题描述】:
我有一个 React 应用程序正在运行,我正在尝试掌握装饰器的窍门。它们导致意外的令牌错误。
我正在使用 transform-decorators-legacy 和 transform-class-properties babel 插件。
我已经尝试了 stage-0 和 stage-2 预设。 SO和其他地方的所有相关帖子似乎都认为插件应该使它工作。有人说使用 stage-0 预设,而其他人推荐使用 stage-2。
这是带有装饰器的文件:
import React, {Component} from 'react';
import Actions from '../firebase/actions';
import RecipeList from '../Recipe/recipe-list';
import connectToStores from 'alt-utils/lib/connectToStores';
import RecipeStore from '../../store/recipe-store';
@connectToStores
class Main extends Component {
constructor() {
super();
Actions.getRecipes();
}
static getStores() {
return [RecipeStore];
}
static getPropsFromStores() {
return RecipeStore.getState();
}
render() {
return (
<section>
<section className="container">
{
this.props.recipes
?
<RecipeList recipeList={this.props.recipes}/>
:
null
}
</section>
</section>
);
}
}
export default Main;
这是 package.json 文件:
{
"name": "glutenfreehacker",
"version": "0.1.0",
"private": true,
"dependencies": {
"babel-plugin-transform-class-properties": "^6.24.1",
"babel-plugin-transform-decorators-legacy": "^1.3.4",
"babel-preset-env": "^1.6.1",
"babel-preset-react": "^6.24.1",
"babel-preset-stage-0": "^6.24.1",
"babel-preset-stage-2": "^6.24.1",
"firebase": "^4.12.1",
"react": "^16.3.2",
"react-dom": "^16.3.2",
"react-redux": "^5.0.7",
"react-router-dom": "^4.2.2",
"react-scripts": "1.1.4",
"recompose": "^0.26.0",
"redux": "^4.0.0"
},
"babel": {
"presets": [
"env",
"stage-2",
"react"
],
"plugins": [
"transform-decorators-legacy",
"transform-class-properties"
]
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test --env=jsdom",
"eject": "react-scripts eject"
}
}
我知道这已在此处和其他地方进行了讨论。但是,我看到的解决方案建议使用上述插件和预设,但我仍然无法使用。
感谢您的阅读,非常感谢您的帮助。
【问题讨论】:
-
你试过给装饰器加括号吗? @connectToStores()
-
刚刚尝试过,我得到了同样的错误。谢谢。
-
装饰器也没有任何意义,只要它不是导出的一部分。例如:导出默认类 Main,而不是自己拥有默认导出
-
@thsorens 在单独的行上导出不会改变这种情况下的行为。