我试图将Swiper添加到我的React Web App中,我在使用常规插件时遇到了麻烦,所以我决定改用以下框架: swiper-react专为React设计。
遵循入门教程:
Stylesheet
/*react-id-swiper requires Swiper's stylesheet. You can use CDN or use provided stylesheet files (css or scss) from react-id-swiper/lib/styles*/
Provided stylesheet files
// scss
import 'react-id-swiper/lib/styles/scss/swiper.scss';
// css
import 'react-id-swiper/lib/styles/css/swiper.css';
它说我需要导入这些CSS文件,但是当我尝试在组件中导入它们时,在运行时我会得到:
./src/component/component/HorizontalEventList.jsx
Module not found: Can't resolve 'react-id-swiper/lib/styles/css/swiper.css' in '/Users/giulioserra/Documents/Siti Web/Hangover/hangover/src/component/component'
没有它们,页面上的结果如下:
这是页面的代码:
import React, { Component } from "react";
import { Link } from "react-router-dom";
import global from "../../resource/global.json";
import System from "../../System/System";
import Spinner from "../component/Spinner";
import EventMini from "../card/EventMini";
import Swiper from 'react-id-swiper';
export default class HorizontalEventList extends Component {
constructor() {
super();
this.state = {
dataFetched: false,
emptyMessage: "Nulla da visualizzare.",
};
}
componentDidMount() {
//here we check if there is a callback to fetch data from
try {
if (this.state.dataFetched) return;
if (this.props.datasource !== undefined) {
this.setState({ datasource: this.props.datasource, dataFetched: true });
return;
}
if (this.props.dataCallback !== undefined && !this.state.dataFetched) {
this.props.dataCallback().then((events) => {
this.setState({ datasource: events, dataFetched: true });
});
}
} catch (err) {
console.log({ error: err });
this.setState({ datasource: {}, dataFetched: true });
}
}
/**
* Generate a list to display the events
* @author Giulio Serra <giulio.serra1995@gmail.com>
*/
generateEvenList() {
let elements = [];
for (const eventID in this.props.datasource) {
const event = this.props.datasource[eventID];
elements.push(
<li
key={eventID}
style={{
marginLeft: "30px",
marginRight: "30px",
marginBottom: "30px",
}}
>
<EventMini event={{ [eventID]: event }} />
</li>
);
}
return (
<div>
<ul
className="list-group list-group-horizontal"
style={{ listStyle: "none", overflowX: "auto" }}
>
{elements}
</ul>
</div>
);
}
render() {
const params = {
slidesPerView: 3,
spaceBetween: 30,
pagination: {
el: '.swiper-pagination',
clickable: true,
}
}
return (
<Swiper {...params}>
<div>Slide #1</div>
<div>Slide #2</div>
<div>Slide #3</div>
<div>Slide #4</div>
<div>Slide #5</div>
</Swiper>
)
}
我在做什么错?谢谢。
答案 0 :(得分:2)
它在readme的样式部分中说。
对于版本<= 2.3.2
您可以直接从
react-id-swiper/lib/styles/
导入(支持 css,scss)css
import 'react-id-swiper/lib/styles/css/swiper.css'
scss
import 'react-id-swiper/lib/styles/scss/swiper.scss'
对于版本> = 3.0.0
您应该直接从支持CSS的
Swiper
包中导入, scss和更少css
import 'swiper/css/swiper.css'
scss
import 'swiper/swiper.scss'
少
import 'swiper/swiper.less'
您应该尝试导入v3.0.0,因为这是npm上的当前版本。
答案 1 :(得分:0)
yo,如果您使用swiper@6.x.x,则没有“ swiper / css / swiper.css”。只有“ swiper / swiper.scss”,所以如果可以使用它,那就去做吧。
在我的情况下,我降级为swiper@5.x.x,因为在该版本中仍然有css文件夹和文件。