2017-03-06 40 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