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

vue中組件的3種使用方式詳解

 更新時(shí)間:2019年03月23日 11:45:37   作者:貴飛  
這篇文章主要給大家介紹了關(guān)于vue中組件的3種使用方式,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用vue具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

前言

組件是Vue.js最強(qiáng)大的功能之一。組件可以擴(kuò)展HTML元素,封裝可重用的代碼。

在vue angular react三大前端框架的大前端時(shí)代。許多人選擇了vue,在 github 上的star,vue已經(jīng)超過(guò)react的數(shù)量了。雖然star并不能代表vue更強(qiáng),不過(guò)在發(fā)展速度上看來(lái),vue確實(shí)很快。


在模塊化的前端時(shí)代,萬(wàn)物皆組件,vue學(xué)習(xí)組件是必不可少的。

可是在大多數(shù)人熟悉了純html、jq之后,在初次接觸vue的組件時(shí)候,卻是滿臉蒙蔽。
今天咱們以最簡(jiǎn)單的方式,帶vue小白童鞋們,步入組件的世界~

咱們今天講三種組件使用方式

  • 基本組件
  • 全局組件
  • 構(gòu)造組件

1. 基本組件四步驟

  • 寫(xiě)好組件(廢話~)
  • 在頁(yè)面種引用組件
  • 在components中聲明組件
  • 在頁(yè)面上使用

咱們以一個(gè)button子組件為例

項(xiàng)目src結(jié)構(gòu):

組件一般都放在components文件夾下:

1.寫(xiě)好子組件:

<template>
 <button class="btn" :style="{color:color}">
 <slot/> <!-- 插槽 -->
 </button>
</template>

<script>
export default {
 // 傳入子組件的參數(shù)寫(xiě)到props
 props: {
 color: {
 type: String, // 顏色參數(shù)類型
 default: "#000" // 默認(rèn)黑色
 }
 }
}
</script>

<style scoped>
 .btn {
 width: 110px;
 height: 60px;
 border-radius: 10px;
 border: none;
 font-size: 15px;
 }
</style>

2.3.4.父組件:

<template>
 <div id="app">
 <!-- 4. 在頁(yè)面上使用 -->
 <Button color="red">我是插槽的值</Button>
 </div>
</template>

<script>
// 2. 在頁(yè)面種引用組件
import Button from '@/components/Button.vue'
export default {
 name: "app",
 // 3. 在components中聲明組件
 components: {
 Button
 }
};
</script>

效果:

2. 全局組件五步驟

  • 寫(xiě)好組件(還是廢話~)
  • 子組件添加install方法
  • 在 main.js 中引用
  • 使用 Vue.use 方法
  • 在頁(yè)面上使用

1.子組件還是那樣~~:

2. 子組件添加install方法

Button.js :

import ButtonComponent from './Button.vue'

// 添加install方法 (插件方法)
const Button = {
 install: function (Vue) {
 Vue.component("Button", ButtonComponent);
 }
}

// 導(dǎo)出Button
export default Button

當(dāng)然 你可以處理多個(gè)全局組件:

import ButtonComponent1 from './Button1.vue'
import ButtonComponent2 from './Button2.vue'
import ButtonComponent3 from './Button3.vue'

const buttonList = [
 ButtonComponent1,
 ButtonComponent2,
 ButtonComponent3
];
// 添加install方法 (插件方法)
const Button = {
 install: function (Vue) {
 buttonList.forEach(button=>{
 // 這里 使用每個(gè)組件的 name 屬性作為組件名
 Vue.component(button.name, button);
 })
 }
}

// 導(dǎo)出Button
export default Button

3.4. main.js

import Vue from 'vue'
import App from './App.vue'
// 3
import Button from '@/components/Button.js'
// 4
Vue.use(Button);
new Vue({
 render: h => h(App),
}).$mount('#app')

5. 在頁(yè)面上使用
app.vue:

<template>
 <div id="app">
 <!-- 5. 在頁(yè)面上使用 -->
 <Button color="blue">我是全局按鈕</Button>
 </div>
</template>

效果如下:

2. 構(gòu)造組件四步驟

  • 寫(xiě)好組件(還**是廢話~)
  • vue.extend構(gòu)建組件
  • 掛載 Vue.prototype
  • 在js中使用

1.寫(xiě)好子組件:

<template>
 <p class="Message">{{value}}</p>
</template>

<script>
export default {
 data() {
 return {
  value: "我是一個(gè)彈框"
 };
 }
};
</script>

<style>
.Message {
 position: fixed;
 bottom: 30px;
 width: 200px;
 background-color: rgba(0, 0, 0, 0.5);
 color: #fff;
 border-radius: 10px;
 left: 50%;
 transform: translateX(-50%);
 line-height: 30px;
 text-align: center;
 font-size: 15px;
 animation: messageFade 3s 1;
}
/* 加個(gè)簡(jiǎn)單動(dòng)畫(huà) */
@keyframes messageFade {
 0% {
 opacity: 0;
 -webkit-transform: translate3d(-50%, 80%, 0);
 transform: translate3d(-50%, 80%, 0);
 }
 16% {
 opacity: 1;
 -webkit-transform: translate3d(-50%, 0, 0);
 transform: translate3d(-50%, 0, 0);
 }
 84% {
 opacity: 1;
 -webkit-transform: translate3d(-50%, 0, 0);
 transform: translate3d(-50%, 0, 0);
 }
 100% {
 opacity: 0;
 -webkit-transform: translate3d(-50%, 80%, 0);
 transform: translate3d(-50%, 80%, 0);
 }
}
</style>

2. vue.extend構(gòu)建組件

Message.js :

import Vue from 'vue';
import Message from './Message.vue';
// 構(gòu)造組件
const MessageConstructor = Vue.extend(Message);
// 設(shè)置刪除組件
const removeDom = (target) => {
 target.parentNode.removeChild(target);
};
// 構(gòu)造組件添加關(guān)閉方法
MessageConstructor.prototype.close = function() {
 this.visible = false;
 removeDom(this.$el);
};

const MessageDiv = (options) => {
 // 實(shí)例化組件
 const instance = new MessageConstructor({
  el: document.createElement('div'),
  // 組件參數(shù),運(yùn)用到組件內(nèi)的data
  data: options,
 });
 // 在body添加組件
 document.body.appendChild(instance.$el);
 Vue.nextTick(() => {
  instance.timer = setTimeout(() => {
   // 定時(shí)關(guān)閉組件
   instance.close();
  }, 3000);
 });
 return instance;
};

export default MessageDiv;

3. 掛載 Vue.prototype

main.js :

import Message from '@/components/Message.js'
Vue.prototype.$message = Message;

4. 使用:

<template>
 <div id="app">
 <Button color="blue" @click.native="msg">我是全局按鈕</Button>
 </div>
</template>

<script>
import Button from "@/components/Button.vue";
export default {
 name: "app",
 components: {
 Button
 },
 methods: {
 msg() {
  // 4. 使用構(gòu)造組件
  this.$message({value:'我是構(gòu)造組件'});
 }
 }
};
</script>

效果:

以上就是三種組件的基本使用啦~~

總結(jié)

以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)腳本之家的支持。

相關(guān)文章

  • 一步步從Vue3.x源碼上理解ref和reactive的區(qū)別

    一步步從Vue3.x源碼上理解ref和reactive的區(qū)別

    vue3的數(shù)據(jù)雙向綁定,大家都明白是proxy數(shù)據(jù)代理,但是在定義響應(yīng)式數(shù)據(jù)的時(shí)候,有ref和reactive兩種方式,如果判斷該使用什么方式,是大家一直不很清楚地問(wèn)題,下面這篇文章主要給大家介紹了關(guān)于從Vue3.x源碼上理解ref和reactive的區(qū)別的相關(guān)資料,需要的朋友可以參考下
    2023-02-02
  • vue router總結(jié) $router和$route及router與 router與route區(qū)別

    vue router總結(jié) $router和$route及router與 router與route區(qū)別

    這篇文章主要介紹了vue router總結(jié) $router和$route及router與 router與route區(qū)別,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值 ,需要的朋友可以參考下
    2019-07-07
  • Vue組件與Vue cli腳手架安裝方法超詳細(xì)講解

    Vue組件與Vue cli腳手架安裝方法超詳細(xì)講解

    CLI是一個(gè)全局安裝的npm包,提供了終端里的vue命令。它可以通過(guò)vue create快速搭建一個(gè)新項(xiàng)目,或者直接通過(guò)vue serve構(gòu)建新想法的原型。你也可以通過(guò)vue ui通過(guò)一套圖形化界面管理你的所有項(xiàng)目
    2022-11-11
  • element table跨分頁(yè)多選及回顯的實(shí)現(xiàn)示例

    element table跨分頁(yè)多選及回顯的實(shí)現(xiàn)示例

    本文主要介紹了element table跨分頁(yè)多選及回顯的實(shí)現(xiàn)示例,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-02-02
  • vue $set 實(shí)現(xiàn)給數(shù)組集合對(duì)象賦值

    vue $set 實(shí)現(xiàn)給數(shù)組集合對(duì)象賦值

    這篇文章主要介紹了vue $set 實(shí)現(xiàn)給數(shù)組集合對(duì)象賦值方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-07-07
  • Vue實(shí)現(xiàn)固定定位圖標(biāo)滑動(dòng)隱藏效果

    Vue實(shí)現(xiàn)固定定位圖標(biāo)滑動(dòng)隱藏效果

    移動(dòng)端頁(yè)面,有時(shí)候會(huì)出現(xiàn)一些固定定位在底部圖標(biāo),比如購(gòu)物車(chē)等。這篇文章主要介紹了Vue制作固定定位圖標(biāo)滑動(dòng)隱藏效果,需要的朋友可以參考下
    2019-05-05
  • vue?props?type設(shè)置多個(gè)類型

    vue?props?type設(shè)置多個(gè)類型

    這篇文章主要介紹了vue?props?type設(shè)置多個(gè)類型,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-04-04
  • 淺談vue獲得后臺(tái)數(shù)據(jù)無(wú)法顯示到table上面的坑

    淺談vue獲得后臺(tái)數(shù)據(jù)無(wú)法顯示到table上面的坑

    這篇文章主要介紹了淺談vue獲得后臺(tái)數(shù)據(jù)無(wú)法顯示到table上面的坑,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-08-08
  • Vue3中Vuex的詳細(xì)使用方法

    Vue3中Vuex的詳細(xì)使用方法

    在vue3.x中vuex調(diào)取值在html代碼里可以直接使用vue2.x的方法,但是在js里與vue2.x就有了那么一丟丟的不同,下面這篇文章主要給大家介紹了關(guān)于Vue3中Vuex詳細(xì)使用的相關(guān)資料,需要的朋友可以參考下
    2022-07-07
  • 關(guān)于vue3?解決getCurrentInstance?打包后線上環(huán)境報(bào)錯(cuò)問(wèn)題

    關(guān)于vue3?解決getCurrentInstance?打包后線上環(huán)境報(bào)錯(cuò)問(wèn)題

    這篇文章主要介紹了vue3?解決getCurrentInstance?打包后線上環(huán)境報(bào)錯(cuò)問(wèn)題,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-05-05

最新評(píng)論

胶南市| 策勒县| 嘉兴市| 上饶市| 丹东市| 若羌县| 资源县| 财经| 台州市| 中西区| 阳泉市| 山东省| 利辛县| 板桥市| 新乐市| 肥东县| 南乐县| 安达市| 寿阳县| 枣庄市| 化隆| 广汉市| 博爱县| 光山县| 杨浦区| 宕昌县| 丰都县| 祁阳县| 潍坊市| 遂溪县| 万宁市| 黄平县| 罗源县| 垦利县| 固始县| 遂平县| 牡丹江市| 定日县| 吴旗县| 五华县| 乐都县|