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

Vue extend學(xué)習(xí)示例講解

 更新時(shí)間:2022年09月20日 14:27:38   作者:正在學(xué)習(xí)前端的渣渣-小方同學(xué)  
這篇文章主要介紹了Vue.extend使用示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

vue中通過extend動(dòng)態(tài)創(chuàng)建全局組件;

1、什么是動(dòng)態(tài)創(chuàng)建組件

只有在觸發(fā)事件的時(shí)候,才產(chǎn)生某組件,平時(shí)它并不存在;

2、Vue.extend()

使用基礎(chǔ) Vue 構(gòu)造器,創(chuàng)建一個(gè)“子類”。參數(shù)是一個(gè)包含組件選項(xiàng)的對(duì)象;其實(shí)就是一個(gè)子類構(gòu)造器是Vue組件的核心api,實(shí)現(xiàn)思路就是使用原型繼承的方法返回了Vue的子類,并且利用mergeOptions把傳入組件的options和父類的options進(jìn)行了合并。

extend創(chuàng)建的是一個(gè)組件構(gòu)造器,而不是一個(gè)具體的實(shí)例;

接收一個(gè)對(duì)象(包含組件選項(xiàng)的對(duì)象)作為參數(shù),需要使用new來創(chuàng)建實(shí)例,并且需要$mount手動(dòng)掛載到一個(gè)元素上,才可以獲取到這個(gè)元素的相應(yīng)的信息。

  • 脫離填鴨式的寫法;代碼自由
  • 代碼復(fù)用,解耦
  • 原生JS語法結(jié)合vue(jsx)
  • 通過傳入?yún)?shù),可以顯示不同狀態(tài)的模板

基礎(chǔ)用法:

<div id="mount-point"></div>
// 創(chuàng)建構(gòu)造器
/* Vue.extend( options )
  參數(shù):{Object} options
  用法:使用基礎(chǔ) Vue 構(gòu)造器,創(chuàng)建一個(gè)“子類”。參數(shù)是一個(gè)包含組件選項(xiàng)的對(duì)象;
       data 選項(xiàng)是特例,需要注意: 在 Vue.extend() 中它必須是函數(shù);*/
var Profile = Vue.extend({
  template: '<p>{{firstName}} {{lastName}} aka {{alias}}</p>',
  data: function () {
    return {
      firstName: 'Walter',
      lastName: 'White',
      alias: 'Heisenberg'
    }
  }
})
// 創(chuàng)建 Profile 實(shí)例,并掛載到一個(gè)元素上。
new Profile().$mount('#mount-point')
// 結(jié)果如下:
<p>Walter White aka Heisenberg</p>
/*
可以看到,extend 創(chuàng)建的是 Vue 構(gòu)造器,而不是我們平時(shí)常寫的組件實(shí)例,所以不可以通過 new Vue({ components: testExtend }) 來直接使用,需要通過 new Profile().$mount('#mount-point') 來掛載到指定的元素上。
*/

3、通過extend實(shí)現(xiàn)彈窗的動(dòng)態(tài)創(chuàng)建

3.1、創(chuàng)建動(dòng)態(tài)組件

<!--動(dòng)態(tài)組件的模板-->
<template>
  <!--  可以用MessageBox做蒙塵-->
  <div :class="['MessageBox',type]">
    <div class="inner">
      <header class="header">
        <h1 class="title">{{ title }}</h1>
        <span @click="$messageBox.hide()">x</span>
      </header>
      <div class="content">{{ content }}</div>
    </div>
  </div>
</template>
<script>
export default {
  name: "MessageBox",
  props: {
    title: {
      type: String,
      default: "this is title",
    },
    content: {
      type: String,
      default: "this is content",
    },
    type: {
      type: String,
      default: "primary",
      //檢測(cè)傳進(jìn)來的類型是否是這四種,通過ES6提供的includes方法模糊查詢
      validator(value) {
        return [
          "primary",
          "success",
          "warn",
          "danger"
        ].includes(value);
      }
    }
  }
}
</script>
<style scoped lang="less">
.MessageBox {
  position: fixed;
  left: 50%;
  top: 0;
  //蒙塵的大小設(shè)置
  width: 50%;
  height: 400px;
  background-color: rgba(0, 0, 0, .5);
  //不同彈窗的樣式
  &.primary {
    .header {
      background-color: blue;
      color: #fff;
    }
  }
  &.success {
    .header {
      background-color: green;
      color: #fff;
    }
  }
  &.warn {
    .header {
      background-color: rgba(255, 138, 71, 0.96);
      color: #fff;
    }
  }
  &.danger {
    .header {
      background-color: red;
      color: #fff;
    }
  }
  .inner {
    position: absolute;
    top: 100px;
    left: 50%;
    width: 500px;
    margin-left: -250px;
    background-color: #fff;
    box-shadow: 1px 3px 5px #ddd;
    border-radius: 5px;
    overflow: hidden;
    .header {
      height: 44px;
      padding: 0 10px;
      line-height: 44px;
      box-sizing: border-box;
      h1 {
        margin: 0;
        font-weight: normal;
      }
      .title {
        font-size: 16px;
        float: left;
      }
      span {
        //將鼠標(biāo)改為小手樣式
        cursor: pointer;
        float: right;
      }
    }
    .content {
      padding: 20px;
      box-sizing: border-box;
    }
  }
}
</style>

3.2、編輯動(dòng)態(tài)組件的邏輯

//引入需要?jiǎng)討B(tài)創(chuàng)建的模板
import _MessageBox from "@/components/messageBox/MessageBox";
export default {
    //install開發(fā)插件的方法,install帶有Vue的構(gòu)造器,可以使用Vue.extend,和Vue.component(注冊(cè)組件)
    //在Vue.use的時(shí)候就會(huì)調(diào)用這個(gè)install
    install(Vue) {
        let messageBox = null;
        //使用Vue.component全局注冊(cè)組件
        Vue.component(_MessageBox.name, _MessageBox);
        //將方法添加到Vue的prototype屬性中,這樣實(shí)例就可以繼承里面的方法
        Vue.prototype.$messageBox = {
            show, hide,
            info({title, content, type}, callback) {
                this.show({title, content, type: "primary"}, callback)
            },
            success({title, content, type}, callback) {
                this.show({title, content, type: "success"}, callback)
            },
            warn({title, content, type}, callback) {
                this.show({title, content, type: "warn"}, callback)
            },
            danger({title, content, type}, callback) {
                this.show({title, content, type: "danger"}, callback)
            }
        }
        //顯示彈窗
        function show(props, callback) {
            //判斷這個(gè)組件是否存在,如果不存在
            if (!messageBox) {
                //生成構(gòu)造函數(shù)、構(gòu)造器
                const MessageBox = Vue.extend({
                    /*
                    render該渲染函數(shù)接收一個(gè) createElement 方法作為第一個(gè)參數(shù)用來創(chuàng)建 VNode(節(jié)點(diǎn))。
                   如果組件是一個(gè)函數(shù)組件,渲染函數(shù)還會(huì)接收一個(gè)額外的 context 參數(shù),為沒有實(shí)例的函數(shù)組件提供上下文信息。
                  */
                    //此處傳入的是一個(gè)函數(shù)組件,所以渲染的函數(shù)還可以額外接收一個(gè)參數(shù)
                    render(h) {
                        //h函數(shù)就是vue中的createElement函數(shù),這個(gè)函數(shù)作用就是創(chuàng)建虛擬dom,追蹤dom變化的
                        return h("MessageBox", {
                            //用于接收傳遞的參數(shù)
                            props: {...props}
                        })
                    }
                });
                //將動(dòng)態(tài)模板組件實(shí)例化
                messageBox = new MessageBox();
                //將這個(gè)實(shí)例手動(dòng)掛載,掛載后可以通過$el獲取這個(gè)元素
                this.vm = messageBox.$mount();
                //將組件添加到body上,脫離了根節(jié)點(diǎn),不在"id=app中"
                document.body.appendChild(this.vm.$el)
                callback && callback();
            }
        }
        //關(guān)閉彈窗
        function hide(callback) {
            //移出這個(gè)組件
            document.body.removeChild(this.vm.$el);
            //將這個(gè)實(shí)例銷毀
            messageBox.$destroy();
            messageBox = null;
            this.vm = null;
            //如果存在才會(huì)執(zhí)行
            callback && callback();
        }
    }
}

3.3、在main.js中引入使用

import Vue from 'vue'
import App from './App.vue'
//1、引入
import MessageBox from "@/components/messageBox";
//2、全局注冊(cè)使用
Vue.use(MessageBox);
new Vue({
    render: h => h(App)
}).$mount('#app')

3.4、在需要的地方通過觸發(fā)事件顯示彈窗

<template>
  <div>
    <button @click="showMessageBox">show</button>
    <button @click="showInfoMessageBox">info</button>
    <button @click="showSuccessMessageBox">success</button>
    <button @click="showWarnMessageBox">warn</button>
    <button @click="showDangerMessageBox">danger</button>
  </div>
</template>
<script>
export default {
  name: "Extend",
  methods: {
    //通過this.$messageBox可以訪問到Vue實(shí)例的屬性和方法
    showMessageBox() {
      this.$messageBox.success({
        title: 'App',
        content: 'this is content of extend study',
        type: 'success'
      }, () => {
        console.log('show over')
      })
    },
    showInfoMessageBox() {
      this.$messageBox.info({
        title: 'App',
        content: 'this is content of extend study',
      }, () => {
        console.log('info over')
      })
    },
    showSuccessMessageBox() {
      this.$messageBox.success({
        title: 'App',
        content: 'this is content of extend study',
        type: 'success'
      }, () => {
        console.log('success over')
      })
    },
    showWarnMessageBox() {
      this.$messageBox.warn({
        title: 'App',
        content: 'this is content of extend study',
        type: 'warn'
      }, () => {
        console.log('warn over')
      })
    },
    showDangerMessageBox() {
      this.$messageBox.danger({
        title: 'App',
        content: 'this is content of extend study',
        type: 'danger'
      })
    }
  }
}
</script>

3.5、效果圖

到此這篇關(guān)于Vue extend學(xué)習(xí)示例講解的文章就介紹到這了,更多相關(guān)Vue extend內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 解析vue.js中常用v-指令

    解析vue.js中常用v-指令

    本文以click為例給大家介紹vue.js中常用v-指令,可以用 v-on 指令監(jiān)聽 DOM 事件,并在觸發(fā)時(shí)運(yùn)行一些 JavaScript代碼,本文通過示例代碼給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧
    2021-10-10
  • 如何解決d3.event在v7版本無效影響zoom拖拽縮放問題

    如何解決d3.event在v7版本無效影響zoom拖拽縮放問題

    這篇文章主要介紹了如何解決d3.event在v7版本無效影響zoom拖拽縮放問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-03-03
  • 淺談vue中$bus的使用和涉及到的問題

    淺談vue中$bus的使用和涉及到的問題

    這篇文章主要介紹了淺談vue中$bus的使用和涉及到的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2020-07-07
  • vue實(shí)現(xiàn)禁止瀏覽器記住密碼功能的示例代碼

    vue實(shí)現(xiàn)禁止瀏覽器記住密碼功能的示例代碼

    這篇文章主要介紹了vue實(shí)現(xiàn)禁止瀏覽器記住密碼功能的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-02-02
  • vue組件中的數(shù)據(jù)傳遞方法

    vue組件中的數(shù)據(jù)傳遞方法

    這篇文章主要介紹了vue組件中的數(shù)據(jù)傳遞方法,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友參考下吧
    2018-05-05
  • vue中this.$parent的使用方式

    vue中this.$parent的使用方式

    這篇文章主要介紹了vue中this.$parent的使用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-03-03
  • vite中的glob-import批量導(dǎo)入的實(shí)現(xiàn)

    vite中的glob-import批量導(dǎo)入的實(shí)現(xiàn)

    本文主要介紹了vite中的glob-import批量導(dǎo)入的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-07-07
  • 一文詳解Vue選項(xiàng)式?API?的生命周期選項(xiàng)和組合式API

    一文詳解Vue選項(xiàng)式?API?的生命周期選項(xiàng)和組合式API

    這篇文章主要為大家介紹了Vue選項(xiàng)式?API?的生命周期選項(xiàng)和組合式API變化詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-03-03
  • Vue3+Vant實(shí)現(xiàn)簡(jiǎn)單的登錄功能

    Vue3+Vant實(shí)現(xiàn)簡(jiǎn)單的登錄功能

    這篇文章主要為大家詳細(xì)介紹了Vue3如何結(jié)合Vant實(shí)現(xiàn)簡(jiǎn)單的登錄功能,文中的示例代碼講解詳細(xì),有需要的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2024-04-04
  • element-plus?el-form表單驗(yàn)證使用方法以及注意事項(xiàng)

    element-plus?el-form表單驗(yàn)證使用方法以及注意事項(xiàng)

    這篇文章主要給大家介紹了關(guān)于element-plus?el-form表單驗(yàn)證使用方法以及注意事項(xiàng)的相關(guān)資料,表單驗(yàn)證能通過設(shè)置驗(yàn)證規(guī)則驗(yàn)證用戶的輸入,并對(duì)不規(guī)范的輸入做出對(duì)應(yīng)提示,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2023-12-12

最新評(píng)論