Next 没有 SSR 的 Js 动态导入不起作用
Next Js dynamic imports with no SSR not working
我正在尝试使用这个 react-carousel-3d 库 https://github.com/suhailsulu/react-carousel-3d 但我收到以下错误,因为该库未开发为支持 SSR。
`ReferenceError: window is not defined`
at Object.<anonymous> (C:\Deba\Workspace2021\Nextjs\myportfolio\node_modulesd-react-carousal\dist\index.js:1:255)
现在我正在尝试使用没有 SSR 的动态导入 https://nextjs.org/docs/advanced-features/dynamic-import#with-no-ssr
const {Carousel} = dynamic(
() => import('../node_modules/3d-react-carousal/src/index.js'),
{ ssr: false }
)
我现在遇到以下错误:
./node_modules/3d-react-carousal/src/index.js 189:12
Module parse failed: Unexpected token (189:12)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
render() {
return (
<div className="react-3d-carousel" style={{ height: this.state.height }}>
{this.state.slides && this.state.slides.length > 0 &&
<div className="slider-container">
有人可以指出我在这里做错了什么或关于如何让它工作的任何想法吗?
不确定是否可以从 node_module 动态加载,如下所示:
const {Carousel} = dynamic(
() => import('3d-react-carousal'),
{ ssr: false }
)
但是您应该可以通过先创建一个 carousal 组件,然后像这样动态导入它来做到这一点:
// create a component named MyCarousel.js in components folder
import {Carousel} from '3d-react-carousal';
let slides = [
<img src="https://picsum.photos/800/300/?random" alt="1" />,
<img src="https://picsum.photos/800/301/?random" alt="2" /> ,
<img src="https://picsum.photos/800/302/?random" alt="3" /> ,
<img src="https://picsum.photos/800/303/?random" alt="4" /> ,
<img src="https://picsum.photos/800/304/?random" alt="5" /> ];
const MyCarousel = (<Carousel slides={slides} autoplay={true} interval={1000}/>);
export default MyCarousel;
// then dynamic import it:
const MyCarousel = dynamic(
() => import('../components/MyCarousel'),
{ ssr: false }
)
我正在尝试使用这个 react-carousel-3d 库 https://github.com/suhailsulu/react-carousel-3d 但我收到以下错误,因为该库未开发为支持 SSR。
`ReferenceError: window is not defined`
at Object.<anonymous> (C:\Deba\Workspace2021\Nextjs\myportfolio\node_modulesd-react-carousal\dist\index.js:1:255)
现在我正在尝试使用没有 SSR 的动态导入 https://nextjs.org/docs/advanced-features/dynamic-import#with-no-ssr
const {Carousel} = dynamic(
() => import('../node_modules/3d-react-carousal/src/index.js'),
{ ssr: false }
)
我现在遇到以下错误:
./node_modules/3d-react-carousal/src/index.js 189:12
Module parse failed: Unexpected token (189:12)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
render() {
return (
<div className="react-3d-carousel" style={{ height: this.state.height }}>
{this.state.slides && this.state.slides.length > 0 &&
<div className="slider-container">
有人可以指出我在这里做错了什么或关于如何让它工作的任何想法吗?
不确定是否可以从 node_module 动态加载,如下所示:
const {Carousel} = dynamic(
() => import('3d-react-carousal'),
{ ssr: false }
)
但是您应该可以通过先创建一个 carousal 组件,然后像这样动态导入它来做到这一点:
// create a component named MyCarousel.js in components folder
import {Carousel} from '3d-react-carousal';
let slides = [
<img src="https://picsum.photos/800/300/?random" alt="1" />,
<img src="https://picsum.photos/800/301/?random" alt="2" /> ,
<img src="https://picsum.photos/800/302/?random" alt="3" /> ,
<img src="https://picsum.photos/800/303/?random" alt="4" /> ,
<img src="https://picsum.photos/800/304/?random" alt="5" /> ];
const MyCarousel = (<Carousel slides={slides} autoplay={true} interval={1000}/>);
export default MyCarousel;
// then dynamic import it:
const MyCarousel = dynamic(
() => import('../components/MyCarousel'),
{ ssr: false }
)