2017-08-11 182 views
3

我在Cory House的pluralsight course之后关于在ES6中构建React。不幸的是,我陷入了第一个步骤之一,设置了基本的组件。找不到模块:错误:无法解析模块“路线”

在我看到下面的错误信息控制台:

Warning: [react-router] Location "/" did not match any routes 

如果我看在我开发服务器我看到下面的

ERROR in ./src/index.js

Warning: [react-router] Location "/" did not match any routes

然后在下面,我看到eslint已经踢出以下错误:

C:\Projects\es6react\src\index.js (1/0)

✖ 5:9 routes not found in './routes' import/named

所以这个应该非常简单。然而,看着我的目录结构,index.js文件和routes.js没有什么突出的......即使大约30分钟后。

index.js

import 'babel-polyfill'; 
import React from 'react'; 
import {render} from 'react-dom'; 
import {Router, browserHistory} from 'react-router'; 
import {routes} from './routes'; 
import './styles/styles.css'; 
import '../node_modules/bootstrap/dist/css/bootstrap.min.css'; 

render(
    <Router history={browserHistory} routes={routes} />, 
    document.getElementById('app') 
); 

routes.js

import React from 'react'; 
import {Route,IndexRoute} from 'react-router'; 
import App from './components/App'; 
import HomePage from './components/home/HomePage'; 
import AboutPage from './components/about/AboutPage'; 

export default(
    <Route path="/" component={App}> 
     <IndexRoute component={HomePage} /> 
     <Route path="about" component={AboutPage}/> 
    </Route> 
); 

目录结构

enter image description here

而且万一我的scripts部分从我package.json

"scripts": { 
    "prestart": "babel-node tools/startMessage.js", 
    "start": "npm-run-all --parallel open:src lint:watch test:watch", 
    "open:src":"babel-node tools/srcServer.js", 
    "lint": "node_modules/.bin/esw webpack.config.* src tools", 
    "lint:watch": "npm run lint -- --watch", 
    "test":"mocha --reporter progress tools/testSetup.js \"src/**/*.test.js\"", 
    "test:watch": "npm run test -- --watch" 
    }, 

回答

3

您正在使用默认的出口,需要导入它作为默认的(无大括号):

import routes from './routes'; 

在另一方面你可以使用命名的出口和进口的名字是:

// index.js 
export const routes = ... 

// routes.js 
import {routes} from './routes'; 
+0

就在我的面前......谢谢 – NealR

+0

欢迎您:) – madox2

1

因为你是从做默认出口10文件未命名为导出,并将其导入为命名导出。

使用此:

import routes from './routes';  //remove {} 
1

您已经使用在routes.js“出口默认”,这意味着将其导入,您需要使用:

的进口途径从“./routes”;

在您的代码中,您使用了{routes},这些{routes}会在导出时没有默认情况下导入。

相关问题