2017-03-06 39 views
-1

问题解释:javascript私有属性语法无法识别

具体来说,Id喜欢了解下面的示例如何可以不使用打字机编写。了解如何声明类的属性等将对我有用。我相信应该可以使用blueprintjs(这是写在打字稿中),而不是在我的实现中使用打字稿。


我在下面的文档:http://blueprintjs.com/docs/#components.toaster.js.react

有示例代码:

import { Button, Position, Toaster } from "@blueprintjs/core"; 


class MyComponent extends React.Component { 
    private toaster: Toaster; 
    private refHandlers = { 
     toaster: (ref: Toaster) => this.toaster = ref, 
    }; 

    public render() { 
     return (
      <div> 
       <Button onClick={this.addToast} text="Procure toast" /> 
       <Toaster position={Position.TOP_RIGHT} ref={this.refHandlers.toaster} /> 
      </div> 
     ) 
    } 

    private addToast =() => { 
     this.toaster.show({ message: "Toasted!" }); 
    } 
} 

,但我得到Syntax Error: Unexpected token, expected ((5:16)这是后 '私' 的 '烤面包机'。我不是预编译打字稿。我用web6使用es6。我将如何重写这个示例代码来处理我的环境?

谢谢。

+2

*“蓝图写在打字稿,JavaScript的一个静态类型的超集,编译成纯JavaScript的所有代码样本在本网站和所有互动的例子也被写在打字稿。” * HTTP:// blueprintjs .com/docs/ –

+0

在EcmaScript 6中没有属性访问修饰符,没有类属性,也没有类型声明。 – Bergi

回答

0

这里是我做到了。基于本教程,我从webpack获得了一些帮助:https://www.typescriptlang.org/docs/handbook/react-&-webpack.html。我将tsconfig.json配置文件中的目标设置为es6,这就是webpack编译的内容(或多或少)。

欢迎对此解决方案进行任何手动改进。

import {Button, Position, Toaster} from "@blueprintjs/core"; 

class ToastLauncher extends React.Component { 

    constructor(props) { 
     super(props); 

     this.refHandlers = { 
      toaster: (ref) => this.toaster = ref, 
     }; 

     this.addToast =() => { 
      this.toaster.show({ message: "Toasted!" }); 
     }; 

    } 

    render() { 
     return (
      <div> 
       <Button onClick={this.addToast} text="Procure toast" /> 
       <Toaster position={Position.TOP_RIGHT} ref={this.refHandlers.toaster} /> 
      </div> 
     ) 
    } 
} 

export default ToastLauncher 
2

blueprintjs是使用typescript实现的,并且该示例使用的是typescript语法。

请参见:http://blueprintjs.com/docs/#TypeScript

+0

感谢您的指针。我现在正在研究打字稿:https://www.typescriptlang.org/docs/handbook/react-&-webpack.html。我的一些代码是用webpack编译成ES6的,然后在打字稿中写一些我的组件? – xanld