gpt4 book ai didi

javascript - 将 css 注入(inject) Vue 组件?

转载 作者:数据小太阳 更新时间:2023-10-29 05:28:37 24 4
gpt4 key购买 nike

见下文 my-component.vue。此组件需要可主题化。

它需要获取外部 css 表,因为我需要允许其他开发人员自定义其内部外观。

除了accept a javascript object还有其他方法吗? ?

<template>
<div class="container">
<div class="A"></div>
<div class="B"></div>
<div class="C"></div>
</div>
</template>

<style scoped>
.A {
background-color:green;
}
.B {
background-color: red;
}
.C {
background-color: yellow
}
</style>

最佳答案

在你的组件中:

<template>
<div :class="boxStyle"></div>
</template>

<script>
export default {
props: {
boxStyle: {
type: String,
default: 'box-default'
}
}
}
</script>

<style lang="scss" scoped>
.box-default {
width: 100px;
height: 100px;
background: black;
}
</style>

用户可以这样使用它:

<template>
<div id="app">
<my :boxStyle="'mySpecialBox'"></my>
</div>
</template>


<script>

import myCompoentns from './components/myComponent.vue'

export default {
name: 'app',
components: {
'my': myCompoentns
},
}
</script>

<style lang="scss">
.mySpecialBox {
width: 250px;
height: 250px;
background: red;
}
</style>

这样,用户可以定义任何他想要的样式,任何他想要的类名。他只需要使用适当的属性名称。作用域样式没有副作用。

关于javascript - 将 css 注入(inject) Vue 组件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45280859/

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