Include local CSS files in a single vue component
Asked Answered
V

2

5

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

Voss answered 18/12, 2018 at 13:58 Comment(2)
Do you refer to scoped CSS? Ref:vue-loader.vuejs.org/guide/scoped-css.htmlLongshore
i used scoped css but it only works if i refresh the page and still affect other components loaded via vue routerVoss
G
9

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.

Grecism answered 18/12, 2018 at 15:25 Comment(2)
Don't be afraid to nest the import under a css selector as well, it works!Forge
important point is extension should be .scssErosion
L
1

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>
Listed answered 6/4, 2021 at 16:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.