2016-02-15 27 views
2

我的應用程序有一個組件,該組件在特定頁面的頂部創建導航欄。我想只在用戶當前登錄時才顯示「註銷」按鈕(存儲在localStorage中的令牌)。React組件找不到函數

當下面的代碼運行時,瀏覽器給了我以下錯誤:

的ReferenceError:找不到變量:showLogout

import React from 'react' 
import NavHelper from './components/nav-helper' 

export default React.createClass({ 
    render() { 
    return(
     <NavHelper> 
     <nav className='top-nav top-nav-light cf' role='navigation'> 
      <input id='menu-toggle' className='menu-toggle' type='checkbox'/> 
      <label htmlFor='menu-toggle'>Menu</label> 
      <ul className='list-unstyled list-inline cf'> 
       <li><a href="/home">Website</a></li> 
       <li><a href='/languages'>Languages</a></li> 
       <li><a href='/topics'>Topics</a></li> 
       //========================== 
       {window.localStorage.token ? showLogout() : null} 
       //========================== 
       <li className='pull-right'><a href='/saved'>Saved</a></li> 
      </ul> 
     </nav> 
     <div className='container'> 
      {this.props.children} 
     </div> 
     </NavHelper> 

    ) 
    }, 
    showLogout() { 
    return (<li className='pull-right'><a href='/logout'>Logout</a></li>) 
    } 
}) 

回答

3

因爲這是你應該是指一類內部功能如下:this.showLogout()

+0

Doh。我發誓我嘗試過,但沒有奏效。但是再次嘗試後,它現在正在工作。謝謝。 –