o..o
o..o

Reputation: 1921

Import scss variables into typescript in Vue

I'm trying to import SASS (scss) variables in to the javascript in typescript Vue 3 project:

// @/assets/styles/colors.scss
$white: #fff;
// @/assets/styles/_exports.scss
@import "./colors.scss";

:export {
  white: $white;
}
<template>
  <div>
  </div>
</template>
<script lang="ts">
import colors from "@/assets/styles/_export.scss";
</script>
// vue.config.js
module.exports = {
  css: {
    loaderOptions: {
      sass: {
        prependData: '@import "@/assets/styles/colors.scss";'
      }
    }
  }
};

But I'm getting Cannot find module '@/assets/styles/_export.scss' or its corresponding type declarations..

Any ideas pls?

Thanks

Upvotes: 3

Views: 4676

Answers (2)

VladStepu2001
VladStepu2001

Reputation: 163

In addition to Michael Benares's answer, for Vue 3 project created with Vue CLI, I also had to name the file as "_name.module.scss" (hence ".module") in order to able to import the style to TypeScript.
Without that, the imported object was undefined.

The source - https://stackoverflow.com/a/71942017/12136394

Upvotes: 0

Michael Benares
Michael Benares

Reputation: 126

I created a declaration file named shims-scss.d.ts in src directory.

// shims-scss.d.ts

declare module '*.scss' {
  const content: {[className: string]: string};
  export default content;
}

It worked for me to import an object from a scss file like as _exports.scss you wrote.

Upvotes: 10

Related Questions