Jah*_*son 2 javascript css sass vue.js bootstrap-4
我目前正在尝试允许使用引导程序对我的组件进行自定义主题。我想在 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>
Run Code Online (Sandbox Code Playgroud)
因此,我正在寻找一种方法,可以根据组件收到的道具来自定义十六进制代码。感谢您的帮助。
从评论输入后进行编辑,这是尝试过的,但没有成功:
<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>
Run Code Online (Sandbox Code Playgroud)
虽然在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
Run Code Online (Sandbox Code Playgroud)
我尝试了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>
Run Code Online (Sandbox Code Playgroud)
我正在从 package.json 导入 bootstrap;"bootstrap": "4.4.1"