如何命名空间es6类(对于React组件) [英] How to namespace es6 classes (for React components)

查看:1411
本文介绍了如何命名空间es6类(对于React组件)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是ES6部分的一部分反应问题。我正在尝试使用命名空间的组件在与ES6类的反应中,巴贝尔。所以我想真正的问题是如何命名空间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 { … };

但是,使用类似标题作为命名空间对于ES6而言,它已经被淘汰了(而且以前一直是一个有问题的做法),你应该使用新的模块系统。将主要导出为命名导出,而不是导入标题类而不是 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆