javascript - Vue 3 get props in style - Stack Overflow

I have some problems implementing props in style tag.I have props like:props: {icon: {type: String,req

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745630570a4637083.html

相关推荐

  • javascript - Vue 3 get props in style - Stack Overflow

    I have some problems implementing props in style tag.I have props like:props: {icon: {type: String,req

    14小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信