【问题标题】:window is undefined in next js when using materialize css使用物化 css 时,下一个 js 中的窗口未定义
【发布时间】:2020-11-27 05:21:25
【问题描述】:

我正在使用下一个 js。当我尝试import M from 'materialize-css'; 时,我得到window is undefined

【问题讨论】:

    标签: reactjs next.js materialize


    【解决方案1】:

    我添加了useEffect 并添加了这一行:

         if(typeof window !== 'undefined'){
           const M = require('materialize-css');
           ...
         }
          
    

    而不是import 声明

    【讨论】:

      【解决方案2】:

      您只能在客户端使用dynamic 导入materialize-css

      import dynamic from 'next/dynamic';
      const M = dynamic(() => import('materialize-css'), {
          ssr: false,
      });
      

      【讨论】:

        猜你喜欢
        • 2020-12-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-07-23
        • 2022-09-24
        • 2021-08-23
        • 2019-10-13
        相关资源
        最近更新 更多