欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

在 Vue3 中如何使用 styled-components

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

前言

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

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

不過還是找到一個(gè)質(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>

也可以直接鏈?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)畫

<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的文章就介紹到這了,更多相關(guān)Vue3使用 styled-components內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Vue實(shí)現(xiàn)Google第三方登錄的示例代碼

    Vue實(shí)現(xiàn)Google第三方登錄的示例代碼

    本文記錄作者在vue項(xiàng)目中使用到Google第三方登錄,查詢到的資料文檔也不詳細(xì),故此把自己所遇到的坑及問題詳細(xì)的記錄下來。
    2021-07-07
  • Vue如何實(shí)現(xiàn)自動(dòng)觸發(fā)功能

    Vue如何實(shí)現(xiàn)自動(dòng)觸發(fā)功能

    這篇文章主要介紹了Vue如何實(shí)現(xiàn)自動(dòng)觸發(fā)功能,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-01-01
  • vue3.0公共組件自動(dòng)導(dǎo)入的方法實(shí)例

    vue3.0公共組件自動(dòng)導(dǎo)入的方法實(shí)例

    這篇文章主要給大家介紹了關(guān)于vue3.0公共組件自動(dòng)導(dǎo)入的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-04-04
  • elementUI select組件value值注意事項(xiàng)詳解

    elementUI select組件value值注意事項(xiàng)詳解

    這篇文章主要介紹了elementUI select組件value值注意事項(xiàng)詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-05-05
  • 使用Bootstrap4 + Vue2實(shí)現(xiàn)分頁查詢的示例代碼

    使用Bootstrap4 + Vue2實(shí)現(xiàn)分頁查詢的示例代碼

    本篇文章主要介紹了使用Bootstrap4 + Vue2實(shí)現(xiàn)分頁查詢的示例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-12-12
  • Vue.Draggable拖拽功能的配置使用方法

    Vue.Draggable拖拽功能的配置使用方法

    這篇文章主要為大家詳細(xì)介紹了Vue.Draggable拖拽功能配置使用的方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-12-12
  • Vue集成three.js并加載glb、gltf、FBX、json模型的場(chǎng)景分析

    Vue集成three.js并加載glb、gltf、FBX、json模型的場(chǎng)景分析

    這篇文章主要介紹了Vue集成three.js,并加載glb、gltf、FBX、json模型,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-09-09
  • 老生常談Vue中的偵聽器watch

    老生常談Vue中的偵聽器watch

    開發(fā)中我們?cè)赿ata返回的對(duì)象中定義了數(shù)據(jù),這個(gè)數(shù)據(jù)通過插值語法等方式綁定到template中,這篇文章主要介紹了Vue中的偵聽器watch,需要的朋友可以參考下
    2022-10-10
  • Vue3基于rem比例H5縮放方案示例詳解

    Vue3基于rem比例H5縮放方案示例詳解

    這篇文章主要為大家介紹了Vue3基于rem比例H5縮放方案示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-11-11
  • Electron自動(dòng)更新失效報(bào)錯(cuò)Error:?Object?has?been?destroyed的問題解決

    Electron自動(dòng)更新失效報(bào)錯(cuò)Error:?Object?has?been?destroyed的問題解決

    本文主要講解如何解決?Error:?Object?has?been?destroyed?這個(gè)?Electron?中最常見的問題,以及?Electron?自動(dòng)更新的流程,文中通過代碼示例給大家講解的非常詳細(xì),需要的朋友可以參考下
    2024-01-01

最新評(píng)論