最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Vue3中使用styled-components的實(shí)現(xiàn)

 更新時(shí)間:2024年05月11日 10:49:15   作者:來(lái)一杯奶茶呀!  
本文主要介紹了Vue3中使用styled-components的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

前言

隨著組件化時(shí)代的興起,前端應(yīng)用開(kāi)始采用組件級(jí)別的 CSS 封裝:通過(guò) JavaScript 聲明和抽象樣式,以提高組件的可維護(hù)性。在組件加載時(shí)動(dòng)態(tài)加載樣式,并動(dòng)態(tài)生成類(lèi)名,從而避免全局污染。 styled-components 是其中的杰出代表。 正如其名稱(chēng)所示,styled-components 以組件的形式聲明樣式,將樣式與組件分離,實(shí)現(xiàn)邏輯組件與展示組件的分離。

styled-components 的官方 Vue 版本目前已多年沒(méi)有更新,而且只支持到 Vue2。那么,在 Vue3 中怎么才能使用到 styled-components 呢?在 Github 翻了一下,大部分復(fù)刻 vue-styled-components 的庫(kù)要么不再更新,要么沒(méi)有任何文檔和說(shuō)明。

不過(guò)還是找到一個(gè)質(zhì)量還可以的庫(kù):

vue-styled-components

查看了一下文檔,還原了大部分核心 API,使用上與官方 styled-components 差別不大。下面來(lái)看看如何使用。

使用

安裝

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>

動(dòng)畫(huà)

<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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • vue使用lodash中debounce(防抖函數(shù))的幾種方法實(shí)現(xiàn)

    vue使用lodash中debounce(防抖函數(shù))的幾種方法實(shí)現(xiàn)

    本文主要介紹了vue使用lodash中debounce(防抖函數(shù))的幾種方法實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2025-02-02
  • 在Vuex使用dispatch和commit來(lái)調(diào)用mutations的區(qū)別詳解

    在Vuex使用dispatch和commit來(lái)調(diào)用mutations的區(qū)別詳解

    今天小編就為大家分享一篇在Vuex使用dispatch和commit來(lái)調(diào)用mutations的區(qū)別詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-09-09
  • elementUI中Table表格問(wèn)題的解決方法

    elementUI中Table表格問(wèn)題的解決方法

    這篇文章主要給大家介紹了關(guān)于elementUI中Table表格問(wèn)題的解決方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2018-12-12
  • 利用FetchEventSource在大模型流式輸出的應(yīng)用方式

    利用FetchEventSource在大模型流式輸出的應(yīng)用方式

    這篇文章主要介紹了利用FetchEventSource在大模型流式輸出的應(yīng)用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-08-08
  • elementui?el-calendar日歷組件使用示例

    elementui?el-calendar日歷組件使用示例

    這篇文章主要為大家介紹了elementui?el-calendar日歷組件使用示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-12-12
  • Vue滑塊解鎖組件使用方法詳解

    Vue滑塊解鎖組件使用方法詳解

    這篇文章主要為大家詳細(xì)介紹了Vue滑塊解鎖組件的使用方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-03-03
  • VUE + UEditor 單圖片跨域上傳功能的實(shí)現(xiàn)方法

    VUE + UEditor 單圖片跨域上傳功能的實(shí)現(xiàn)方法

    這篇文章主要介紹了VUE + UEditor 單圖片跨域上傳功能的實(shí)現(xiàn)方法,需要的朋友參考下
    2018-02-02
  • 快速入門(mén)Vue

    快速入門(mén)Vue

    本篇文章主要介紹了如何快速入門(mén)Vue的方法,對(duì)0基礎(chǔ)學(xué)習(xí)Vue的朋友會(huì)很有幫助,跟著小編一起來(lái)看下吧
    2016-12-12
  • Vue.js單向綁定和雙向綁定實(shí)例分析

    Vue.js單向綁定和雙向綁定實(shí)例分析

    這篇文章主要介紹了Vue.js單向綁定和雙向綁定,結(jié)合實(shí)例形式分析了vue.js單向綁定與雙向綁定相關(guān)原理、實(shí)現(xiàn)方法及操作技巧,需要的朋友可以參考下
    2018-08-08
  • Vue-cli assets SubDirectory及PublicPath區(qū)別詳解

    Vue-cli assets SubDirectory及PublicPath區(qū)別詳解

    這篇文章主要介紹了Vue-cli assets SubDirectory及PublicPath區(qū)別詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-08-08

最新評(píng)論

海伦市| 大荔县| 大足县| 常德市| 武邑县| 肇东市| 宁陵县| 梅河口市| 阿尔山市| 佛坪县| 仪陇县| 宁化县| 武邑县| 高州市| 朝阳区| 株洲县| 张北县| 嘉祥县| 白城市| 姚安县| 拜泉县| 湘潭县| 灵山县| 通化市| 巴东县| 如皋市| 嘉善县| 潜山县| 华容县| 油尖旺区| 鄂温| 托克逊县| 丘北县| 昆明市| 屯门区| 万山特区| 密云县| 中超| 子长县| 饶平县| 固镇县|