Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to include css files in Vue 2

I'm new to vue js and trying to learn this. I installed a fresh new version of vue webpack in my system. I'm having a css, js and images of this a theme template which I want to include into the HTML so i tried adding it in index.html but I can see errors in console and the assets are not being added. While I searched I came to know that I can use require in main.js file. But I'm getting the error:

Following I've tried in my main.js file:

// The Vue build version to load with the `import` command // (runtime-only or standalone) has been set in webpack.base.conf with an alias.  import Vue from 'vue'  import App from './App'  import router from './router'   require('./assets/styles/vendor.css');  require('./assets/styles/main.css');  require('./assets/scripts/vendor/modernizr.js');   Vue.config.productionTip = false   /* eslint-disable no-new */  new Vue({   el: '#app',    router,   template: '<App/>',   components: { App }  }) 

While I tried using import to use it but still I got error

// The Vue build version to load with the `import` command // (runtime-only or standalone) has been set in webpack.base.conf with an alias.  import Vue from 'vue'  import App from './App'  import router from './router'   import('./assets/styles/vendor.css')  // require('./assets/styles/vendor.css');  // require('./assets/styles/main.css');  // require('./assets/scripts/vendor/modernizr.js');   Vue.config.productionTip = false   /* eslint-disable no-new */  new Vue({   el: '#app',    router,   template: '<App/>',   components: { App }  }) 

Here is the error screenshot: Error message

Help me out in this.

like image 387
Nitish Kumar Avatar asked May 04 '17 13:05

Nitish Kumar


People also ask

How do I add CSS to Vue?

In Vue. js, we can add an inline style to our element by binding the style attribute in the HTML tag. For reference, :style is shorthand for v-bind:style . Inline styling can be done in two ways: using object syntax or array syntax.

How do I import components into Vue 2?

STEP 01: First, Import the Child Component into the Parent Component inside script tag but above export default function declaration. STEP 02: Then, Register the Child Component inside the Parent Component by adding it to components object. STEP 03: Finally, Use the Child Component in the Parent Component Template.

How do I import CSS files into Vue 3?

Importing CSS in Vue components If you are setup and using webpack, you should be able to import css directly from inside <style> tags in component files. More info here: https://cli.vuejs.org/guide/css.html.

How do I import global CSS in Vue?

To add global CSS in Vue. js, we can import our global CSS file in main. js . import "./assets/css/main.


2 Answers

You can import the css file on App.vue, inside the style tag.

<style>   @import './assets/styles/yourstyles.css'; </style> 

Also, make sure you have the right loaders installed, if you need any.

like image 137
gedii Avatar answered Oct 02 '22 03:10

gedii


Try using the @ symbol before the url string. Import your css in the following manner:

import Vue from 'vue'  require('@/assets/styles/main.css') 

In your App.vue file you can do this to import a css file in the style tag

<template>   <div>   </div> </template>  <style scoped src="@/assets/styles/mystyles.css"> </style> 
like image 44
Sir Waithaka Avatar answered Oct 02 '22 02:10

Sir Waithaka