我正在使用 React on Rails,并且一直在尝试使用 Antd UI 框架。
我已经成功地从“antd”导入了按钮和日期选择器等组件
import { Button } from 'antd';
但它们根本没有样式。查看我的服务器,我有以下错误...
ERROR in ./node_modules/antd/lib/button/style/index.less
Module parse failed: Unexpected character '@' (1:0)
You may need an appropriate loader to handle this file type.
| @import "../../style/themes/default";
| @import "../../style/mixins/index";
| @import "./mixin";
@ ./node_modules/antd/lib/button/style/index.js 5:0-23
@ ./app/javascript/packs/application.js
ERROR in ./node_modules/antd/lib/style/index.less
Module parse failed: Unexpected character '@' (1:0)
You may need an appropriate loader to handle this file type.
| @import "./themes/default";
| @import "./core/index";
|
我尝试了几种不同的方法来访问样式,例如直接引用我的 jsx 文件中的特定样式表
//app/javascript/bundles/Page/components/Page.jsx
import React, { Component } from "react";
// import { Button, DatePicker } from 'antd'
import { Button } from 'antd';
// import Button from 'antd/lib/button';
// import 'antd/lib/button/style/css';
export default class Page extends Component {
render() {
return (
<div >
<Button type="primary">Primary</Button>
<Button>Default</Button>
<Button type="dashed">Dashed</Button>
<Button type="danger">Danger</Button>
</div>
);
}
}
查看 Antd 文档,我遵循了按需导入技术并添加了
"import", {
"libraryName": "antd",
"style": "css",
}
]
到我的 .babelrc 文件
https://ant.design/docs/react/getting-started#Import-on-Demand
我也尝试安装less
andless-loader
因为我很确定它与包含“@”的 css 文件有关,这表明它是一个 less 或 sass 文件。
我能够加载样式的唯一成功方法是放置
@import 'antd/dist/antd.css';
在我的 app/assets/stylesheets/page.scss 中。
虽然此选项有效,但它不允许import on demand
导入 css 的能力和感觉是不正确的方式,因为它使用 rails 资产管道而不是 webpack(通过 webpacker)