gpt4 book ai didi

javascript - 在我的 React 项目中安装 Swiper 时遇到问题

转载 作者:行者123 更新时间:2023-12-02 00:02:10 25 4
gpt4 key购买 nike

我正在尝试添加 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'

没有它们,页面上的结果如下:

SCREENSHOT

页面代码如下:

     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>
)
}

我做错了什么?谢谢。

最佳答案

如果您使用swiper@6.x.x,则没有'swiper/css/swiper.css'。只有 'swiper/swiper.scss',所以如果你可以使用它,那就去吧。

在我的例子中,我降级到 swiper@5.x.x 因为在那个版本中仍然有 css 文件夹和文件。

关于javascript - 在我的 React 项目中安装 Swiper 时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61540545/

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