自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

面試被問到Vue?想進(jìn)一步提升?那就停下來看一下吧

開發(fā) 前端
在開發(fā)和設(shè)計一個組件的時候有可能就會繞一個大圈子,所以我非常推薦各位在學(xué)習(xí)Vue的時候先要對Vue核心的所有API都有一個了解。這篇文章我會從實(shí)踐出發(fā),遇到一些知識點(diǎn)會順帶總結(jié)一下。

[[274374]]

Vue作為最近最炙手可熱的前端框架,其簡單的入門方式和功能強(qiáng)大的API是其優(yōu)點(diǎn)。而同時因?yàn)槠銩PI的多樣性和豐富性,所以他的很多開發(fā)方式就和一切基于組件的React不同,如果沒有對Vue的API(有一些甚至文檔都沒提到)有一個全面的了解,那么在開發(fā)和設(shè)計一個組件的時候有可能就會繞一個大圈子,所以我非常推薦各位在學(xué)習(xí)Vue的時候先要對Vue核心的所有API都有一個了解。這篇文章我會從實(shí)踐出發(fā),遇到一些知識點(diǎn)會順帶總結(jié)一下。文章很長,一次看不完可以先收藏,如果你剛?cè)腴Tvue,那么相信這篇文章對你以后的提升絕對有幫助。

進(jìn)入正題,我相信不論什么項(xiàng)目幾乎都會有一個必不可少的功能,就是用戶操作反饋、或者提醒,像這樣(簡單的一個demo)。

其實(shí)在vue的中大型項(xiàng)目中,這些類似的小功能會更加豐富以及嚴(yán)謹(jǐn),而在以Vue作為核心框架的前端項(xiàng)目中,因?yàn)閂ue本身是一個組件化和虛擬Dom的框架,要實(shí)現(xiàn)一個通知組件的展示當(dāng)然是非常簡單的。但因?yàn)橥ㄖM件的使用特性,直接在模板當(dāng)中書寫組件并通過v-show或者props控制通知組件的顯示顯然是非常不方便的并且這樣意味著你的代碼結(jié)構(gòu)要變,當(dāng)各種各樣的彈層變多的時候,我們都將其掛載到APP或者一個組件下顯然不太合理,而且如果要在action或者其他非組件場景中要用到通知,那么純組件模式的用法也無法實(shí)現(xiàn)。那么有沒有辦法即用到Vue組件化特性方便得實(shí)現(xiàn)一個通知組件的展現(xiàn),那么我們可否用一個方法來控制彈層組件的顯示和隱藏呢?

目標(biāo)一

實(shí)現(xiàn)一個簡單的反饋通知,可以通過方法在組件內(nèi)直接調(diào)用。比如Vue.$confirm({...obj})

首先,我們來實(shí)現(xiàn)通知組件,相信這個大部分人都能寫出來一個像模像樣的組件,不啰嗦,直接上代碼:

  1. <template>  
  2.     <div  
  3.         :class="type"  
  4.         class="eqc-notifier">  
  5.         <i  
  6.             :class="iconClass"  
  7.             class="icon fl"/>  
  8.         <span>{{ msg }}</span>  
  9.     <!-- <span class="close fr eqf-no" @click="close"></span> -->  
  10.     </div>  
  11. </template>  
  12. <script>  
  13. export default {  
  14.     name: 'Notification',  
  15.     props: {  
  16.         type: {  
  17.             type: String,  
  18.             default: ''  
  19.         },  
  20.         msg: {  
  21.             type: String,  
  22.             default: ''  
  23.         }  
  24.     },  
  25.     computed: {  
  26.         iconClass() {  
  27.             switch (this.type) {  
  28.                 case 'success':  
  29.                     return 'eqf-info-f'  
  30.                 case 'fail':  
  31.                     return 'eqf-no-f'  
  32.                 case 'info':  
  33.                     return 'eqf-info-f'  
  34.                 case 'warn':  
  35.                     return 'eqf-alert-f'  
  36.             }  
  37.         }  
  38.     },  
  39.     mounted() {  
  40.         setTimeout(() => this.close(), 4000)  
  41.     },  
  42.     methods: {  
  43.         close() {  
  44.         }  
  45.     }  
  46.  
  47. </script>  
  48. <style lang="scss">  
  49.     .eqc-notifier {  
  50.         position: fixed;  
  51.         top: 68px;  
  52.         left: 50%;  
  53.         height: 36px;  
  54.         padding-right: 10px;  
  55.         line-height: 36px;  
  56.         box-shadow: 0 0 16px 0 rgba(0, 0, 0, 0.16);  
  57.         border-radius: 3px;  
  58.         background: #fff;  
  59.         z-index: 100; // 層級最高  
  60.         transform: translateX(-50%);  
  61.         animation: fade-in 0.3s;  
  62.     .icon {  
  63.         margin: 10px;  
  64.         font-size: 16px;  
  65.     }  
  66.     .close {  
  67.         margin: 8px;  
  68.         font-size: 20px;  
  69.         color: #666;  
  70.         transition: all 0.3s;  
  71.         cursor: pointer;  
  72.         &:hover {  
  73.             color: #ff296a;  
  74.         }  
  75.     }  
  76.     &.success {  
  77.         color: #1bc7b1;  
  78.     }  
  79.     &.fail {  
  80.         color: #ff296a;  
  81.     }  
  82.     &.info {  
  83.         color: #1593ff;  
  84.     }  
  85.     &.warn {  
  86.         color: #f89300;  
  87.     }  
  88.     &.close {  
  89.         animation: fade-out 0.3s;  
  90.     }  
  91.     }  
  92. </style> 

在這里需要注意,我們定義了一個close方法,但內(nèi)容是空的,雖然在模板上有用到,但是似乎沒什么意義,在后面我們要擴(kuò)展組件的時候我會講到為什么要這么做。

創(chuàng)建完這個組件之后,我們就可以在模板中使用了<notification type="xxx" msg="xxx" />

實(shí)現(xiàn)通過方法調(diào)用該通知組件

其實(shí)在實(shí)現(xiàn)通過方法調(diào)用之前,我們需要擴(kuò)展一下這個組件,因?yàn)閮H僅這些屬性,并不夠我們使用。在使用方法調(diào)用的時候,我們需要考慮一下幾個問題:

  •  顯示反饋的定位
  •  組件的出現(xiàn)和自動消失控制
  •  連續(xù)多次調(diào)用通知方法,如何排版多個通知

在這個前提下,我們需要擴(kuò)展該組件,但是擴(kuò)展的這些屬性不能直接放在原組件內(nèi),因?yàn)檫@些可能會影響組件在模板內(nèi)的使用,那怎么辦呢?這時候我們就要用到Vue里面非常好用的一個API,extend,通過他去繼承原組件的屬性并擴(kuò)展他。

來看代碼

 

  1. import Notifier from './Notifier.vue'  
  2. function install(Vue) {  
  3.     VueVue.notifier = Vue.prototype.notifier = {  
  4.         success,  
  5.         fail,  
  6.         info,  
  7.         warn  
  8.     }  
  9.  
  10. function open(type, msg) {  
  11.     let UiNotifier = Vue.extend(Notifier)  
  12.     let vm = new UiNotifier({  
  13.         propsData: { type, msg },  
  14.         methods: {  
  15.             close: function () {  
  16.                 let dialog = this.$el  
  17.                 dialog.addEventListener('animationend', () => {  
  18.                     document.body.removeChild(dialog)  
  19.                     this.$destroy()  
  20.                 })  
  21.                 dialog.className = `${this.type} eqc-notifier close`  
  22.                 dialog = null  
  23.             }  
  24.         }  
  25.     }).$mount()  
  26.     document.body.appendChild(vm.$el)  
  27.  
  28. function success(msg) {  
  29.     open('success', msg)  
  30.  
  31. function fail(msg) {  
  32.     open('fail', msg)  
  33.  
  34. function info(msg) {  
  35.     open('info', msg)  
  36.  
  37. function warn(msg) {  
  38.     open('warn', msg)  
  39.  
  40. Vue.use(install)  
  41. export default install 

可以看到close方法在這里被實(shí)現(xiàn)了,那么為什么要在原組件上面加上那些方法的定義呢?因?yàn)樾枰谀0迳辖壎?,而模板是無法extend的,只能覆蓋,如果要覆蓋重新實(shí)現(xiàn),那擴(kuò)展的意義就不是很大了。其實(shí)這里只是一個消息彈窗組件,是可以在模板中就被實(shí)現(xiàn),還有插件怎么注入,大家都可以自己抉擇。

同時在使用extend的時候要注意:

  •  方法和屬性的定義是直接覆蓋的
  •  生命周期方法類似余mixin,會合并,也就是原組件和繼承之后的組件都會被調(diào)用,原組件先調(diào)用

首先通過 let UiNotifier = Vue.extend(Notifier),我們得到了一個類似于Vue的子類,接著就可以通過new UiNotifier({...options})的方式去創(chuàng)建Vue的實(shí)例了,同時通過該方式創(chuàng)建的實(shí)例,會有組件定義里面的所有屬性。

在創(chuàng)建實(shí)例之后,vm.$mount()手動將組件掛載到DOM上面,這樣我們可以不依賴Vue組件樹來輸出DOM片段,達(dá)到自由顯示通知的效果。

擴(kuò)展:

(

說一下$mount,我們也許很多項(xiàng)目的主文件是這樣的

  1. new Vue({  
  2.     router,  
  3.     store,  
  4.     el: '#app',  
  5.     render: h => h(App)  
  6. }) 

其實(shí)el與$mount在使用效果上沒有任何區(qū)別,都是為了將實(shí)例化后的vue掛載到指定的dom元素中。如果在實(shí)例化vue的時候指定el,則該vue將會渲染在此el對應(yīng)的dom中,反之,若沒有指定el,則vue實(shí)例會處于一種“未掛載”的狀態(tài),此時可以通過$mount來手動執(zhí)行掛載。值得注意的是如果$mount沒有提供參數(shù),模板將被渲染為文檔之外的的元素,并且你必須使用原生DOM API把它插入文檔中,所以我上面寫的你應(yīng)該明白了吧!

  1. 這是$mount的一個源碼片段,其實(shí)$mount的方法支持傳入2個參數(shù)的,第一個是 el,它表示掛載的元素,可以是字符串,也可以是 DOM 對象,如果是字符串在瀏覽器環(huán)境下會調(diào)用 query 方法轉(zhuǎn)換成 DOM 對象的。第二個參數(shù)是和服務(wù)端渲染相關(guān),在瀏覽器環(huán)境下不需要傳第二個參數(shù)。 

)

好了,我們現(xiàn)在其實(shí)就可以在組件中:

  1. this.notifier[state](msg)來調(diào)用了,是不是很方便? 

進(jìn)階

我們剛才實(shí)現(xiàn)了在Vue中通過方法來進(jìn)行用戶反饋的提醒,再增加一個難度:

我們vue項(xiàng)目中應(yīng)該也遇到過這種情況,彈出一個對話框或是選擇框?不但要求用方法彈出,并且能接收到對話框交互所返回的結(jié)果。

這里就不詳細(xì)的分析了直接上代碼說(之前的代碼,用render來寫的組件,懶得改了,直接拿來用...),先創(chuàng)建一個對話框組件---Confirm.vue。

  1. <script>  
  2.     let __this = null  
  3.     export default {  
  4.         name: 'Confirm',  
  5.         data() {  
  6.             return {  
  7.                 config: {  
  8.                     msg: '',  
  9.                     ifBtn: '',  
  10.                     top: null  
  11.                 }  
  12.             }  
  13.         },  
  14.         created() {  
  15.             __this = this  
  16.         },  
  17.         methods: {  
  18.             createBox(h) {  
  19.                 let config = {}  
  20.                 config.attrs = {  
  21.                     id: '__confirm'  
  22.                 }  
  23.                 let children = []  
  24.                 children.push(this.createContainer(h))  
  25.                 children.push(this.createBg(h))  
  26.                 return h('div', config, children)  
  27.             },  
  28.             createBg(h) {  
  29.                 return h('div', {  
  30.                     class: 'bg',  
  31.                     on: {  
  32.                         click: __this.$cancel  
  33.                     }  
  34.                 })  
  35.             },  
  36.             createContainer(h) {  
  37.                 let config = {}  
  38.                 config.class = {  
  39.                     'box-container': true  
  40.                 }  
  41.                 if (__this.config.top) {  
  42.                     config.style = {  
  43.                         'top': __this.config.top + 'px',  
  44.                         'transform': 'translate(-50%, 0)'  
  45.                     }  
  46.                 }  
  47.                 let children = []  
  48.                 children.push(this.createContentBox(h))  
  49.                 children.push(this.createClose(h))  
  50.                 if (__this.config.ifBtn) {  
  51.                     children.push(__this.createBtnBox(h))  
  52.                 }  
  53.                 return h('div', config, children)  
  54.             },  
  55.             createContentBox(h) {  
  56.                 let config = {}  
  57.                 config.class = {  
  58.                     'content-box': true  
  59.                 }  
  60.                 return h('div', config, [__this.createContent(h)])  
  61.             },  
  62.             createContent(h) {  
  63.                 let config = {}  
  64.                 config.domProps = {  
  65.                     innerHTML: __this.config.msg  
  66.                 }  
  67.                 return h('p', config)  
  68.             },  
  69.             createClose(h) {  
  70.                 return h('i', {  
  71.                     class: 'eqf-no pointer close-btn',  
  72.                     on: {  
  73.                         click: __this.$cancel  
  74.                     }  
  75.                 })  
  76.             },  
  77.             createBtnBox(h) {  
  78.                 return h(  
  79.                     'div', {  
  80.                         class: {  
  81.                             'btn-box': true  
  82.                         }  
  83.                     }, [  
  84.                         __this.createBtn(h, 'btn-cancel middle mr10', '取消', __this.$cancel),  
  85.                         __this.createBtn(h, 'btn-primary middle mr10', '確定', __this.$confirm)  
  86.                     ])  
  87.             },  
  88.             createBtn(h, styles, content, callBack) {  
  89.                 return h('button', {  
  90.                     class: styles,  
  91.                     on: {  
  92.                         click: callBack  
  93.                     }  
  94.                 }, content)  
  95.             }  
  96.         },  
  97.         render(h) {  
  98.             return this.createBox(h)  
  99.         }  
  100.     }  
  101.     </script>    
  102.     <style scoped>  
  103.     #__confirm {  
  104.         position: fixed;  
  105.         top: 0;  
  106.         left: 0;  
  107.         z-index: 10;  
  108.         width: 100%;  
  109.         height: 100%;  
  110.     }  
  111.     #__confirm .bg {  
  112.         position: fixed;  
  113.         top: 0;  
  114.         left: 0;  
  115.         z-index: 0;  
  116.         width: 100%;  
  117.         height: 100%;  
  118.     }  
  119.     #__confirm .box-container {  
  120.         position: absolute;  
  121.         width: 500px;  
  122.         padding: 20px;  
  123.         padding-top: 30px;  
  124.         border-radius: 3px;  
  125.         background: #fff;  
  126.         z-index: 1;  
  127.         box-shadow: 2px 2px 10px rgba(0,0,0,0.4);  
  128.         top: 50%;  
  129.         left: 50%;  
  130.         transform: translate(-50%, -50%);  
  131.     }  
  132.     #__confirm .content-box {  
  133.         font-size: 14px;  
  134.         line-height: 20px;  
  135.         margin-bottom: 10px;  
  136.     }  
  137.     #__confirm .btn-box {  
  138.         margin-top: 20px;  
  139.         text-align: right;  
  140.     }  
  141.     #__confirm .close-btn {  
  142.         position: absolute;  
  143.         top: 15px;  
  144.         right: 20px;  
  145.         font-size: 16px;  
  146.         color: #666666;  
  147.     }  
  148.     #__confirm .close-btn:hover {  
  149.         color: #1593FF;  
  150.     }  
  151.         #__confirm .bg {  
  152.             position: fixed;  
  153.         }  
  154.     </style> 

然后創(chuàng)建confirm.js:

  1. 'use strict'  
  2. import Confirm from './Confirm.vue'  
  3. const confirmConstructor = Vue.extend(Confirm)  
  4. const ConfirmViewStyle = config => {  
  5.     const confirmInstance = new confirmConstructor({  
  6.         data() {  
  7.             return {  
  8.                 config  
  9.             }  
  10.         }  
  11.     })  
  12.     confirmInstanceconfirmInstance.vm = confirmInstance.$mount()  
  13.     confirmInstanceconfirmInstance.dom = confirmInstance.vm.$el  
  14.     document.body.appendChild(confirmInstance.dom)  
  15.  
  16. const close = () => {  
  17.     let dom = document.querySelector('body .modelServe-container')  
  18.     dom && dom.remove()  
  19.     Vue.prototype.$receive = null  
  20.  
  21. const closeConfirm = () => {  
  22.     let dom = document.getElementById('__confirm')  
  23.     dom && dom.remove()  
  24.     Vue.prototype.$confirm = null  
  25.  
  26. function install(Vue) {  
  27.     Vue.prototype.modelServe = {  
  28.         confirm: (obj) => {  
  29.             return new Promise(resolve => {  
  30.                 Vue.prototype.$confirm = (data) => {  
  31.                     resolve(data)  
  32.                     closeConfirm()  
  33.                 }  
  34.                 ConfirmViewStyle(obj)  
  35.             })  
  36.         }  
  37.     }  
  38.     Vue.prototype.$dismiss = close  
  39.     Vue.prototype.$cancel = closeConfirm  
  40.  
  41. Vue.use(install)  
  42. export default install 

思路很簡單,在我們創(chuàng)建的時候同時返回一個promise,同時將resolve通行證暴露給vue的一個全局方法也就是將控制權(quán)暴露給外部,這樣我們就可以向這樣,我上面的confiram.vue是直接把取消綁定成了$cancel,把確定綁定成了$confirm,所以點(diǎn)擊確定會進(jìn)入full,也就是.then中,當(dāng)然你也可以傳參數(shù)。

 

  1. this.modelServe.confirm({  
  2.     msg: '返回后數(shù)據(jù)不會被保存,確認(rèn)?',  
  3.     ifBtn: true  
  4. }).then(_ => {  
  5.     this.goBack()  
  6. }).catch()  

寫的有點(diǎn)多,其實(shí)還可以擴(kuò)展出好多技巧,比如模態(tài)框中傳一個完整的組件,并展示出來,簡單地寫一下,其實(shí)只需改動一點(diǎn)。

 

  1. import Model from './Model.vue'  
  2. const modelConstructor = Vue.extend(Model)  
  3. const modelViewStyle = (obj) => {  
  4. let component = obj.component  
  5. const modelViewInstance = new modelConstructor({  
  6.     data() {  
  7.         return {  
  8.             disabledClick: obj.stopClick // 是否禁止點(diǎn)擊遮罩層關(guān)閉  
  9.         }  
  10.     }  
  11. })  
  12. let app = document.getElementById('container')  
  13. modelViewInstancemodelViewInstance.vm = modelViewInstance.$mount()  
  14. modelViewInstancemodelViewInstance.dom = modelViewInstance.vm.$el  
  15. app.appendChild(modelViewInstance.dom)  
  16. new Vue({  
  17.     el: '#__model__',  
  18.     mixins: [component],  
  19.     data() {  
  20.         return {  
  21.             serveObj: obj.obj  
  22.         }  
  23.     }  
  24. })  
  25.  
  26. ...  
  27. Vue.prototype.modelServe = {  
  28.     open: (obj) => {  
  29.         return new Promise(resolve => {  
  30.             modelViewStyle(obj, resolve)  
  31.             Vue.prototype.$receive = (data) => {  
  32.                 resolve(data)  
  33.                 close()  
  34.             }  
  35.         })  
  36.     }  

調(diào)用:

  1. sendCallBack() {  
  2.     this.modelServe.open({  
  3.         component: AddCallback,  
  4.         stopClick: true  
  5.     }).then(data =>   
  6.         if (data === 1) {  
  7.             this.addInit()  
  8.         } else {  
  9.             this.goBack()  
  10.         }  
  11.     }) 

},

這里我們用了mixins,最后最后再簡單地介紹一下mixins,extend,extends的區(qū)別。

**- Vue.extend使用基礎(chǔ) Vue 構(gòu)造器,創(chuàng)建一個“子類”。參數(shù)是一個包含組件選項(xiàng)的對象。

  •     mixins 選項(xiàng)接受一個混入對象的數(shù)組。這些混入實(shí)例對象可以像正常的實(shí)例對象一樣包含選項(xiàng),他們將在 Vue.extend() 里最終選擇使用相同的選項(xiàng)合并邏輯合并。舉例:如果你的混入包含一個鉤子而創(chuàng)建組件本身也有一個,兩個函數(shù)將被調(diào)用。Mixin 鉤子按照傳入順序依次調(diào)用,并在調(diào)用組件自身的鉤子之前被調(diào)用。

注意(data混入組件數(shù)據(jù)優(yōu)先鉤子函數(shù)將混合為一個數(shù)組,混入對象的鉤子將在組件自身鉤子之前調(diào)用,值為對象的選項(xiàng),例如 methods, components 和 directives,將被混合為同一個對象。兩個對象鍵名沖突時,取組件對象的鍵值對。)

  •     extends 允許聲明擴(kuò)展另一個組件(可以是一個簡單的選項(xiàng)對象或構(gòu)造函數(shù)),而無需使用 Vue.extend。這主要是為了便于擴(kuò)展單文件組件。這和 mixins 類似。**

概括

  1. extend用于創(chuàng)建vue實(shí)例  
  2. mixins可以混入多個mixin,extends只能繼承一個  
  3. mixins類似于面向切面的編程(AOP),extends類似于面向?qū)ο蟮木幊?nbsp; 
  4. 優(yōu)先級Vue.extend>extends>mixins 

總結(jié)

到這里,關(guān)于如何實(shí)現(xiàn)通過方法調(diào)用一個Vue組件內(nèi)容以及用到的一些API以及原理就差不多了,代碼如有不懂得地方可以隨時提問,歡迎交流。

 

責(zé)任編輯:龐桂玉 來源: segmentfault
相關(guān)推薦

2023-09-01 18:20:43

Chrome代碼測試版

2011-07-27 12:58:43

Android MarAndroid應(yīng)用商店

2009-03-17 09:54:46

Windows 7微軟測試

2019-03-22 10:20:39

加速Windows 10啟動

2011-07-29 15:02:22

LifeSize視頻協(xié)作

2020-12-10 20:00:04

數(shù)字貨幣比特幣區(qū)塊鏈

2017-09-18 15:04:11

VMwareNSX容器

2015-06-18 14:11:29

飛康OpenStackFreeStor

2013-11-01 16:46:31

Chrome瀏覽器

2015-10-19 14:57:51

2014-01-08 10:22:28

思科Videoscape

2009-11-30 18:35:05

BizSparkDreamSparkWebSiteSpar

2010-03-15 09:40:19

Windows 8研發(fā)

2009-08-26 14:48:05

C#委托與事件

2009-12-28 10:08:07

OracleSQLDevelope開發(fā)框架

2024-05-10 15:09:34

2015-12-22 12:00:05

SDN云服務(wù)

2012-06-14 15:50:20

teradata商業(yè)洞察數(shù)據(jù)倉庫

2023-11-14 18:04:26

SQL語句性能

2011-11-10 19:44:08

思科騰訊通通信
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號