這是最小reactjs應用程序,它採用的WebPack然後運行時捆綁中,從瀏覽器控制檯提供了以下錯誤:的WebPack reactjs應用加載失敗:未捕獲的ReferenceError:snapapp沒有定義
Uncaught ReferenceError: snapapp is not defined
燦有人建議爲什麼?
的index.html
<!DOCTYPE html>
<html lang="en">
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/babel-core/5.8.23/browser.min.js"></script>
</head>
<body class='section'>
<div id="app"></div>
<script type="text/javascript" src="./build/snapapp.js"></script>
<script type="text/javascript">
(function() {
var mountNode = document.getElementById('app');
snapapp.start(mountNode);
})();
</script>
</body>
</html>
index.js
import { render } from 'react-dom';
import rootLayout from './src/components/ApplicationLayout.jsx';
import hello from './src/components/hello.jsx';
export function start(mountNode) {
render(<rootLayout />, mountNode);
}
webpack.config.js
var path = require('path');
var webpack = require('webpack');
module.exports = {
entry: './index.js',
output: {
path: 'build',
filename: 'snapapp.js',
libraryTarget: "umd"
},
module: {
loaders: [
{
test: /.jsx?$/,
loader: 'babel-loader',
exclude: /node_modules/,
query: {
presets: ['es2015', 'react']
}
}
]
}
};
SRC /組件/ hello.jsx
import React from 'react';
class Hello extends React.Component {
render() {
return <h1>Hello</h1>
}
}
我在這裏放棄了我的新手狀態,但是從哪裏導入它?這樣的導入是什麼樣的? –
讓它起作用的最簡單方法是從index.js中導出一個函數,而不是在HTML中初始化它。在導入方面,在另一個JS文件中,即'Main.js',你可以從'./index';'或'var snapapp = require('./ index')導入snapapp;' – Lee
嗯... snapapp.js是index.js - 這是webpack捆綁index.js的結果,所以我不能從那裏加載它。 –