Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Include local CSS files in a single vue component

Am building a Laravel vue SPA, I need to know How to include local CSS files in a single vue component only?

like image 829
Dickson Godwin Avatar asked Jan 02 '23 12:01

Dickson Godwin


2 Answers

You can use the @import available through scss like so:

<style lang="scss" scoped>
  @import '../css/mycss.scss'; /* injected */
  @import '../css/mycss.css'; /* will use url import (do not use)*/
</style>

This may require that you add node-sass dependency and make some adjustments to your webpack configuration, if you don't already have it. If you're using a Vue cli generated project though, it is likely already included.

Note that if you want the css to be scoped, you should rename the extension to .scss, this way it will be handled by vue-loader. Importing a .css file is does not work the same way, and is treated as a url import.

like image 86
Daniel Avatar answered Jan 13 '23 15:01

Daniel


You can import CSS file on component inside script tag

This worked for me:

<template>
</template>

<script>
import "@/assets/<file-name>.css";

export default {}
</script>
like image 25
Hosein Avatar answered Jan 13 '23 14:01

Hosein