2017-03-07 92 views
0
import React from 'react'; 
//import logo from './logo.svg'; 
import './App.css'; 
import '../public/components/NavBar'; 


class App extends React.Component { 
    render() { 
    return (
     <NavBar /> 
    ); 
    }; 
}; 

export default App; 

夠簡單。爲什麼我會收到以下編譯錯誤?從外部目錄導入React組件

Failed to compile. 

Error in ./src/App.js 

C:\Users\martin\Documents\Web Projects\ReactApp\app\src\App.js 
    10:9 error 'NavBar' is not defined react/jsx-no-undef 

✖ 1 problem (1 error, 0 warnings) 

的NavBar

import React, {Component} from 'react'; 

class NavBar extends Component { 
    handleClick() { 
    alert('Hello world!'); 
    } 

    render() { 
    return (
     <nav class="navbar navbar-default"> 
      <div class="container-fluid"> 
      <div class="navbar-header"> 
       <a class="navbar-brand" href="{this.handleClick}"> 
       Yeet Club 
       </a> 
      </div> 
      </div> 
     </nav> 
    ); 
    } 
} 

export default NavBar; 

回答

0

JavaScript導入需要from和一個名字:

import NavBar from '../public/components/NavBar'; 
+0

我得到這個''在./public/components/NavBar.js 模塊解析錯誤失敗:C:\ Users \ martin \ Documents \ Web Projects \ ReactApp \ app \ public \ components \ NavBar.js意外的令牌(10:2) 您可能需要適當的l oader來處理這種文件類型。 SyntaxError:意外令牌(10:2) @ ./src/App.js 16:14-52'' – santafebound

+0

我包含上面的NavBar代碼。我仍然相信問題在於導入組件,而不是組件代碼。當它包含在src /文件夾中時,該組件可以正常工作... – santafebound