最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - Vue 3 get props in style - Stack Overflow

matteradmin6PV0评论

I have some problems implementing props in style tag. I have props like:

 props: {
    icon: {
      type: String,
      required: true
    },
    label: {
      type: String,
      required: true
    }
  }

And I want to use label props in Style, like:

    <style scoped lang="scss">
      .class{
        color: label;
      }
    </style>

is that possible? Because it's not working like this

Idea is actually to make a call like:

.drop-color{
    color: map-get($var, label);
  }

This will work if I define it as;

.drop-color{
    color: map-get($var, 'blue');
  }

I just need to pass prop label instead.

I have some problems implementing props in style tag. I have props like:

 props: {
    icon: {
      type: String,
      required: true
    },
    label: {
      type: String,
      required: true
    }
  }

And I want to use label props in Style, like:

    <style scoped lang="scss">
      .class{
        color: label;
      }
    </style>

is that possible? Because it's not working like this

Idea is actually to make a call like:

.drop-color{
    color: map-get($var, label);
  }

This will work if I define it as;

.drop-color{
    color: map-get($var, 'blue');
  }

I just need to pass prop label instead.

Share Improve this question edited Sep 8, 2021 at 12:11 Rade Iliev asked Sep 8, 2021 at 12:03 Rade IlievRade Iliev 2395 silver badges14 bronze badges 1
  • Check this answer - stackoverflow./a/42872117/10000229 – Zoha Malik Commented Sep 8, 2021 at 12:10
Add a ment  | 

2 Answers 2

Reset to default 4

With new script setup you're able to do it using v-bind of vue 3.2.x:

<script setup>
import {defineProps} from 'vue'

const props = defineProps({
     icon: {
      type: String,
      required: true
    },
    label: {
      type: String,
      required: true
    }
})


</script>
  <style scoped lang="scss">
      .class{
        color: v-bind(label);
      }
    </style>

LIVE DEMO

I don't think you can do that. v-bind is runtime expression, whereas map-get and other SASS specific things are transpiled to CSS at build time. So, if the key of the map is not available at build time, it won't build correctly.

But, you can import SASS variables and maps into JS and use them instead.

I don't know if SASS's builtin :export supports maps, but if it doesn't you can use third party libraries like this one:

https://www.npmjs./package/sass-export

Post a comment

comment list (0)

  1. No comments so far