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

在 Vue3 中如何使用 styled-components

 更新時間:2024年05月11日 10:35:15   作者:來一杯奶茶呀!  
styled-components 的官方 Vue 版本目前已多年沒有更新,而且只支持到 Vue2,那么,在 Vue3 中怎么才能使用到 styled-components 呢,下面給大家介紹在 Vue3 中使用 styled-components的相關知識,感興趣的朋友跟隨小編一起看看吧

前言

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

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

不過還是找到一個質量還可以的庫:

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>

也可以直接鏈式調用

<script setup lang="ts">
import { styled } from '@vvibe/vue-styled-components'
const StyledLink = styled.a`
  color: darkred;
`
</script>
<template>
  <StyledLink>鏈接</StyledLink>
</template>

響應式樣式

<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

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

相關文章

  • Vue自定義指令詳解

    Vue自定義指令詳解

    這篇文章主要為大家詳細介紹了Vue自定義指令的相關資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-07-07
  • VUE中$refs的基本用法舉例

    VUE中$refs的基本用法舉例

    ref 加在子組件上,用this.$refs.(ref值) 獲取到的是組件實例,可以使用組件的所有方法,?在使用方法的時候直接this.$refs.(ref值).方法() 就可以使用了,這篇文章主要介紹了VUE中$refs的基本用法,需要的朋友可以參考下
    2022-12-12
  • 基于vue.js輪播組件vue-awesome-swiper實現(xiàn)輪播圖

    基于vue.js輪播組件vue-awesome-swiper實現(xiàn)輪播圖

    一般做移動端輪播圖的時候,最常用的就是Swiper插件了,而vue.js也有一個輪播組件vue-awesome-swiper,用法跟swiper相似。接下來通過本文給大家詳解講解vue.js輪播組件vue-awesome-swiper實現(xiàn)輪播圖實例代碼,需要的朋友參考下
    2017-03-03
  • Vue加載組件、動態(tài)加載組件的幾種方式

    Vue加載組件、動態(tài)加載組件的幾種方式

    組件是Vue.js最強大的功能之一。組件可以擴展HTML元素,封裝可重用的代碼。這篇文章通過實例代碼給大家介紹了Vue加載組件、動態(tài)加載組件的幾種方式,需要的朋友參考下吧
    2018-08-08
  • vue-video-player視頻播放器使用配置詳解

    vue-video-player視頻播放器使用配置詳解

    這篇文章主要為大家詳細介紹了vue-video-player視頻播放器的使用和配置,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-10-10
  • vuejs如何解決瀏覽器切換頁面后setInterval計時器停止執(zhí)行的問題

    vuejs如何解決瀏覽器切換頁面后setInterval計時器停止執(zhí)行的問題

    setinterval()是定時調用的函數(shù),可按照指定的周期(以毫秒計)來調用函數(shù)或計算表達式,這篇文章主要給大家介紹了關于vuejs如何解決瀏覽器切換頁面后setInterval計時器停止執(zhí)行的問題,需要的朋友可以參考下
    2024-01-01
  • vue實現(xiàn)二級導航欄效果

    vue實現(xiàn)二級導航欄效果

    這篇文章主要為大家詳細介紹了vue實現(xiàn)二級導航欄效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-10-10
  • vue-router:嵌套路由的使用方法

    vue-router:嵌套路由的使用方法

    本篇文章主要介紹了vue-router:嵌套路由的使用方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-02-02
  • Vue3實現(xiàn)全局公共函數(shù)的完整方案(創(chuàng)建、掛載、引用、使用)

    Vue3實現(xiàn)全局公共函數(shù)的完整方案(創(chuàng)建、掛載、引用、使用)

    本文詳細介紹了如何在Vue3項目中創(chuàng)建、掛載、引用和使用全局公共函數(shù),包括標準目錄結構、創(chuàng)建公共函數(shù)、全局注冊、頁面使用以及規(guī)范,通過本文,新手開發(fā)者可以快速搭建企業(yè)級規(guī)范工具庫,需要的朋友可以參考下
    2026-03-03
  • Vue和React中diff算法的區(qū)別及說明

    Vue和React中diff算法的區(qū)別及說明

    React和Vue都使用虛擬DOM和diff算法來更新DOM,但它們在實現(xiàn)上有所不同,React采用基于索引的比較,Vue采用雙端比較算法,React在比較時不復用不同類型的節(jié)點,而Vue會優(yōu)先復用兩端相同的節(jié)點,React對key的依賴較高,而Vue在沒有key時也能通過雙端比較優(yōu)化
    2025-03-03

最新評論

台东市| 湘西| 察雅县| 尼勒克县| 内乡县| 乌鲁木齐县| 桦川县| 芒康县| 盐边县| 北川| 三明市| 敦化市| 惠水县| 博乐市| 顺义区| 乐至县| 清徐县| 瑞安市| 岢岚县| 清河县| 邢台县| 望谟县| 南陵县| 怀化市| 福清市| 凤阳县| 海宁市| 万山特区| 上蔡县| 黎川县| 南安市| 荣昌县| 门源| 图们市| 西昌市| 朝阳县| 红原县| 颍上县| 怀化市| 藁城市| 永宁县|