如何命名 es6 类(用于 React 组件) [英] How to namespace es6 classes (for React components)
问题描述
这是 ES6 问题和 React 问题的一部分.我正在尝试在带有 ES6 类的 React 中使用 命名空间组件巴别塔.所以我想真正的问题是如何命名空间 es6 类,以便我可以做这里解释的事情:https://facebook.github.io/react/docs/jsx-in-depth.html#namespaced-components
This is part ES6 question part React question. I'm trying to use namespaced components in React with ES6 classes and Babel. So I guess the real question is how to name space es6 classes so I can do what is explained here: https://facebook.github.io/react/docs/jsx-in-depth.html#namespaced-components
由于我收到意外的令牌错误:
Since I get an unexpected token error:
class Headline extends Component { ... }
class Headline.Primary extends Component { ...
^
推荐答案
这在 ES6 中并没有真正改变,你仍然需要做一个作业:
This doesn't really change with ES6, you still will have to do an assignment:
Headline.Primary = class Primary extends Component { … };
然而,使用像 Headline
这样的类作为命名空间在 ES6 中已经被弃用了(无论如何之前一直是一个有问题的做法),你应该改用新的模块系统.将 Primary
导出为命名导出,而不是导入 Headline
类,而是将 import * 作为标题来自 ...
.
However, using classes like Headline
as namespaces is getting pretty deprecated with ES6 (and has previously been a questionable practice anyway), you should instead leverage the new module system. Export Primary
as a named export, and instead of importing the Headline
class rather do import * as headlines from …
.
这篇关于如何命名 es6 类(用于 React 组件)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!