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

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

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

前言

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

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

不過還是找到一個質(zhì)量還可以的庫:

vue-styled-components

查看了一下文檔,還原了大部分核心 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>

也可以直接鏈式調(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>

擴展樣式

<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>

更多細節(jié)查看文檔:https://vue-styled-components.com

到此這篇關(guān)于Vue3中使用styled-components的實現(xiàn)的文章就介紹到這了,更多相關(guān)Vue3 styled-components內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

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

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

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

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

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

    elementUI中Table表格問題的解決方法

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

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

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

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

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

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

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

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

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

    快速入門Vue

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

    Vue.js單向綁定和雙向綁定實例分析

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

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

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

最新評論

习水县| 平山县| 津市市| 田阳县| 图片| 长泰县| 思南县| 阳西县| 嘉黎县| 当雄县| 寿宁县| 宁德市| 保亭| 侯马市| 抚顺县| 启东市| 白山市| 台东县| 东阿县| 顺义区| 开远市| 长葛市| 岫岩| 通化市| 宁津县| 喜德县| 诸城市| 巴中市| 太白县| 小金县| 达拉特旗| 曲麻莱县| 会东县| 上虞市| 阿克| 宁远县| 松阳县| 称多县| 桂平市| 班戈县| 佛坪县|