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

詳解vue 組件

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

Vue的兩大核心

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

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

組件基礎(chǔ)

組件渲染過程

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

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

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

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

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

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

runtime-compiler

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

啟用runtime-compiler

vue.config.js(若沒有手動創(chuàng)建一個)

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

組件定義

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

例子

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

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

2. 單文件組件(推薦)

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

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

<template> 里只能有一個根節(jié)點,即第一層只能有一個節(jié)點,不能多個節(jié)點平級

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

組件注冊

1. 全局注冊

全局注冊是通過Vue.component()注冊

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

優(yōu)點

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

缺點

  • 全局注冊的組件會全部一起打包,增加app.js體積

適合

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

2. 局部注冊

在需要的地方導(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)點

  • 按需加載

缺點

  • 每次使用必須導(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>

每個組件都會創(chuàng)建一個新實例,組件的data必須是function,因為每個實例維護(hù)自己的data數(shù)據(jù)

組件傳參

1. 通過props屬性

定義一個button,按鈕文本通過props傳入

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

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

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

運行效果

2. 通過插槽<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會替換插槽的值,若為空,使用默認(rèn)的

運行效果

注意:看到是用自定義組件的innerHtml替換插槽,若插槽只有一個,可以不寫name attribute,若多個插槽需指定插槽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)聽自定義事件

<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. 運行效果

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

相關(guān)文章

  • Vue生命周期詳解

    Vue生命周期詳解

    這篇文章詳細(xì)介紹了Vue的生命周期,文中通過代碼示例介紹的非常詳細(xì)。對大家的學(xué)習(xí)有一定的參考借鑒價值,需要的朋友可以參考下
    2023-04-04
  • 使用Vue快速實現(xiàn)一個無縫輪播效果

    使用Vue快速實現(xiàn)一個無縫輪播效果

    這篇文章主要為大家詳細(xì)介紹了如何使用Vue快速實現(xiàn)一個無縫輪播效果,文中的示例代碼講解詳細(xì),具有一定的借鑒價值,有需要的小伙伴可以參考下
    2024-04-04
  • element table多層嵌套顯示的實踐

    element table多層嵌套顯示的實踐

    本文主要介紹了element table多層嵌套顯示的實踐,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-09-09
  • Vue中 v-if 和v-else-if頁面加載出現(xiàn)閃現(xiàn)的問題及解決方法

    Vue中 v-if 和v-else-if頁面加載出現(xiàn)閃現(xiàn)的問題及解決方法

    vue中v-if 和v-else-if在頁面加載的時候,不滿足條件的標(biāo)簽會加載然后再消失掉,如果要解決這個問題,下面小編給大家?guī)砹藢嵗a,需要的朋友參考下吧
    2018-10-10
  • vue項目優(yōu)化之通過keep-alive數(shù)據(jù)緩存的方法

    vue項目優(yōu)化之通過keep-alive數(shù)據(jù)緩存的方法

    本篇文章主要介紹了vue項目優(yōu)化之通過keep-alive數(shù)據(jù)緩存的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-12-12
  • Element-ui Drawer抽屜按需引入基礎(chǔ)使用

    Element-ui Drawer抽屜按需引入基礎(chǔ)使用

    這篇文章主要為大家介紹了Element-ui Drawer抽屜按需引入基礎(chǔ)使用,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-07-07
  • Vue中的組件注冊方法及注意事項

    Vue中的組件注冊方法及注意事項

    在Vue中,組件是構(gòu)建頁面的基本單位。通過組件化開發(fā),可以提高代碼的復(fù)用性和可維護(hù)性。組件的注冊方法包括全局注冊和局部注冊兩種方式。同時,需要注意組件名的命名規(guī)范、組件選項的定義方式、組件之間的通信等問題,以實現(xiàn)更好的組件復(fù)用和開發(fā)效率
    2023-05-05
  • Vue?中使用?WebWorker的示例代碼

    Vue?中使用?WebWorker的示例代碼

    這篇文章主要介紹了Vue中使用WebWorker的示例代碼,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-08-08
  • vue阻止重復(fù)請求實現(xiàn)示例詳解

    vue阻止重復(fù)請求實現(xiàn)示例詳解

    這篇文章主要為大家介紹了vue阻止重復(fù)請求實現(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-01-01
  • vue 使用monaco實現(xiàn)代碼高亮

    vue 使用monaco實現(xiàn)代碼高亮

    這篇文章主要介紹了vue 使用monaco實現(xiàn)代碼高亮的方法,幫助大家更好的理解和學(xué)習(xí)使用vue框架,感興趣的朋友可以了解下
    2021-03-03

最新評論