我在我的新Reactjs应用程序上得到一个404错误,它是在寻找一个main.js文件



这个应用程序应该渲染我在这里(/Users/ldco2016/Projects/reactquiz/src/components/app .jsx:

import React, {Component} from 'react';
import ReactDOM from 'react-dom';
class App extends Component{
    render(){
        return(
            <div>
                APP
            </div>
        )
    }
}
export default App

但我得到一个空白页面,Chrome开发工具控制台告诉我这个:

GET http://localhost:8080/app/js/main.js 404 (Not Found)

我知道这意味着什么,但我不清楚它是否试图找到我想要渲染的页面或main.js文件。

/用户/ldco2016/项目/reactquiz/package.json:

{
  "name": "reactquiz",
  "version": "1.0.0",
  "description": "A Reactjs Quiz App",
  "main": "index.js",
  "scripts": {
    "test": "echo "Error: no test specified" && exit 1"
  },
  "author": "Daniel Cortes",
  "license": "ISC",
  "devDependencies": {
    "babel-core": "6.13.*",
    "babel-loader": "6.2.*",
    "webpack": "1.13.*"
  },
  "dependencies": {
    "react": "^15.3.1",
    "react-dom": "^15.3.1"
  }
}

/用户/ldco2016/项目/reactquiz/app/index . html:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Reactjs Quiz</title>
</head>
<body>
    <div id="app"></div>
    <script src="js/main.js"></script>
</body>
</html>

ldco2016@DCortes-MacBook-Pro-3 ~/项目/reactquiz/webpack.config.js:

module.export = {
    entry: [
        './src/index.js'
    ],
    output: {
        path: __dirname,
        filename: 'app/js/main.js'
    },
    module: {
        loaders: [{
            test: /.jsx?$/,
            loader: 'babel',
            exclude: /node_modules/
        }]
    }
}

/用户/ldco2016/项目/reactquiz/src/index.js:

import React from 'react';
import ReactDOM from 'react-dom';
import App from './components/App.jsx';
ReactDOM.render(
    <App />,
    document.getElementById('app')
);

你需要运行webpack来生成main.js文件。对您的文件进行两次更改,首先在package.json中添加脚本"start": "webpack --progress -p --config webpack.config.js --watch",并在webpack.config.js中将加载器更改为babel-loader

注:第二次修改可能不需要。

package.json

{
  "name": "reactquiz",
  "version": "1.0.0",
  "description": "A Reactjs Quiz App",
  "main": "index.js",
  "scripts": {
    "start": "webpack --progress -p --config webpack.config.js --watch"
  },
  "author": "Daniel Cortes",
  "license": "ISC",
  "devDependencies": {
    "babel-core": "6.13.*",
    "babel-loader": "6.2.*",
    "webpack": "1.13.*"
  },
  "dependencies": {
    "react": "^15.3.1",
    "react-dom": "^15.3.1"
  }
}

webpack.config.js

module.export = {
    entry: [
        './src/index.js'
    ],
    output: {
        path: __dirname,
        filename: 'app/js/main.js'
    },
    module: {
        loaders: [{
            test: /.jsx?$/,
            loader: 'babel-loader',
            exclude: /node_modules/
        }]
    }
}

现在用命令

运行webpack
npm run start

现在打开您的本地主机,上面的工作。

相关内容

  • 没有找到相关文章

最新更新