Francisco
Francisco

Reputation: 163

Materialize inside Vue.js

I am new with Vue.js and I want to make my project with Materialize. I try a lot of plugins like: vue-materialize (https://github.com/paulpflug/vue-materialize), vue-material-components(https://www.npmjs.com/package/vue-material-components) and didn't work. I also tried to add jQuery plugin to webpack and I don't have any solution:

new webpack.ProvidePlugin({
    $: 'jquery',
    jquery: 'jquery',
    'window.jQuery': 'jquery',
    jQuery: 'jquery'
}),

Now I am trying to make work a input select field. How can I make this work?

Upvotes: 0

Views: 12520

Answers (3)

Gaurav
Gaurav

Reputation: 609

You can use Vue-Material to materialize your Vue project. Following are the steps that you need to follow:

  1. Install vuematerial $ npm install --save vue-material
  2. Import vuematerial to your main.js import VueMaterial from 'vue-material' import 'vue-material/dist/vue-material.css'
  3. Start using vuematerial in your project Vue.use(VueMaterial)

A sample main.js file should look something like this:

import Vue from 'vue';
import VueMaterial from 'vue-material'
import 'vue-material/dist/vue-material.css';
import App from './App';
import router from './router';

Vue.use(VueMaterial)
Vue.config.productionTip = false;

/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  template: '<App/>',
  components: { App },
});

You will now be able to use vuematerial components inside your template.

Upvotes: 6

Oswaldo
Oswaldo

Reputation: 3376

Here is my setup to use Materialize with Vue Webpack template:

build/webpack.base.conf.js

var webpack = require('webpack')
module.exports =
  resolve: {
    alias: {
      ...
      'jquery': 'materialize-css/node_modules/jquery/dist/jquery.js'
    }
  },
  module: {
    rules: [
      {
        test: /\.(png|jpe?g|gif|svg)(\?.*)?$/,
        ...
        options: {
          limit: 10000
  ....
  plugins: [
    new webpack.ProvidePlugin({
      $: 'jquery',
      jquery: 'jquery',
      jQuery: 'jquery',
      'window.$': 'jquery',
      'window.jQuery': 'jquery'
    })
  ]
}

index.html

<head>
  ...
  <link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
</head>

package.json

{
  ...
  "dependencies": {
    "materialize-css": "^0.98.2",
  ...
  "devDependencies": {
    "@types/jquery": "^2.0.43", // ==> if using typescript

src/main.js

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

So, for a "input select field" like below:

<div class="input-field" ref="myInput">
  <select>
    <option value="1">Option 1</option>
    <option value="2">Option 2</option>
    <option value="3">Option 3</option>
  </select>
  <label>Materialize Select</label>
</div>

...you could put this code in your mounted lifecycle hook:

mounted() {
  $(this.$refs.myInput).material_select()

Upvotes: 5

Sandun Madola
Sandun Madola

Reputation: 890

Vue.js given link by you reffers Materializecss and both are look same. Here I have added fully working code with drop down button.

You can just copy and paste following code to a text file and save it as a .html file (ex: name.html). You can edit this code by adding relevant code in between the comment <!--your code start--> and <!--your code end-->.

Click the Run code Snippet button below to test online and you can see DROP ME! button. Once you click it you can see how drop down works.

More details refer materializecss which is more user friendly than the link you have given.

<!DOCTYPE html>
<html>

    <head>
        <title>Slider</title>
        <!-- Compiled and minified CSS -->
        <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.97.3/css/materialize.min.css">
        <!--Let browser know website is optimized for mobile-->
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    </head>

    <body>
        <!--your code start-->
        
              <!-- Dropdown Trigger -->
              <a class='dropdown-button btn' href='#' data-activates='dropdown1'>Drop Me!</a>

              <!-- Dropdown Structure -->
              <ul id='dropdown1' class='dropdown-content'>
                <li><a href="#!">one</a></li>
                <li><a href="#!">two</a></li>
                <li class="divider"></li>
                <li><a href="#!">three</a></li>
              </ul>      
      
        <!--your code end-->

        <!--Import jQuery before materialize.js-->
        <script type="text/javascript" src="https://code.jquery.com/jquery-2.1.1.min.js"></script>

        <!-- Compiled and minified JavaScript -->
        <script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.97.3/js/materialize.min.js"></script>
     
    </body>

</html>

Upvotes: -4

Related Questions