在SCSS(SASS)变量VUEJS中使用V-Bind

发布于 2025-02-08 06:04:38 字数 408 浏览 2 评论 0原文

我正在研究Vuejs中的一个组件,我想制作一个颜色属性,该属性采用十六进制的颜色,并使用SASS的RGBA功能转换为RGB颜色并与之合作。

props: {
    color: {
        type: String,
        default: '#195cff'
    }
}

我试图这样做:

$color: v-bind(color);

input {
    background: rgba($color, 0.5);
}

但是我认为在SASS变量中无法访问V-Bind。我该如何完成这项工作。

I'm working on a component in VueJs and I want to make a color attribute that take hexadecimal color and convert into rgb color with rgba function of sass and work with it.

props: {
    color: {
        type: String,
        default: '#195cff'
    }
}

I tried to do it like this:

$color: v-bind(color);

input {
    background: rgba($color, 0.5);
}

But I think v-bind is not accessible in sass variable. How can I do this work.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

沉默的熊 2025-02-15 06:04:38

这绝对是不可能的,但是我找到了一种获得类似结果的方法。

props: {
        color: {
            type: String,
            default: '#195cff',
        },
    },
    methods: {
        hexToRgb(hex) {
            let result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
            return result ? {
                r: parseInt(result[1], 16),
                g: parseInt(result[2], 16),
                b: parseInt(result[3], 16)
            } : null;
        }
    },
    data: function () {
        return {
            rgbColor: `${this.hexToRgb(this.color).r},${this.hexToRgb(this.color).g},${this.hexToRgb(this.color).b}`
        }
    }

和在CSS中

input {
    background: rgb(v-bind(rgbColor),0.5);
}

That's strictly impossible but I found a way to have a similar result.

props: {
        color: {
            type: String,
            default: '#195cff',
        },
    },
    methods: {
        hexToRgb(hex) {
            let result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
            return result ? {
                r: parseInt(result[1], 16),
                g: parseInt(result[2], 16),
                b: parseInt(result[3], 16)
            } : null;
        }
    },
    data: function () {
        return {
            rgbColor: `${this.hexToRgb(this.color).r},${this.hexToRgb(this.color).g},${this.hexToRgb(this.color).b}`
        }
    }

And in css

input {
    background: rgb(v-bind(rgbColor),0.5);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文