vue如何在style標(biāo)簽中使用變量(數(shù)據(jù))詳解
參考資料
在 style 中使用 data 變量
options 方式:
<template>
<div class="text">hello</div>
</template>
<script>
export default {
data() {
return {
color: 'red'
}
}
}
</script>
<style>
.text {
color: v-bind(color);
}
</style>Composition 方式
<script setup>
const theme = {
color: 'red'
}
</script>
<template>
<p>hello</p>
</template>
<style scoped>
p {
color: v-bind('theme.color');
}
</style>還有一個(gè)問題,如果我們的變量是數(shù)字,但是我們想要設(shè)置像素怎么辦?
其實(shí)這個(gè)很好解決
一種是使用 computed 計(jì)算屬性改變它
<script setup>
import { computed } from 'vue';
const props = defineProps({ size: Number });
const sizePx = computed(() => `${props.size}px`)
</script>
<template>
<p>hello</p>
</template>
<style scoped>
p {
font-size: v-bind(sizePx);
}
</style>還有一種方式是使用 calc css 計(jì)算屬性
<script setup>
defineProps({ size: Number });
</script>
<template>
<p>hello</p>
</template>
<style scoped>
p {
font-size: calc(1px * v-bind(size));
}
</style>當(dāng)然還有第三種那就是傳值的時(shí)候就傳成字符串格式
option 方式大同小異
==========================================================================================
那么如何在代碼中使用style屬性呢?
也很簡(jiǎn)單
同樣參考
<template>
<p :class="$style.red">This should be red</p>
</template>
<style module>
.red {
color: red;
}
</style>還可以設(shè)置不同的變量
<template>
<p :class="classes1.red">This should be red</p>
</template>
<style module="classes1">
.red {
color: red;
}
</style>
<style module="classes2">
.red {
color: green;
}
</style>如果是在 script 中使用,可以使用 useCssModule
import { useCssModule } from 'vue';
const classes1 = useCssModule('classes1');
const classes2 = useCssModule('classes2');總結(jié)
到此這篇關(guān)于vue如何在style標(biāo)簽中使用變量(數(shù)據(jù))的文章就介紹到這了,更多相關(guān)vue在style標(biāo)簽使用變量?jī)?nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- vue3如何實(shí)現(xiàn)在style中使用響應(yīng)式變量
- Vue3如何利用xlsx、xlsx-js-style導(dǎo)出Excel表格使用(適合新手)
- Vue3中使用styled-components的實(shí)現(xiàn)
- 在 Vue3 中如何使用 styled-components
- Vue使用xlsx和xlsx-style導(dǎo)出表格出現(xiàn)部分樣式缺失的問題解決
- vue使用xlsx庫(kù)和xlsx-style庫(kù)導(dǎo)入導(dǎo)出excel、設(shè)置單元格背景色、文字居中、合并單元格、設(shè)置列寬
- 使用Vue綁定class和style樣式的幾種寫法總結(jié)
- 在VUE style中使用data中的變量的方法
- 在vue中:style 的使用方式匯總
相關(guān)文章
vue導(dǎo)入.md文件的步驟(markdown轉(zhuǎn)HTML)
這篇文章主要介紹了vue導(dǎo)入.md文件的步驟(markdown轉(zhuǎn)HTML),幫助大家更好的理解和使用vue框架,感興趣的朋友可以了解下2020-12-12
Vue3使用Element?Plus實(shí)現(xiàn)列表界面的方法步驟
寫后臺(tái)管理的時(shí)候會(huì)有很多列表以及相應(yīng)的條件查詢,下面這篇文章主要給大家介紹了關(guān)于Vue3使用Element?Plus實(shí)現(xiàn)列表界面的方法步驟,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-04-04
vue2.0開發(fā)實(shí)踐總結(jié)之入門篇
這篇文章主要為大家總結(jié)了vue2.0開發(fā)實(shí)踐之入門,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-12-12
使用vue初用antd 用v-model來雙向綁定Form表單問題
這篇文章主要介紹了使用vue初用antd 用v-model來雙向綁定Form表單問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-04-04
vue內(nèi)置組件keep-alive事件動(dòng)態(tài)緩存實(shí)例
這篇文章主要介紹了vue內(nèi)置組件keep-alive事件動(dòng)態(tài)緩存實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-10-10
vue項(xiàng)目中導(dǎo)入swiper插件的方法
這篇文章主要介紹了vue項(xiàng)目中導(dǎo)入swiper插件的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-01-01

