【Vue】組件

Vue的兩大核心

  1. 數(shù)據(jù)驅(qū)動 - 數(shù)據(jù)驅(qū)動界面顯示
  2. 模塊化 - 復(fù)用公共模塊,組件實現(xiàn)模塊化提供基礎(chǔ)

組件基礎(chǔ)


組件渲染過程

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

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

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

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

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

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

runtime-compiler

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

啟用runtime-compiler兩種方式

  1. main.js啟用
Vue.config.runtimeCompiler = true
  1. vue.config.js(若沒有手動創(chuàng)建一個)
module.exports = {
    runtimeCompiler: true      //默認(rèn)false
}

組件定義


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

例子

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

這種形式需要啟用運(yùn)行時編譯(runtime-compiler)

2. 單文件組件(推薦)

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

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

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

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

組件注冊


  1. 全局注冊
    全局注冊是通過Vue.component()注冊
import CustomButton from './components/ComponentDemo.vue'
Vue.component('CustomButton', CustomButton)

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

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

缺點(diǎn)

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

適合

  • 基礎(chǔ)組件全局注冊
  1. 局部注冊
    在需要的地方導(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>

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

組件傳參
  1. 通過props屬性
<template>
  <button> {{buttonText}} </button>
</template>
<script>
export default {
  props: {
    buttonText: String
  }
};
</script>
<customButton buttonText="Button 1"></customButton>
<customButton buttonText="Button 2"></customButton>
<customButton buttonText="Button 3"></customButton>
運(yùn)行效果
  1. 通過插槽<slot></slot>
<template>
  <button style="margin:10px"><slot>Defalt Button</slot></button>
</template>
<script>
export default {
  props: {
    buttonText: String
  }
};
</script>
<customButton></customButton>
<customButton><span style="color:blue">Button 2</span></customButton>
<customButton>Button 3</customButton>
運(yù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>
  1. 調(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>
運(yùn)行效果
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 此文基于官方文檔,里面部分例子有改動,加上了一些自己的理解 什么是組件? 組件(Component)是 Vue.j...
    陸志均閱讀 3,949評論 5 14
  • 三、組件 組件 (Component) 是 Vue.js 最強(qiáng)大的功能之一。組件可以擴(kuò)展 HTML元素,封裝可重用...
    小山居閱讀 713評論 0 1
  • 從接觸angularjs1.x開始,使用并開發(fā)過很多組件和指令,它能極大的擴(kuò)展web的業(yè)務(wù)處理能力,而且代碼很簡潔...
    老鼠AI大米_Java全棧閱讀 3,557評論 0 4
  • PS:轉(zhuǎn)載請注明出處作者: TigerChain地址: http://www.itdecent.cn/p/8de...
    TigerChain閱讀 4,782評論 1 53
  • Vue組件 vue組件:封裝前端vue特效代碼,便于引用 全局組件全局組件通過Vue.component在scri...
    3e0693dcfb2f閱讀 398評論 0 0

友情鏈接更多精彩內(nèi)容