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

詳解vue 組件

 更新時(shí)間:2020年06月11日 11:50:01   作者:WilsonPan  
這篇文章主要介紹了詳解vue 組件的相關(guān)知識(shí),文中講解非常細(xì)致,代碼供大家參考學(xué)習(xí),感興趣的朋友可以了解下

Vue的兩大核心

1. 數(shù)據(jù)驅(qū)動(dòng) - 數(shù)據(jù)驅(qū)動(dòng)界面顯示

2. 模塊化 - 復(fù)用公共模塊,組件實(shí)現(xiàn)模塊化提供基礎(chǔ)

組件基礎(chǔ)

組件渲染過(guò)程

template ---> ast(抽象語(yǔ)法樹) ---> render ---> VDom(虛擬DOM) ---> 真實(shí)的Dom ---> 頁(yè)面

Vue組件需要編譯,編譯過(guò)程可能發(fā)生在

  • 打包過(guò)程 (使用vue文件編寫)
  • 運(yùn)行時(shí)(將字符串賦值template字段,掛載到一個(gè)元素上并以其 DOM 內(nèi)部的 HTML 作為模板)

對(duì)應(yīng)的兩種方式 runtime-only vs runtime-compiler

runtime-only(默認(rèn))

  • 打包時(shí)只包含運(yùn)行時(shí),因此體積更少
  • 將template在打包的時(shí)候,就已經(jīng)編譯為render函數(shù),因此性能更好

runtime-compiler

  • 打包時(shí)需要包含(運(yùn)行時(shí) + 編譯器),因此體積更大,大概多10Kb
  • 在運(yùn)行的時(shí)候才把template編譯為render函數(shù),因此性能更差

啟用runtime-compiler

vue.config.js(若沒(méi)有手動(dòng)創(chuàng)建一個(gè))

module.exports = {
 runtimeCompiler: true //默認(rèn)false
}

組件定義

1. 字符串形式定義(不推薦)

例子

const CustomButton = {
 template: "<button>自定義按鈕</button>"
};

這種形式在運(yùn)行時(shí)才把template編譯成render函數(shù),因此需要啟用運(yùn)行時(shí)編譯(runtime-compiler)

2. 單文件組件(推薦)

創(chuàng)建.vue后綴的文件,定義如下

<template>
 <div>
 <button>自定義按鈕</button>
 </div>
</template>

<template> 里只能有一個(gè)根節(jié)點(diǎn),即第一層只能有一個(gè)節(jié)點(diǎn),不能多個(gè)節(jié)點(diǎn)平級(jí)

這種形式在打包的時(shí)就編譯成render函數(shù),因此跟推薦這種方式定義組件

組件注冊(cè)

1. 全局注冊(cè)

全局注冊(cè)是通過(guò)Vue.component()注冊(cè)

import CustomButton from './components/ComponentDemo.vue'
Vue.component('CustomButton', CustomButton)

優(yōu)點(diǎn)

  • 其他地方可以直接使用
  • 不再需要components指定組件

缺點(diǎn)

  • 全局注冊(cè)的組件會(huì)全部一起打包,增加app.js體積

適合

  • 基礎(chǔ)組件全局注冊(cè)

2. 局部注冊(cè)

在需要的地方導(dǎo)入

<template>
 <div id="app">
 <customButton></customButton>
 </div>
</template>
<script>
import CustomButton from "./components/ComponentDemo.vue";

export default {
 name: "App",
 components: { CustomButton }
};
</script>

優(yōu)點(diǎn)

  • 按需加載

缺點(diǎn)

  • 每次使用必須導(dǎo)入,然后components指定

適合

  • 非基礎(chǔ)組件

組件使用

組件復(fù)用

<template>
 <div id="app">
 <img alt="Vue logo" src="./assets/logo.png" />
 <customButton></customButton>
 <customButton></customButton>
 <customButton></customButton>
 </div>
</template>

customButton 組件

<template>
 <div id="app">
 <button @click="increment">click me {{times}} times</button>
 </div>
</template>
<script>
export default {
 data() {
 return { times: 0 };
 },
 methods: {
 increment() {
 return this.times++;
 }
 }
};
</script>

每個(gè)組件都會(huì)創(chuàng)建一個(gè)新實(shí)例,組件的data必須是function,因?yàn)槊總€(gè)實(shí)例維護(hù)自己的data數(shù)據(jù)

組件傳參

1. 通過(guò)props屬性

定義一個(gè)button,按鈕文本通過(guò)props傳入

<template>
 <button> {{buttonText}} </button>
</template>
<script>
export default {
 props: {
 buttonText: String
 }
};
</script>

調(diào)用者通過(guò)attribute傳入

<customButton buttonText="Button 1"></customButton>
<customButton buttonText="Button 2"></customButton>
<customButton buttonText="Button 3"></customButton>

運(yùn)行效果

2. 通過(guò)插槽<slot></slot>

組件在需要替換的地方放入插槽<slot></slot>

<template>
 <button style="margin:10px"><slot>Defalt Button</slot></button>
</template>
<script>
export default {
 props: {
 buttonText: String
 }
};
</script>

調(diào)用者的innerHtml會(huì)替換插槽的值,若為空,使用默認(rèn)的

運(yùn)行效果

注意:看到是用自定義組件的innerHtml替換插槽,若插槽只有一個(gè),可以不寫name attribute,若多個(gè)插槽需指定插槽name attribute

自定義事件

1. 在組件內(nèi)部調(diào)用this.$emit觸發(fā)自定義事件

<template>
 <div style="margin:10px">
 <button @click="increment">
 <slot>Defalt Button</slot>
 </button>
 <span>Click me {{times}} times</span>
 </div>
</template>
<script>
export default {
 props: {
 buttonText: String
 },
 data() {
 return { times: 0 };
 },
 methods: {
 increment() {
 this.times++;
 ("increment");
 }
 }
};
</script>

2. 調(diào)用者監(jiān)聽(tīng)自定義事件

<template>
 <div id="app">
 <customButton @increment="handleIncrement"></customButton>
 <customButton @increment="handleIncrement">
 <span style="color:blue">Button 2</span>
 </customButton>
 <customButton @increment="handleIncrement">Button 3</customButton>
 <p>Total click {{totalClicks}} times</p>
 </div>
</template>
<script>
import CustomButton from "./components/ComponentDemo.vue";

export default {
 name: "App",
 components: { CustomButton },
 data() {
 return { totalClicks: 0 };
 },
 methods: {
 handleIncrement() {
 this.totalClicks++;
 }
 }
};
</script>

3. 運(yùn)行效果

以上就是詳解vue 組件的詳細(xì)內(nèi)容,更多關(guān)于vue組件的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • vue spa應(yīng)用中的路由緩存問(wèn)題與解決方案

    vue spa應(yīng)用中的路由緩存問(wèn)題與解決方案

    這篇文章主要介紹了vue spa應(yīng)用中的路由緩存問(wèn)題與解決方案,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-05-05
  • Vuex中mutations與actions的區(qū)別詳解

    Vuex中mutations與actions的區(qū)別詳解

    下面小編就為大家分享一篇Vuex中mutations與actions的區(qū)別詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-03-03
  • Vue3屬性值傳遞defineProps詳解

    Vue3屬性值傳遞defineProps詳解

    在Vue3中,defineProps()函數(shù)是定義和接收組件屬性的主要方式,通過(guò)簡(jiǎn)單定義或?qū)ο蠖x,開(kāi)發(fā)者可以靈活地接收并處理組件上的屬性值,簡(jiǎn)單定義方式通過(guò)數(shù)組傳遞屬性名,而對(duì)象定義則可以約束屬性的數(shù)據(jù)類型、默認(rèn)值及是否必須傳遞等
    2024-09-09
  • vue3.2?reactive函數(shù)問(wèn)題小結(jié)

    vue3.2?reactive函數(shù)問(wèn)題小結(jié)

    reactive用來(lái)包裝一個(gè)對(duì)象,使其每個(gè)對(duì)象屬性都具有響應(yīng)性(也就是深層次響應(yīng)式),這篇文章主要介紹了vue3.2?reactive函數(shù)注意點(diǎn)及問(wèn)題小結(jié),需要的朋友可以參考下
    2022-12-12
  • VuePress 中如何增加用戶登錄功能

    VuePress 中如何增加用戶登錄功能

    VuePress 由兩部分組成:一個(gè)以 Vue 驅(qū)動(dòng)的主題系統(tǒng)的簡(jiǎn)約靜態(tài)網(wǎng)站生成工具,和一個(gè)為編寫技術(shù)文檔而優(yōu)化的默認(rèn)主題。它是為了支持 Vue 子項(xiàng)目的文檔需求而創(chuàng)建的
    2019-11-11
  • vue引入axios同源跨域問(wèn)題

    vue引入axios同源跨域問(wèn)題

    這篇文章主要介紹了vue引入axios同源跨域問(wèn)題,文章給大家提供了解決方案,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2018-09-09
  • ElementPlus 自定義封裝 el-date-picker 的快捷功能示例詳解

    ElementPlus 自定義封裝 el-date-picker 的快捷功能示例詳解

    文章討論了用戶對(duì)官網(wǎng)提供的案例不滿足快捷功能需求的情況,建議在外部自定義組件中導(dǎo)入并使用快捷內(nèi)容,以滿足用戶需求,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),感興趣的朋友一起看看吧
    2024-12-12
  • Vue 列表頁(yè)帶參數(shù)進(jìn)詳情頁(yè)的操作(router-link)

    Vue 列表頁(yè)帶參數(shù)進(jìn)詳情頁(yè)的操作(router-link)

    這篇文章主要介紹了Vue 列表頁(yè)帶參數(shù)進(jìn)詳情頁(yè)的操作(router-link),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-11-11
  • 在vue中實(shí)現(xiàn)iframe嵌套Html頁(yè)面及注意事項(xiàng)說(shuō)明

    在vue中實(shí)現(xiàn)iframe嵌套Html頁(yè)面及注意事項(xiàng)說(shuō)明

    這篇文章主要介紹了在vue中實(shí)現(xiàn)iframe嵌套Html頁(yè)面及注意事項(xiàng)說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-10-10
  • Vue入門之?dāng)?shù)據(jù)綁定(小結(jié))

    Vue入門之?dāng)?shù)據(jù)綁定(小結(jié))

    本篇文章主要介紹了探索Vue高階組件的使用,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-01-01

最新評(píng)論

安平县| 潜江市| 岳池县| 吉木乃县| 武定县| 沙洋县| 香港| 虹口区| 屏南县| 临泽县| 宜黄县| 商都县| 大理市| 武川县| 舞钢市| 阿拉善左旗| 三河市| 得荣县| 新营市| 辉县市| 津南区| 伊川县| 盈江县| 苏尼特右旗| 长宁县| 疏附县| 白山市| 湖南省| 陇西县| 新沂市| 富裕县| 孟州市| 河源市| 彝良县| 唐海县| 桃园市| 略阳县| 沽源县| 伊金霍洛旗| 张家口市| 山阴县|