gpt4 book ai didi

reactjs - React Bootstrap 没有设计我的 React 组件的样式

转载 作者:行者123 更新时间:2023-12-03 13:16:14 28 4
gpt4 key购买 nike

昨天刚开始使用 React,因此设置了一个演示应用程序。环境是:

  1. typescript
  2. 网络包
  3. React 和 React DOM

我正在尝试设置 Bootstrap 样式。

我按照本教程进行操作,但对其进行了修改以适应 Typescript:

https://medium.com/@victorleungtw/how-to-use-webpack-with-react-and-bootstrap-b94d33765970

一切正常,页面显示登录表单,并且 Bootstrap HTMl 由 React-Bootstrap 输出。但样式并未应用于元素,它只是一个纯 HTML 页面,未应用任何 Bootstrap 样式。我不确定配置中缺少什么:

webpack.config.js

我已经按照教程中所述通过 npm 添加了加载器,但也进行了调整以从 css 设置 Typescript 模块。

module.exports = {
entry: {
catalog: "./src/apps/CatalogApp.tsx",
auth: "./src/apps/AuthApp.tsx"
},
output: {
filename: "[name].bundle.js",
publicPath: "/build/",
path: __dirname + "/dist"
},

// Enable sourcemaps for debugging webpack's output.
devtool: "source-map",

resolve: {
// Add '.ts' and '.tsx' as resolvable extensions.
extensions: [".ts", ".tsx", ".js", ".json"]
},

module: {
rules: [
// All files with a '.ts' or '.tsx' extension will be handled by 'awesome-typescript-loader'.
{
test: /\.tsx?$/,
loader: "awesome-typescript-loader"
},

// All output '.js' files will have any sourcemaps re-processed by 'source-map-loader'.
{
enforce: "pre",
test: /\.js$/,
loader: "source-map-loader"
},

// css loader
{
test: /\.css$/,
loader: "typings-for-css-modules-loader?modules"
},

{
test: /\.(woff|woff2)(\?v=\d+\.\d+\.\d+)?$/,
loader: 'url-loader?limit=10000&mimetype=application/font-woff'
},
{
test: /\.ttf(\?v=\d+\.\d+\.\d+)?$/,
loader: 'url-loader?limit=10000&mimetype=application/octet-stream'
},
{
test: /\.eot(\?v=\d+\.\d+\.\d+)?$/,
loader: 'file-loader'
},
{
test: /\.svg(\?v=\d+\.\d+\.\d+)?$/,
loader: 'url-loader?limit=10000&mimetype=image/svg+xml'
}
]
},

// When importing a module whose path matches one of the following, just
// assume a corresponding global variable exists and use that instead.
// This is important because it allows us to avoid bundling all of our
// dependencies, which allows browsers to cache those libraries between builds.
externals: {
"react": "React",
"react-dom": "ReactDOM"
}
};

AuthApp.tsx

这是我的应用程序的入口点。

import * as React from "react";
import * as ReactDOM from "react-dom";

import { SigninForm } from "../components/users/SigninForm";
import { MiniCart } from '../components/cart/MiniCart';
import { UserMenu } from '../components/users/UserMenu';
import * as bs from 'bootstrap/dist/css/bootstrap.css';
import * as bst from 'bootstrap/dist/css/bootstrap-theme.css';

if (document.getElementById("signin-form")) {
ReactDOM.render(
<SigninForm />,
document.getElementById("signin-form")
);
}

if (document.getElementById('cart')) {
ReactDOM.render(
<MiniCart />,
document.getElementById('cart')
);
}

if (document.getElementById('user-menu')) {
ReactDOM.render(
<UserMenu />,
document.getElementById('user-menu')
);
}

SigninForm.tsx

以及登录表单的相关 UI。

import * as React from 'react';
import * as ReactDOM from "react-dom";
import { Button, Col, Row } from 'react-bootstrap';

import { Spinner } from '../shared/Spinner';

interface SigninFormProps {
}

interface SigninFormState {
email: string;
password: string;
}

export class SigninForm extends React.Component<SigninFormProps, SigninFormState> {

constructor(props: SigninFormProps) {
super(props);
this.state = {
email: '',
password: ''
};
this.handleChange = this.handleChange.bind(this);
this.handleSubmit = this.handleSubmit.bind(this);
}

handleChange(event: any) {
this.setState({ [event.target.name]: event.target.value });
}

handleSubmit(event: any) {
event.preventDefault();
console.log('Submitting form', this.state);
}

render() {
return (
<Row>
<Col md={8}>
<form className="signin-form" onSubmit={this.handleSubmit}>
<div className="form-group">
<label htmlFor="email">Email</label>
<input id="email" name="email" type="email" value={this.state.email} onChange={this.handleChange} />
</div>
<div className="form-group">
<label htmlFor="password">Password</label>
<input id="password" name="password" type="password" value={this.state.password} onChange={this.handleChange} />
</div>
<Button>Submit</Button>
</form>
</Col>
<Col md={4}>
Right Side
</Col>
</Row>
);
}
}

最佳答案

我遇到了同样的问题,这对我有用 -->

我最终使用 npm 安装了 bootstrap:

  • 1) npm i --save bootstrap

然后,在index.js(我渲染App.js的地方)中,我从nodes_modules导入了bootstrap的缩小CSS文件,如下所示:

  • 2) 导入 '../node_modules/bootstrap/dist/css/bootstrap.min.css';

现在响应式定位正在工作(就像以前一样,我看到 react-bootstrap 渲染了正确的类名,但没有样式)。

注:https://react-bootstrap.github.io/getting-started/introduction说你应该将 css 添加到 index.html 的头部。我这样做并没有成功。

关于reactjs - React Bootstrap 没有设计我的 React 组件的样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47354472/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com