What are the steps to integrate MaterializeCss into Vue.js?

I prefer not to utilize Vue-Material or Vuetify.

My choice is Materialize. Here's what I do:

npm install materialize-css@next

In my main.js file, where I define my new Vue App, I import Materialize like this:

import 'materialize-css'

The JavaScript seems to be working fine, but the CSS is not loading; I tested it with a Card Reveal.

The swapping animation functions correctly, but lacks styling. The Card Reveal feature is one of the specific reasons why I opted for MaterializeCss over the other two frameworks. Additionally, I prefer using traditional HTML elements rather than adding numerous new ones (as seen in vuetify).

Answer №1

Step 1: setting up

npm install materialize-css@next --save
npm install material-design-icons --save

Step 2: incorporating materialize css in src/main.js

In the file src/main.js

import 'materialize-css/dist/css/materialize.min.css'
import 'material-design-icons/iconfont/material-icons.css'

Step 3: initializing materialize components

Add the following code in your component (for example, App.vue):

import M from 'materialize-css'

export default {
...
mounted () {
    M.AutoInit()
},
...

Answer №2

This code snippet is used to bring in the main javascript file from the npm module located in the node_modules folder:

import 'materialize-css'

If you also want to import the CSS files, you can simply add this line of code:

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

Answer №3

Another suggestion I have is to include the materialize css CDN link in your index.html file. Additionally, don't forget to insert a script tag with the following code:

document.addEventListener('DOMContentLoaded', function() {
    M.AutoInit();
 });

Answer №4

Thanks to the advice from bruinspaw, I have some updated suggestions for anyone revisiting this thread after five years:

1. Installation

pnpm install materialize-css@next
pnpm install material-design-icons --save

(I personally prefer using pnpm over npm, but either option works fine) (If you're interested in using material-design-icons-iconfont instead, see below)

2. Import Material Design icons and Materialize CSS

Add the following to your src/main.ts (or main.js if not using TypeScript):

import 'material-design-icons/iconfont/material-icons.css'
import 'materialize-css/dist/css/materialize.min.css'
import 'materialize-css/dist/js/materialize.min.js'

Note the additional import above, and further instructions are provided if using material-design-icons-iconfont.

3. Set up Components

Incorporate the following into your App.vue file (or relevant component):

<script setup lang="ts">
import { onMounted } from 'vue'
import M from 'materialize-css'
// ... other code
onMounted(() => {
  M.AutoInit()
})
</script>

(Change "ts" to "js" if TypeScript is not being used). One significant change from the original post is the use of the (Vue 3) Composition API (as indicated by 'setup') instead of the older Options API.

A key difference is that there is no need to include

<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
in index.html as suggested in the initial solution (as noted by David North). Additionally, I recommend trying out material-design-icons-iconfont instead of material-design-icons for a more streamlined version of the Material Design Icons. Make the following adjustments if you choose to go with this option:

  1. Install using
    pnpm install material-design-icons-iconfont --save
    instead of
    pnpm install material-design-icons --save
  2. Use
    import 'material-design-icons-iconfont/dist/material-design-icons.css'
    in place of
    import 'material-design-icons/iconfont/material-icons.css'

(This marks my debut post on Stack Overflow, so I hope I'm contributing constructively by updating an old discussion like this)

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Can you please provide the appropriate PropTypes for a dictionary in a ReactJS project?

Looking for something along the lines of y = {"key0": [value0, value1], "key1":[value2]} What is the proper way to define the proptypes for y? ...

AngularJS making a HttpPost request resulting in a 500-Internal Server Error

I'm currently working on an application where I need to include a user in the database which requires a POST operation. However, every time I try to run the application, I encounter a 500-Internal Server Error for the POST API call. Here is a snippe ...

What could be the reason for data.map not being recognized as a function?

I developed the following React component: import React, { useState } from 'react'; export default function Example(){ var friend = function (id, firstName, lastName){ this.id = id; this.firstName = firstName; this.lastName = lastN ...

What is the best method for combining numerous tiles within a level in Kaboom JS?

Creating a level in kaboomJS with a extensive tile map collisions can sometimes result in slower performance. I'm exploring options to optimize this process, such as potentially merging multiple tiles together so that a whole row of blocks could funct ...

Ways to bypass mongoose schema validation while making an update request in the API

In my model, one of the fields is specified as providerID: { type: Number, required: true, unique: true }. The providerID is a unique number that is assigned when inserting provider details for the first time. There are situations where I need to update ...

I am facing an issue where the data is not displaying as expected in the console

Having trouble with my Vue app where I have two methods - one to retrieve data from my Laravel backend and the other to use that data. The issue is that the second method is not able to grab the data. Below is a sample of my code: <template> ...

Troubleshooting KuCoin API: Dealing with Invalid KC-API-SIGN Error and FAQs on Creating the Correct Signature

I want to retrieve open orders for my account using the following code snippet: import { KEY, PASSWORD, SECRET } from "./secrets.js"; import CryptoJS from "crypto-js"; const baseUrl = 'https://api.kucoin.com' const endPointOr ...

Troubleshooting problem with Electron and sqlite3 post application packaging

I've been facing various challenges with Node and databases lately, hence the numerous questions I've posted here recently. Here's some background: I have an Electron app with an AngularJS frontend. On the electron side, I run an express s ...

We were caught off guard by the TypeScript error: an unexpected token showed up when we were expecting a constructor,

Trying to implement a function within a class in TypeScript. class Test { function add(x: number, y: number): number { return x + y; } } Encountering an error message stating: TypeScript Unexpected token, A constructor, method, access ...

Issue encountered when submitting form: Error identified as "Unexpected string expression without template curly in string"

As a novice in React.js, I am facing an issue where setState is not functioning properly when submitting a form. Any suggestions on how to resolve this problem? > class Home extends Component{ constructor(props){ > super(props) > ...

Leverage the object imported from the global <script> tag within a React component

I am currently attempting to incorporate a PayPal button into my React project. As per the instructions provided in PayPal's developer documentation, I have to include <script src="https://www.paypal.com/sdk/js?client-id=YOUR_CLIENT_ID"& ...

Incorporate action icons (such as edit and delete) into a table in React.js using material-ui

Within my existing table, I utilized a library known as react-bootstrap-table-next This library effectively displays data in a table format with values originating from a JSON response Now, my goal is to integrate an Action column that includes options f ...

Unlocking the Secret: How to Bind a Global Touch Event Handler in Angular 4 to Prevent iOS Safari Page Drag

The issue at hand is that within my Angular 4 application, there is a D3.js chart that relies on user touch input for dragging a needle to a specific value. The drag functionality is triggered by 'touchstart', while the registration of the final ...

Tips on preventing duplication of APIs when retrieving data using nextjs

In my code, I have a function that can be called either from server-side rendering or client side: export const getData = async (): Promise<any> => { const response = await fetch(`/data`, { method: 'GET', headers: CONTENT_TYPE_ ...

Making changes to an input field can impact other elements when using the v-model directive

I am facing an issue with a cart setup where the quantity of all products are being updated when I increase the quantity of one product. How can I prevent this and only update the quantity of the selected product? <div v-for="(product, index) in cartPr ...

Incorporate a Variety of Elements onto Your Webpage

I have developed a custom tooltip plugin using JQuery, and I am implementing it on multiple A tags. Each A tag should have a unique tooltip associated with it, so I have the following code: var $tooltip = $("<div>").attr("id", tooltip.id).attr("cla ...

Exploring the contrast between 'completed' and 'upcoming' in callback functions within node.js

Within the passport documentation for authentication configuration, there is a function that appears rather intimidating as it utilizes the enigmatic function "done." passport.use(new LocalStrategy( function(username, password, done) { User.findOne( ...

Implementing a search filter for special characters in AngularJS

Looking to filter an array of players by their names, but facing a challenge with special characters in the names. Found a code snippet on Google that looks like this: $scope.modelFilterNormalized = function(){ if($scope.modelFilter) return $sco ...

How to Ensure an Element Appears Above Another Despite Z-Index Troubles?

After conducting approximately 2 hours of research on this topic, I was unable to find clear answers or solutions. Hence, I have decided to address the issue here. The problem I'm facing is as follows: Due to the nature of HTML/CSS, it seems impossi ...

Implementing NPM commands in Jenkins using shell scripting

Whenever I attempt to run the "npm" command, I encounter a syntax error. Users/Shared/Jenkins/Home/workspace/projectName/npm test ^^^^ SyntaxError: Unexpected identifier This is the Jenkins Build shel ...