溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

vue內(nèi)置組件怎么使用

發(fā)布時(shí)間:2022-12-27 09:10:22 來(lái)源:億速云 閱讀:114 作者:iii 欄目:web開(kāi)發(fā)

這篇文章主要介紹了vue內(nèi)置組件怎么使用的相關(guān)知識(shí),內(nèi)容詳細(xì)易懂,操作簡(jiǎn)單快捷,具有一定借鑒價(jià)值,相信大家閱讀完這篇vue內(nèi)置組件怎么使用文章都會(huì)有所收獲,下面我們一起來(lái)看看吧。

vue組件有:1、component,用于渲染一個(gè)“元組件”為動(dòng)態(tài)組件。2、transition,用于為單個(gè)元素或組件提供動(dòng)畫(huà)過(guò)渡效果。3、transition-group,用于為列表中的多個(gè)元素或組件提供過(guò)渡效果。4、keep-alive,用于緩存包裹在其中的動(dòng)態(tài)切換組件。5、slot。6、teleport,用于將其插槽內(nèi)容渲染到DOM中的另一個(gè)位置。7、Suspense。

內(nèi)置組件無(wú)需注冊(cè)便可以直接在模板中使用。它們也是 tree-shakeable 的:僅在使用時(shí)才會(huì)包含在構(gòu)建中。

在渲染函數(shù)中使用它們時(shí),需要顯式導(dǎo)入。例如:

import { h, Transition } from 'vue'

h(Transition, {
  /* props */
})

1、component

  • Props:

    is - string | Component

  • 用法:

渲染一個(gè)“元組件”為動(dòng)態(tài)組件。依 is 的值,來(lái)決定哪個(gè)組件被渲染。is 的值是一個(gè)字符串,它既可以是 HTML 標(biāo)簽名稱也可以是組件名稱。

  <!--  動(dòng)態(tài)組件由 vm 實(shí)例的 `componentId` property 控制 -->
  <component :is="componentId"></component>
  
  <!-- 也能夠渲染注冊(cè)過(guò)的組件或 prop 傳入的組件-->
  <component :is="$options.components.child"></component>
  
  <!-- 可以通過(guò)字符串引用組件 -->
  <component :is="condition ? 'FooComponent' : 'BarComponent'"></component>
  
  <!-- 可以用來(lái)渲染原生 HTML 元素 -->
  <component :is="href ? 'a' : 'span'"></component>

2、transition

  • Props:

    name - string 用于自動(dòng)生成 CSS 過(guò)渡類名。例如:name: 'fade' 將自動(dòng)拓展為 .fade-enter,.fade-enter-active 等。

    appear - boolean,是否在初始渲染時(shí)使用過(guò)渡。默認(rèn)為 false。

    persisted - boolean。如果是 true,表示這是一個(gè)不真實(shí)插入/刪除元素的轉(zhuǎn)換,而是切換顯示/隱藏狀態(tài)。過(guò)渡鉤子被注入,但渲染器將跳過(guò)。相反,自定義指令可以通過(guò)調(diào)用注入的鉤子 (例如 v-show) 來(lái)控制轉(zhuǎn)換。

    css - boolean。是否使用 CSS 過(guò)渡類。默認(rèn)為 true。如果設(shè)置為 false,將只通過(guò)組件事件觸發(fā)注冊(cè)的 JavaScript 鉤子。

    type - string。指定過(guò)渡事件類型,偵聽(tīng)過(guò)渡何時(shí)結(jié)束。有效值為 "transition""animation"。默認(rèn) Vue.js 將自動(dòng)檢測(cè)出持續(xù)時(shí)間長(zhǎng)的為過(guò)渡事件類型。

    mode - string 控制離開(kāi)/進(jìn)入過(guò)渡的時(shí)間序列。有效的模式有 "out-in""in-out";默認(rèn)同時(shí)進(jìn)行。

    duration - number | { enter : number, leave : number }。指定過(guò)渡的持續(xù)時(shí)間。默認(rèn)情況下,Vue 會(huì)等待過(guò)渡所在根元素的第一個(gè) transitionendanimationend 事件。

    enter-from-class - string

    leave-from-class - string

    appear-class - string

    enter-to-class - string

    leave-to-class - string

    appear-to-class - string

    enter-active-class - string

    leave-active-class - string

    appear-active-class - string

  • 事件:

    before-enter

    before-leave

    enter

    leave

    appear

    after-enter

    after-leave

    after-appear

    enter-cancelled

    leave-cancelled (僅 v-show)

    appear-cancelled

  • 用法:

<transition> 元素作為單個(gè)元素/組件的過(guò)渡效果。<transition> 只會(huì)把過(guò)渡效果應(yīng)用到其包裹的內(nèi)容上,而不會(huì)額外渲染 DOM 元素,也不會(huì)出現(xiàn)在可被檢查的組件層級(jí)中。

  <!--  動(dòng)態(tài)組件由 vm 實(shí)例的 `componentId` property 控制 -->
  <component :is="componentId"></component>
  
  <!-- 也能夠渲染注冊(cè)過(guò)的組件或 prop 傳入的組件-->
  <component :is="$options.components.child"></component>
  
  <!-- 可以通過(guò)字符串引用組件 -->
  <component :is="condition ? 'FooComponent' : 'BarComponent'"></component>
  
  <!-- 可以用來(lái)渲染原生 HTML 元素 -->
  <component :is="href ? 'a' : 'span'"></component>
  const app = Vue.createApp({
    ...
    methods: {
      transitionComplete (el) {
        // 因?yàn)閭鬟f了'el'的DOM元素作為參數(shù)
      }
    }
    ...
  })
  
  app.mount('#transition-demo')

3、transition-group

  • Props:

    tag - string,默認(rèn)為 span。

    move-class - 覆蓋移動(dòng)過(guò)渡期間應(yīng)用的 CSS 類。

    除了 mode,其他 attribute 和 <transition> 相同。

  • 事件:

    事件和 <transition> 相同。

  • 用法:

<transition-group> 元素作為多個(gè)元素/組件的過(guò)渡效果。<transition-group> 渲染一個(gè)真實(shí)的 DOM 元素。默認(rèn)渲染 <span>,可以通過(guò) tag attribute 配置哪個(gè)元素應(yīng)該被渲染。

注意,每個(gè) <transition-group> 的子節(jié)點(diǎn)必須有獨(dú)立的 key,動(dòng)畫(huà)才能正常工作

<transition-group> 支持通過(guò) CSS transform 過(guò)渡移動(dòng)。當(dāng)一個(gè)子節(jié)點(diǎn)被更新,從屏幕上的位置發(fā)生變化,它會(huì)被應(yīng)用一個(gè)移動(dòng)中的 CSS 類 (通過(guò) name attribute 或配置 move-class attribute 自動(dòng)生成)。如果 CSS transform property 是“可過(guò)渡”property,當(dāng)應(yīng)用移動(dòng)類時(shí),將會(huì)使用 FLIP 技術(shù)使元素流暢地到達(dá)動(dòng)畫(huà)終點(diǎn)。

  <transition-group tag="ul" name="slide">
    <li v-for="item in items" :key="item.id">
      {{ item.text }}
    </li>
  </transition-group>

4、keep-alive

  • Props:

    include - string | RegExp | Array。只有名稱匹配的組件會(huì)被緩存。

    exclude - string | RegExp | Array。任何名稱匹配的組件都不會(huì)被緩存。

    max - number | string。最多可以緩存多少組件實(shí)例。

  • 用法:

<keep-alive> 包裹動(dòng)態(tài)組件時(shí),會(huì)緩存不活動(dòng)的組件實(shí)例,而不是銷(xiāo)毀它們。和 <transition> 相似,<keep-alive> 是一個(gè)抽象組件:它自身不會(huì)渲染一個(gè) DOM 元素,也不會(huì)出現(xiàn)在組件的父組件鏈中。

當(dāng)組件在 <keep-alive> 內(nèi)被切換,它的 activateddeactivated 這兩個(gè)生命周期鉤子函數(shù)將會(huì)被對(duì)應(yīng)執(zhí)行。

主要用于保留組件狀態(tài)或避免重新渲染。

  <!-- 基本 -->
  <keep-alive>
    <component :is="view"></component>
  </keep-alive>
  
  <!-- 多個(gè)條件判斷的子組件 -->
  <keep-alive>
    <comp-a v-if="a > 1"></comp-a>
    <comp-b v-else></comp-b>
  </keep-alive>
  
  <!-- 和 `<transition>` 一起使用 -->
  <transition>
    <keep-alive>
      <component :is="view"></component>
    </keep-alive>
  </transition>

注意,<keep-alive> 是用在其一個(gè)直屬的子組件被切換的情形。如果你在其中有 v-for 則不會(huì)工作。如果有上述的多個(gè)條件性的子元素,<keep-alive> 要求同時(shí)只有一個(gè)子元素被渲染。

  • includeexclude

The includeexclude prop 允許組件有條件地緩存。二者都可以用逗號(hào)分隔字符串、正則表達(dá)式或一個(gè)數(shù)組來(lái)表示:

  <!-- 逗號(hào)分隔字符串 -->
  <keep-alive include="a,b">
    <component :is="view"></component>
  </keep-alive>
  
  <!-- regex (使用 `v-bind`) -->
  <keep-alive :include="/a|b/">
    <component :is="view"></component>
  </keep-alive>
  
  <!-- Array (使用 `v-bind`) -->
  <keep-alive :include="['a', 'b']">
    <component :is="view"></component>
  </keep-alive>

匹配首先檢查組件自身的 name 選項(xiàng),如果 name 選項(xiàng)不可用,則匹配它的局部注冊(cè)名稱 (父組件 components 選項(xiàng)的鍵值)。匿名組件不能被匹配。

  • max

最多可以緩存多少組件實(shí)例。一旦這個(gè)數(shù)字達(dá)到了,在新實(shí)例被創(chuàng)建之前,已緩存組件中最久沒(méi)有被訪問(wèn)的實(shí)例會(huì)被銷(xiāo)毀掉。

  <keep-alive :max="10">
    <component :is="view"></component>
  </keep-alive>

<keep-alive> 不會(huì)在函數(shù)式組件中正常工作,因?yàn)樗鼈儧](méi)有緩存實(shí)例。

5、slot

  • Props:

    name - string,用于具名插槽

  • 用法:

<slot> 元素作為組件模板之中的內(nèi)容分發(fā)插槽。<slot> 元素自身將被替換。

6、teleport

  • Props:

to - string。需要 prop,必須是有效的查詢選擇器或 HTMLElement (如果在瀏覽器環(huán)境中使用)。指定將在其中移動(dòng) <teleport> 內(nèi)容的目標(biāo)元素

  <!-- 正確 -->
  <teleport to="#some-id" />
  <teleport to=".some-class" />
  <teleport to="[data-teleport]" />
  
  <!-- 錯(cuò)誤 -->
  <teleport to="h2" />
  <teleport to="some-string" />

disabled - boolean。此可選屬性可用于禁用 <teleport> 的功能,這意味著其插槽內(nèi)容將不會(huì)移動(dòng)到任何位置,而是在您在周?chē)附M件中指定了 <teleport> 的位置渲染。

  <teleport to="#popup" :disabled="displayVideoInline">
    <video src="./my-movie.mp4">
  </teleport>

請(qǐng)注意,這將移動(dòng)實(shí)際的 DOM 節(jié)點(diǎn),而不是被銷(xiāo)毀和重新創(chuàng)建,并且它還將保持任何組件實(shí)例的活動(dòng)狀態(tài)。所有有狀態(tài)的 HTML 元素 (即播放的視頻) 都將保持其狀態(tài)。

7、Suspense

用于協(xié)調(diào)對(duì)組件樹(shù)中嵌套的異步依賴的處理。

  • Props

interface SuspenseProps {
  timeout?: string | number
}

  • 事件

    @resolve

    @pending

    @fallback

  • 詳細(xì)信息

<Suspense> 接受兩個(gè)插槽:#default 和 #fallback。它將在內(nèi)存中渲染默認(rèn)插槽的同時(shí)展示后備插槽內(nèi)容。

如果在渲染時(shí)遇到異步依賴項(xiàng) (異步組件和具有 async setup() 的組件),它將等到所有異步依賴項(xiàng)解析完成時(shí)再顯示默認(rèn)插槽。

關(guān)于“vue內(nèi)置組件怎么使用”這篇文章的內(nèi)容就介紹到這里,感謝各位的閱讀!相信大家對(duì)“vue內(nèi)置組件怎么使用”知識(shí)都有一定的了解,大家如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

vue
AI