3

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
})
Edric
  • 24,639
  • 13
  • 81
  • 91
Jalasem
  • 27,261
  • 3
  • 21
  • 29

2 Answers2

2

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

Carlo Bellettini
  • 1,130
  • 11
  • 20
0

I think that it could be as simple as that:

(function($){
  $(function(){
  
 $('.carousel').carousel({
    fullWidth: true,
    indicators: true
  });

  }); // end of document ready
})(jQuery); // end of jQuery name space

new Vue({
  el: '#app',
  data: {
    images: [{
        img: "http://quintagroup.com/cms/technology/Images/materialize.png",
        link: "#link1"
      },
      {
        img: "http://quintagroup.com/cms/technology/Images/materialize.png",
        link: "#link2"
      },
      {
        img: "http://quintagroup.com/cms/technology/Images/materialize.png",
        link: "#link3"
      },
      {
        img: "http://quintagroup.com/cms/technology/Images/materialize.png",
        link: "#link4"
      },
      {
        img: "http://quintagroup.com/cms/technology/Images/materialize.png",
        link: "#link5"
      }
    ]
  }
})
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0-alpha.1/css/materialize.min.css">

<script src="https://unpkg.com/vue"></script>
<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0-alpha.1/js/materialize.min.js"></script>


<div id=app>
  <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>
Carlo Bellettini
  • 1,130
  • 11
  • 20