Min*_*Wen 27 css vue.js vue-component vuejs2
我是vue.js的新手.这是我的问题:
在像这样的*.vue文件中:
<template>
<div id="a">
</div>
</template>
<script>
export default {
name: 'SquareButton',
props: ['color']
}
</script>
<style scoped>
#a {
background-color: ?
}
<style>
Run Code Online (Sandbox Code Playgroud)
我怎样才能使用道具(现在color在background-color:哪里?).
谢谢.
Gui*_*del 44
我知道我们在这里谈论的是 vue 2,但如果任何来自 vue 3 的人遇到这个问题(就像我一样),vue 3 引入了一种更简洁的方法来做到这一点:
<template>
<div id="a">
</div>
</template>
<script>
export default {
name: 'SquareButton',
props: ['color']
}
</script>
<style scoped>
#a {
background-color: v-bind(color);
}
<style>
Run Code Online (Sandbox Code Playgroud)
Vue 实际上在幕后所做的事情与“通过组件的样式过程引入 css 变量”相同,但现在看起来确实好多了。
文档来源:https://vuejs.org/api/sfc-css-features.html#v-bind-in-css
Pot*_*ray 33
你没有.你使用一个计算属性,你使用prop来返回div的样式,如下所示:
<template>
<div id="a" :style="style" @mouseover="mouseOver()">
</div>
</template>
<script>
export default {
name: 'SquareButton',
props: ['color'],
computed: {
style () {
return 'background-color: ' + this.hovering ? this.color: 'red';
}
},
data () {
return {
hovering: false
}
},
methods: {
mouseOver () {
this.hovering = !this.hovering
}
}
}
</script>
<style scoped>
<style>
Run Code Online (Sandbox Code Playgroud)
小智 31
由于我们现在在 2020 年,我建议将此技巧与名为的 css 函数一起使用 var
<template>
<div id="a" :style="cssVars"></div>
</template>
<script>
export default {
props: ['color'],
computed: {
cssVars () {
return{
/* variables you want to pass to css */
'--color': this.color,
}
}
}
<script>
<style scoped>
#a{
background-color: var(--color);
}
</style>
Run Code Online (Sandbox Code Playgroud)
此方法非常有用,因为它允许您稍后通过 css 更新传递的值(例如,当您应用悬停事件时)。
小智 15
您实际上可以!
您应该在计算属性中定义CSS变量,然后将计算属性作为需要CSS变量的元素的样式来调用,最后可以在文档底部的标记中使用该变量。
在这里看看:https : //codepen.io/richardtallent/pen/yvpERW/
some code to make the link work.
Run Code Online (Sandbox Code Playgroud)
并在这里:https : //github.com/vuejs/vue/issues/7346
phe*_*non 15
为什么不:style以这种方式使用prop:
<template>
<div :style="{ backgroundColor: color }">
</template>
<script>
export default {
props: {
color: {
type: String,
default: ''
}
}
}
</script>
Run Code Online (Sandbox Code Playgroud)
确保以驼峰式风格定义 css 属性。
Vue 3 添加了绑定样式的新方法,因此现在您可以轻松地将 props 绑定到 css 属性。
阅读来源: https://learnvue.co/2021/05/how-to-use-vue-css-variables-reactive-styles-rfc/
<template>
<div>
<div class="text">hello</div>
</div>
</template>
<script>
export default {
data() {
return {
color: 'red',
}
}
}
</script>
<style>
.text {
color: v-bind(color);
}
</style>
Run Code Online (Sandbox Code Playgroud)
如果您需要无法通过伪类或媒体查询等样式属性应用的css,请执行以下操作:
初始化Vue时创建一个全局可用的样式组件(否则将需要使用它,否则会遇到棉绒问题)。它创建一个样式标签,该标签仅呈现广告位中的内容:
仅当您确实需要在css和css功能中同时使用不能应用于样式属性的动态值时,才使用此功能。
import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
Vue.config.productionTip = false
Vue.component('v-style', {
render: function(createElement) {
return createElement('style', this.$slots.default)
}
})
new Vue({
router,
store,
render: h => h(App)
}).$mount('#app')
Run Code Online (Sandbox Code Playgroud)
然后像这样在模板的顶部使用它,您将获得组件的完整JavaScript范围和完整的CSS语法:
<template>
<v-style>
@media screen and (max-width: 820px) {
.gwi-text-media-{{ this.id }} {
background-image: url({{ mobileThumb }});
}
}
</v-style>
</template>
Run Code Online (Sandbox Code Playgroud)
对我来说似乎有点hacker,但这确实起作用,在某些情况下,我宁愿这样,而不是必须为鼠标悬停或调整大小的事件添加额外的JS,而这些事件可能会降低您的应用程序性能。
您可以利用 CSSvar(--foo-bar)功能。如果您尝试传递具有自己的动态路径的资产(如 Shopify 那样),它也很有用。
此方法也适用于:before和:after元素的样式,因为它们引用了应用于所有者元素的样式。
使用原始帖子示例传递颜色:
<template>
<div
id="a"
:style="{ '--colour': color }">
</div>
</template>
<script>
export default {
name: 'SquareButton',
props: ['color']
}
</script>
<style scoped>
#a {
background-color: var(--colour);
}
</style>
Run Code Online (Sandbox Code Playgroud)
使用原始帖子示例传递 URL:
<template>
<div
id="a"
:style="{ '--image-url': 'url(' + image + ')' }">
</div>
</template>
<script>
export default {
name: 'SquareButton',
props: ['image']
}
</script>
<style scoped>
#a {
background-url: var(--image-url);
}
</style>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
20370 次 |
| 最近记录: |