Vue3中使用styled-components的實(shí)現(xiàn)
前言
隨著組件化時(shí)代的興起,前端應(yīng)用開始采用組件級別的 CSS 封裝:通過 JavaScript 聲明和抽象樣式,以提高組件的可維護(hù)性。在組件加載時(shí)動態(tài)加載樣式,并動態(tài)生成類名,從而避免全局污染。 styled-components 是其中的杰出代表。 正如其名稱所示,styled-components 以組件的形式聲明樣式,將樣式與組件分離,實(shí)現(xiàn)邏輯組件與展示組件的分離。
styled-components 的官方 Vue 版本目前已多年沒有更新,而且只支持到 Vue2。那么,在 Vue3 中怎么才能使用到 styled-components 呢?在 Github 翻了一下,大部分復(fù)刻 vue-styled-components 的庫要么不再更新,要么沒有任何文檔和說明。
不過還是找到一個質(zhì)量還可以的庫:
查看了一下文檔,還原了大部分核心 API,使用上與官方 styled-components 差別不大。下面來看看如何使用。
使用
安裝
npm i @vvibe/vue-styled-components
styled原生組件
<script setup lang="ts">
import { styled } from '@vvibe/vue-styled-components'
const StyledLink = styled('a')`
color: darkred;
`
</script>
<template>
<StyledLink>鏈接</StyledLink>
</template>
也可以直接鏈?zhǔn)秸{(diào)用
<script setup lang="ts">
import { styled } from '@vvibe/vue-styled-components'
const StyledLink = styled.a`
color: darkred;
`
</script>
<template>
<StyledLink>鏈接</StyledLink>
</template>
響應(yīng)式樣式
<script setup lang="ts">
import { ref } from 'vue'
import { styled } from '@vvibe/vue-styled-components'
const borderColor = ref('darkred')
const inputProps = { borderColor: String }
const StyledInput = styled('input', inputProps)`
width: 100%;
height: 40px;
padding: 4px 8px;
border: 1px solid ${(props) => props.borderColor};
border-radius: 8px;
`
const input = () => (borderColor.value = 'forestgreen')
const focus = () => (borderColor.value = 'skyblue ')
const blur = () => (borderColor.value = 'darkred')
</script>
<template>
<StyledInput placeholder="Type something" :borderColor="borderColor" @input="input" @focus="focus" @blur="blur" />
</template>
擴(kuò)展樣式
<script setup lang="ts">
import { styled } from '@vvibe/vue-styled-components';
const BlueButton = styled.button`
width: 120px;
height: 40px;
margin-right: 8px;
padding: 4px 8px;
border-radius: 9999px;
box-shadow: 0 0 4px rgba(0, 0, 0, 0.3);
background-color: skyblue;
font-weight: bold;
`;
const RedButton = styled(BlueButton)`
background-color: darkred;
color: white;
`;
</script>
<template>
<BlueButton>Blue Button</BlueButton>
<RedButton>Red Button</RedButton>
</template>
動畫
<script setup lang="ts">
import { styled, keyframes } from '@vvibe/vue-styled-components'
const rotate = keyframes`
from {
transform: rotate(0deg);
}
to {
transform: rotate(360deg);
}
`
const translate = keyframes`
0 {
transform: translateX(0);
}
50% {
transform: translateX(250%);
}
60% {
transform: rotate(360deg);
}
`
const StyledBaseDiv = styled.div`
display: inline-block;
width: 100px;
height: 100px;
`
const StyledRotateDiv = styled(StyledBaseDiv)`
background-color: skyblue;
animation: ${rotate} 2s linear infinite;
`
const StyledTranslateDiv = styled(StyledBaseDiv)`
margin-left: 10px;
background-color: darkred;
animation: ${translate} 2s ease infinite alternate;
`
</script>
<template>
<StyledRotateDiv />
<StyledTranslateDiv />
</template>
主題
<script setup lang="ts">
import { styled, ThemeProvider } from '@vvibe/vue-styled-components'
const Wrapper = styled.div`
display: flex;
justify-content: space-around;
`
const StyledLink = styled.a`
margin-right: 8px;
color: ${(props) => props.theme.primary};
font-weight: bold;
`
</script>
<template>
<Wrapper>
<a>This a normal link</a>
<ThemeProvider :theme="{ primary: 'red' }">
<StyledLink>This a theming link</StyledLink>
</ThemeProvider>
</Wrapper>
</template>
更多細(xì)節(jié)查看文檔:https://vue-styled-components.com
到此這篇關(guān)于Vue3中使用styled-components的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)Vue3 styled-components內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- vue3如何實(shí)現(xiàn)在style中使用響應(yīng)式變量
- Vue3如何利用xlsx、xlsx-js-style導(dǎo)出Excel表格使用(適合新手)
- 在 Vue3 中如何使用 styled-components
- Vue使用xlsx和xlsx-style導(dǎo)出表格出現(xiàn)部分樣式缺失的問題解決
- vue使用xlsx庫和xlsx-style庫導(dǎo)入導(dǎo)出excel、設(shè)置單元格背景色、文字居中、合并單元格、設(shè)置列寬
- 使用Vue綁定class和style樣式的幾種寫法總結(jié)
- vue如何在style標(biāo)簽中使用變量(數(shù)據(jù))詳解
- 在VUE style中使用data中的變量的方法
- 在vue中:style 的使用方式匯總
相關(guān)文章
vue3中的reactive、readonly和shallowReactive使用詳解
在 Vue3 中,可以使用 shallowReactive 函數(shù)創(chuàng)建一個淺層響應(yīng)式對象,它接收一個普通對象作為參數(shù),返回一個淺層響應(yīng)式代理對象,本文給大家介紹vue3中的reactive、readonly和shallowReactive使用,感興趣的朋友跟隨小編一起看看吧2024-04-04
vue實(shí)現(xiàn)時(shí)間倒計(jì)時(shí)功能
這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)時(shí)間倒計(jì)時(shí)功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-08-08
el-form表單實(shí)現(xiàn)校驗(yàn)的示例代碼
本文主要介紹了el-form表單實(shí)現(xiàn)校驗(yàn)的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-07-07
Vue項(xiàng)目通過network的ip地址訪問注意事項(xiàng)及說明
這篇文章主要介紹了Vue項(xiàng)目通過network的ip地址訪問注意事項(xiàng)及說明,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-09-09
vue+element-ui實(shí)現(xiàn)表格編輯的三種實(shí)現(xiàn)方式
這篇文章主要介紹了vue+element-ui實(shí)現(xiàn)表格編輯的三種實(shí)現(xiàn)方式,主要有表格內(nèi)部顯示和編輯切換,通過彈出另外一個表格編輯和直接通過樣式控制三種方式,感興趣的小伙伴們可以參考一下2018-10-10
vue學(xué)習(xí)筆記之過濾器的基本使用方法實(shí)例分析
這篇文章主要介紹了vue學(xué)習(xí)筆記之過濾器的基本使用方法,結(jié)合實(shí)例形式分析了vue.js過濾器的基本功能、用法與操作注意事項(xiàng),需要的朋友可以參考下2020-02-02

