Search code examples
vue.jsmaterialize

Use Materializecss Alpha with Vue


Hearing of Materializecss Alpha release I was excited because I was a huge fan of it. But I am confused on how to import it into a typical vue.js app and initializing it plugins

for example, how we implement this in Vue:

var instance = new M.Carousel({
  fullWidth: true,
  indicators: true
})

Solution

  • Without JQuery and with webpack component

    0) I tried to use 1.0.0-alpha.2 with npm (npm install materialize-css@next) but...

    1) 1.0.0-alpha.2 has an error so meanwhile I cloned the last repo version directly from github and used with npm as local dependency (this will not be needed as soon as alpha.3 will be released):

    2) I generated the webpack template with vue-cli

    3) I substituted the HelloWorld component with the following one... where I put the carousel initialization code in the mounted method of the component

    <template>
      <div class="hello">
        <h1>{{ msg }}</h1>
        <div class="carousel">
          <div v-for="elem in images">
            <a class="carousel-item" :href="elem.link"><img :src="elem.img" :alt="elem.link"></a>
          </div>
        </div>
      </div>
    </template>
    
    <script>
      import M from 'materialize-css'
    
      export default {
        name: 'Carousel',
        mounted () {
          var elem = document.querySelector('.carousel')
          // eslint-disable-next-line
          var t = new M.Carousel(elem, {
            indicators: true
          })
        },
        data () {
          return {
            msg: 'Welcome to Your Carousel Component',
            images: [
              {
                img: 'http://quintagroup.com/cms/technology/Images/materialize.png',
                link: 'https://github.com/Dogfalo/materialize'
              },
              {
                img: 'https://vuejs.org/images/logo.png',
                link: 'https://vuejs.org/'
              },
              {
                img: 'https://avatars1.githubusercontent.com/u/9919?s=200&v=4',
                link: 'https://github.com'
              }
            ]
          }
        }
      }
    </script>
    

    4) I added import 'materialize-css/dist/css/materialize.min.css' to main.js