admin管理员组文章数量:1356843
see below my-ponent.vue
.This Component needs to be themable.
It needs to get extrenal css sheet as i need to allow other developers to customize its inner look.
Is there any other approach than 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>
see below my-ponent.vue
.This Component needs to be themable.
It needs to get extrenal css sheet as i need to allow other developers to customize its inner look.
Is there any other approach than 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>
Share
Improve this question
edited Jul 14, 2022 at 0:57
tony19
139k23 gold badges277 silver badges347 bronze badges
asked Jul 24, 2017 at 12:38
LiranCLiranC
2,4802 gold badges30 silver badges56 bronze badges
3
- why this approach is not good? – V. Sambor Commented Jul 24, 2017 at 12:40
- i have experience with css in javascript from react. maybe it's ok, but it's not perfect and not intuitive to vue developers – LiranC Commented Jul 24, 2017 at 12:43
- Don't use scoped CSS and name the classes .ponent__class. – NikxDa Commented Jul 24, 2017 at 13:20
2 Answers
Reset to default 4Inside your ponent:
<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>
the user could use it that way:
<template>
<div id="app">
<my :boxStyle="'mySpecialBox'"></my>
</div>
</template>
<script>
import myCompoentns from './ponents/myComponent.vue'
export default {
name: 'app',
ponents: {
'my': myCompoentns
},
}
</script>
<style lang="scss">
.mySpecialBox {
width: 250px;
height: 250px;
background: red;
}
</style>
That way, the user can define any style he wants, to any class name he wishes. He just need to use the appropriate property name. The scoped styling has no side effects.
You can use "deep" selector to override any styles inside "scoped" ponent styles.
<style scoped>
.a >>> .b { /* ... */ }
</style>
Details: vue-loader/deep-selector
本文标签: javascriptInject css to a Vue componentStack Overflow
版权声明:本文标题:javascript - Inject css to a Vue component? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744002420a2574102.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论