我非常新的反应,作为一个启动我尝试使用react-bootstrap创建登录表单但是当我运行代码时它会在控制台中显示错误,如下所示
index.js:40724未捕获TypeError:无法读取属性'Form' 未定义 在App.render(index.js:40724) 在index.js:36958 at measureLifeCyclePerf(index.js:36238) 在ReactCompositeComponentWrapper._renderValidatedComponentWithoutOwnerOrContext (index.js:36957) 在ReactCompositeComponentWrapper._renderValidatedComponent(index.js:36984) 在ReactCompositeComponentWrapper.performInitialMount(index.js:36524) 在ReactCompositeComponentWrapper.mountComponent(index.js:36420) at Object.mountComponent(index.js:5996) 在ReactCompositeComponentWrapper.performInitialMount(index.js:36533) 在ReactCompositeComponentWrapper.mountComponent(index.js:36420)
以下是我的代码 App.js文件 - 此文件包含我的登录表单
import React from 'react';
import Bootstrap from "react-bootstrap";
export class App extends React.Component{
render(){
return (
<Bootstrap.Form horizontal>
<Bootstrap.FormGroup controlId="formHorizontalEmail">
<Bootstrap.Col componentClass={LoginForm.ControlLabel} sm={2}>
Email
</Bootstrap.Col>
<Bootstrap.Col sm={10}>
<Bootstrap.FormControl type="email" placeholder="Email" />
</Bootstrap.Col>
</Bootstrap.FormGroup>
<Bootstrap.FormGroup controlId="formHorizontalPassword">
<Bootstrap.Col componentClass={LoginForm.ControlLabel} sm={2}>
Password
</Bootstrap.Col>
<Bootstrap.Col sm={10}>
<Bootstrap.FormControl type="password" placeholder="Password" />
</Bootstrap.Col>
</Bootstrap.FormGroup>
<Bootstrap.FormGroup>
<Bootstrap.Col smOffset={2} sm={10}>
<Bootstrap.Checkbox>Remember me</Bootstrap.Checkbox>
</Bootstrap.Col>
</Bootstrap.FormGroup>
<Bootstrap.FormGroup>
<Bootstrap.Col smOffset={2} sm={10}>
<Bootstrap.Button type="submit">Sign in</Bootstrap.Button>
</Bootstrap.Col>
</Bootstrap.FormGroup>
</Bootstrap.Form>
);
}
}
文件main.js - 此文件是我的应用程序的入口点,
import React from 'react';
import ReactDOM from 'react-dom';
import {App} from './app.js';
ReactDOM.render(<App />,document.getElementById('app'));
File Index.html - 此文件包含html页面
<!DOCTYPE html>
<html lang="en">
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<meta charset="utf-8">
<title>React First App</title>
</head>
<body>
<div id="app">
</div>
<script src="index.js"></script>
</body>
</html>
File Package.json
{
"name": "demo",
"version": "1.0.0",
"description": "",
"main": "index.js",
"mode": "development",
"scripts": {
"start": "webpack-dev-server --hot",
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"babel": "^6.23.0",
"babel-cli": "^6.26.0",
"babel-core": "^6.26.0",
"babel-loader": "^7.1.2",
"babel-preset-es2015": "^6.24.1",
"babel-preset-react": "^6.24.1",
"react": "^15.6.1",
"react-bootstrap": "^0.32.1",
"react-dom": "^15.6.1",
"webpack": "^3.5.5",
"webpack-dev-server": "^2.7.1"
},
"devDependencies": {
"css-loader": "^0.28.11"
}
}
File Webpack.config.js
var config = {
entry: './main.js', // entry point
output: {
filename: 'index.js', // place where bundled app will be served
},
devServer: {
inline: true, // autorefresh
port: 8080 // development port server
},
module: {
loaders: [
{
test: /\.jsx?$/, // search for js files
exclude: /node_modules/,
loader: 'babel-loader',
query: {
presets: ['es2015', 'react'] // use es2015 and react
}
},
{
test: /\.css?$/, // search for css files
loader:'css-loader'
}
]
}
}
module.exports = config;
我尝试使用直接表单,但它不起作用。如果我使用普通的html标签,那么它的工作。我虽然这是组件的导出问题,但它在main.js中导出。请注意,如果我从类中删除导出并在app.js末尾添加export default App
,则代码不会导出组件。这很奇怪,但它正在发生。它仅适用于export class component name
我很困惑为什么它没有在bootstrap中获取表单和其他标签。在经历错误之后,我还发现反应将bootstrap.Form转换为_reactbootstrap2.Default.Form。默认显示为未定义。在调试时。但_reactbootstrap2包含Form作为元素。 没有任何线索可以继续。
我已经使用下面的命令安装了react-bootstrap
我的项目目录路径中的npm install --save react-bootstrap
答案 0 :(得分:1)
react-bootstrap
没有默认导出。
而不是import Bootstrap from "react-bootstrap"
,请尝试:
import { Form, Col, FormGroup, Checkbox } from 'react-bootstrap' // add more if you are using more components from bootstrap
然后只使用<Form>
代替<Bootstrap.Form>
。