这是 ES6 问题的一部分,React 问题的一部分。我正在尝试使用namespaced components在 React 中使用 ES6 类和 Babel。所以我想真正的问题是如何命名空间 es6 类,以便我可以执行此处解释的操作:https://facebook.github.io/react/docs/jsx-in-depth.html#namespaced-components
因为我收到意外的 token 错误:
class Headline extends Component { ... }
class Headline.Primary extends Component { ...
^
最佳答案
ECMAScript-6 class declaration语法需要标准的 BindingIdentifer 作为类名。点不是标识符名称中的有效字符。
在OP中链接使用的上下文中,“命名空间”是一个对象,并且使用点表示法将属性一一添加到该对象中以进行属性访问。
您可以通过使用类表达式来复制它:
'use strict'
var ns = {}
ns.MyClass = class {
constructor() {
console.log('in constructor')
}
}
new ns.MyClass()
关于javascript - 如何命名 es6 类(针对 React 组件),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32230966/