问题
I'm using next js. When I try to import M from 'materialize-css';
I get window is undefined
.
回答1:
You can only import the materialize-css
on client side by using dynamic.
import dynamic from 'next/dynamic';
const M = dynamic(() => import('materialize-css'), {
ssr: false,
});
回答2:
I added a useEffect
and added this line:
if(typeof window !== 'undefined'){
const M = require('materialize-css');
...
}
instead of the import
statement
来源:https://stackoverflow.com/questions/63292172/window-is-undefined-in-next-js-when-using-materialize-css