-->

How to include materialize-css npm package with we

2019-06-14 23:38发布

问题:

I'm writing a client-side app that is using Webpack, and I cannot figure out how to require the materialize-css package. I'm using Henrik Joreteg's hjs-webpack package, and with this the yeticss npm package is included by doing an import in a sass file (e.g. @import 'yeticss'), but this doesn't work for materialize. Requiring it straight up in the code (e.g. import 'materialize-css' in a JS file) like any other package also doesn't work.

回答1:

In this case, unlike with yeticss, you need to go in and require the specific files, rather than just the package name, thus:

import 'materialize-css/dist/css/materialize.min.css'
import 'materialize-css/dist/js/materialize.min.js'


回答2:

In my case I'm using create-react-app, and I was able to:

yarn add materialize-css

And then in my index.js (top level react file):

import '../node_modules/materialize-css/dist/css/materialize.min.css';
import '../node_modules/materialize-css/dist/js/materialize.min.js';