我正在關注的陣營,路由器guide 但在瀏覽器控制檯收到此錯誤:客戶端陣營,路由器演示:不變違反
14:14:28.840 Error: Invariant Violation: Element type is invalid: expected
a string (for built-in components) or a class/function (for composite
components) but got: undefined.1 react.js:18307:15
我不知道什麼是正確的 - 陣營 - 路由器類被導入並且應該是有效的React類。唯一的區別是我正在加載類 - 客戶端而不是服務器端。
下面是代碼:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.14.0/react.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.14.0/react-dom.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-router/0.13.4/ReactRouter.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/babel-core/5.6.15/browser.js"></script>
</head>
<body>
<div id="content"></div>
<script type="text/babel">
var Route = window.ReactRouter.Route;
var Router = window.ReactRouter.Router;
var Link = window.ReactRouter.Link;
var DefaultRoute = window.ReactRouter.DefaultRoute;
var RouteHandler = window.ReactRouter.RouteHandler;
var IndexRoute = window.ReactRouter.IndexRoute;
const App = React.createClass({
render() {
return (
<div>
<h1>App</h1>
<ul>
<li><Link to="/about">About</Link></li>
<li><Link to="/inbox">Inbox</Link></li>
</ul>
{this.props.children}
</div>
)
}
})
const About = React.createClass({
render() {
return <h3>About</h3>
}
})
const Inbox = React.createClass({
render() {
return (
<div>
<h2>Inbox</h2>
{this.props.children || "Welcome to your Inbox"}
</div>
)
}
})
const Message = React.createClass({
render() {
return <h3>Message {this.props.params.id}</h3>
}
})
ReactDOM.render((
<Router>
<Route path="/" component={App}>
<Route path="about" component={About} />
<Route path="inbox" component={Inbox}>
<Route path="messages/:id" component={Message} />
</Route>
</Route>
</Router>
), document.getElementById('content'));
</script>
</body>
</html>
你爲什麼混合ES6和ES5語法? –
browser.js無法識別我的'import'調用(「require is undefined」),所以我使用react-router [介紹]中建議的語法(https://github.com/rackt/react-router #umd) –
爲什麼你使用'const App = React.createClass'而不是'class App extends React.Component'?這是創建課程的首選方法,您應該遵循他們的指示。我也沒有看到任何導入調用,所以編輯你的問題與相關信息 –