gpt4 book ai didi

javascript - Vue 是否可以使用 Prop 进行造型? (SCSS/SASS)

转载 作者:行者123 更新时间:2023-12-02 16:39:45 26 4
gpt4 key购买 nike

我目前正在尝试允许使用 bootstrap 的组件自定义主题。我想在 Vue 组件部分的 SCSS 中设置它的 $primary 标签,例如,目前我的样式如下所示:

<style scoped lang="scss">
$primary: #FF1493;
// Bootstrap and its default variables
@import "../../node_modules/bootstrap/scss/bootstrap";
// BootstrapVue and its default variables
@import "../../node_modules/bootstrap-vue/src/index.scss";

@import "src/assets/custom.scss";
</style>

所以我正在寻找一种方法来根据组件收到的 Prop 自定义十六进制代码。感谢您的帮助。

在尝试输入评论后进行编辑,但没有成功:

<template>
<div style="style="{ '--test': #ff1493 }"">
</div>
</template>

<style scoped lang="scss">
$primary: var(--test);
// Bootstrap and its default variables
@import "../../node_modules/bootstrap/scss/bootstrap";
// BootstrapVue and its default variables
@import "../../node_modules/bootstrap-vue/src/index.scss";

@import "src/assets/custom.scss";
</style>

尽管在 SCSS 中导致以下编译错误:

SassError: argument `$color` of `darken($color, $amount)` must be a color
on line 181 of node_modules/bootstrap/scss/_variables.scss, in function `darken`
from line 181 of node_modules/bootstrap/scss/_variables.scss
from line 9 of node_modules/bootstrap/scss/bootstrap.scss
from line 201 of D:\Documents\Code\SiliconGit\src\components\SiDialog.vue

最佳答案

我尝试了来自 Emad Ahmed 的答案,对我来说效果很好。
我的组件看起来像;

<template>
<div id="a" :style="cssVars">
<p>Hello there</p>
</div>
</template>

<script>
export default {
name: "css-change",
props: {
init_color: {
required: false,
type: String,
default: '#ff0000'
}
},
data() {
return {
color: this.init_color
}
},
computed: {
cssVars () {
return{
/* variables you want to pass to css */
'--color': this.color,
}
}
}
}
</script>

<style lang="scss" scoped>
#a{
background-color: var(--color);
}
</style>

我正在从我的 package.json 中导入 Bootstrap ; “ Bootstrap ”:“4.4.1”

关于javascript - Vue 是否可以使用 Prop 进行造型? (SCSS/SASS),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61935450/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com