3

Default Vue.js concept is to have template, JS and styles together inside one *.vue file.

My question is: how to divide CSS / SCSS from *.vue files and get below structure:

components
-- componentA
---- componentA.vue
---- componentA.scss
-- componentB
---- componentB.vue
---- componentB.scss
-- componentC
---- componentC.vue
---- componentC.scss

Is there any good practice to create such project structure and build main/dist CSS via Webpack ? Any examples or Vue CLI templates?

1 Answer 1

6

You can import the styles:

<template>
...
</template>

<style lang="scss">
    @import './foo.scss';
</style>

<script>
...
</script>

You will have to install sass-loader and configure your webpack: vue-loader.

Sign up to request clarification or add additional context in comments.

3 Comments

Is there any automatic way to attach the SCSS file to the global / common CSS once *.scss file exists inside specific component folder (without need to add @import) ?
I haven't tried it, but you can look into preLoaders. Something like: npmjs.com/package/import-glob-loader
Does intellisense for variables in foo.scss work in the .Vue file? I tried in vscode and it does not work (but the scss is imported correctly)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.