UI組件
Vue開發插件流程
本來是昨天要寫總結的,感覺自己寫不好,就放棄了。今天看到了iview和element有一些摩擦,是關於代碼借鑒的問題(哈哈),不做評價。誰下生會寫組件,我仿(chao)寫了radonUI,這個組件體量比較小,好仿寫。就是以這個為切入口寫UI組件,先自己寫,然后在仿寫。看看別人寫的比如bootstrap,這樣提高挺快的。其實UI組件套路差不多,寫過一遍就有感覺了。不像以前沒想法、沒思路。
概述
大部分組件我都寫了一遍,剩下的要參考iview和element組件。這些組件中表格、表單、tree還是比較有難度的,前倆個還沒有實踐。下面是我的總結
全局插件notification
- 思考:在全局組件中要做到像window.alert()一樣調用,在代碼任何的地方調用,就有收集‘調用結果’的地方。
- 這時候插件思想就出現了,就像把alert寫在window上,把調用的方法寫在Vue上
代碼:
// plugins.js
import Vue from 'vue'
import Notification from './Notification.js'
export default {
// install是Vue寫插件固有方法
install: function (Vue, {
notification=true
}={}) {
if ( notification ) {
Notification(Vue);
}
}
};
{notification=true}={}
是options,寫插件的時候可以提供這個選項,他的作用決定組件是否自動加載。這樣的調用插件可以
import myPlugin from './plugins/plugins.js'
Vue.use(myPlugin)
如果{notification=false}={}
就改變調用方式
import myPlugin from './plugins/plugins.js'
Vue.use(myPlugin, {
notification: true
})
// Notification.js
import Vue from 'vue'
import { olNotification } from "../components/index"
// 注冊全局的組件
// 創建一個div 把olNotification組件掛在到div中,就可以調用組件中方法了,把‘調用結果’放到組件(olNotification)中,來展示視圖
const div = document.createElement('div');
div.innerHTML = `<ol-notification></ol-notification>`;
document.body.appendChild(div);
const notification = new Vue({
el: div,
components: { olNotification }
}).$children[0];
export default function() {
Vue.prototype.$Notification = {
remove (item, duration){
setTimeout(() => {
notification.closeItem(item)
}, duration)
},
create(type, title, content, duration){
let data = {
title,
content,
duration
}
// 把‘調用結果’放到組件
notification.addItem(data)
if(duration){
// 一段時間(duration)把‘調用結果’移除組件
this.remove(data, duration)
}
},
// 四種組件形態
success (title, content, duration) {
this.create('success', title, content, duration)
},
info (title, content, duration) {
this.create('info', title, content, duration)
},
warning (title, content, duration) {
this.create('warning', title, content, duration)
},
failed (title, content, duration) {
this.create('failed', title, content, duration)
}
}
}
// olNotification.vue
<style lang = "sass" >
.ol-notification-container {
position: fixed;
top: 1rem;
right: 1rem;
z-index: 9999;
}
.ol-notification{
position: relative;
width: 20rem;
background-color: #fff;
margin-right: 1rem;
border: 1px solid #eaf8fe;
border-radius: 4px;
padding: 1rem 1.5rem;
margin-bottom: 1rem;
&.success,
&.warning,
&.failed,
&.info {
padding-left: 4rem;
}
&.success {
.rd-notification-icon {
color: #87d068;
}
}
&.info {
.rd-notification-icon {
color: #2db7f5;
}
}
&.warning {
.rd-notification-icon {
color: #fa0;
}
}
&.failed {
.rd-notification-icon {
color: #f50;
}
}
}
.ol-notification-title {
font-size: .9rem;
}
.ol-notification-content {
color: #999;
font-size: .8rem;
line-height: 1.5;
margin: 0;
}
.ol-notification-close {
position: absolute;
top: .2rem;
right: .5rem;
font-size: .8rem;
color: #ccc;
}
.ol-notification-close:hover {
color: #969696;
}
.ol-notification-icon{
position: absolute;
top: 50%;
font-size: 1.5rem;
left: 1rem;
margin-top: -.75rem;
line-height: 1.5rem;
}
.notification-enter {
opacity: 0;
}
.notification-enter-active, .notification-leave {
transition: opacity .5s ease;
}
.notification-leave-active {
opacity: 0;
position: absolute;
}
.notification-move {
transition: transform .5s cubic-bezier(.55,0,.1,1);
}
</style>
<template>
<div class="ol-notification-container" >
<transition-group name="notification">
<div
class="ol-notification"
v-for="(item, index) in allItem"
v-bind:key="item"
>
<span class="ol-notification-title">{{item.title}}</span>
<p class="ol-notification-content">{{item.content}}</p>
<span class="ol-notification-close ion-close-round" @click="closeItem(item)"></span>
</div>
</transition-group >
</div>
</template>
<script>
export default {
computed: {
},
components: {
},
data () {
return {
allItem: []
}
},
mounted() {
},
methods:{
closeItem (item) {
this.allItem = this.allItem.filter(function(current) {
return current !== item
})
},
addItem (item) {
this.allItem.push(item);
}
}
}
</script>
createObjectURL
window.URL.createObjectURL(blob|| file) 方法會根據傳入的參數創建一個指向該對象的URL,這個URL 的生命周期僅僅存在於被創建的文檔里面新的對象URL指向執行的File對象或者是Blob對象.
File對象或者Blob對象
這里大概說下File對象和Blob對象:
File對象,就是一個文件,比如我用input type="file"標簽來上傳文件,那么里面的每個文件都是一個File對象.
Blob對象,就是二進制數據,比如通過new Blob()創建的對象就是Blob對象.又比如,在XMLHttpRequest里,如果指定responseType為blob,那么得到的返回值也是一個blob對象.
注意點:
每次調用createObjectURL的時候,一個新的URL對象就被創建了.即使你已經為同一個文件創建過一個URL. 如果你不再需要這個對象,要釋放它,需要使用URL.revokeObjectURL()方法. 當頁面被關閉,瀏覽器會自動釋放它,但是為了最佳性能和內存使用,當確保不再用得到它的時候,就應該釋放它.
二.URL.revokeObjectURL
URL.revokeObjectURL()方法會釋放一個通過URL.createObjectURL()創建的對象URL. 當你要已經用過了這個對象URL,然后要讓瀏覽器知道這個URL已經不再需要指向對應的文件的時候,就需要調用這個方法.
具體的意思就是說,一個對象URL,使用這個url是可以訪問到指定的文件的,但是我可能只需要訪問一次,一旦已經訪問到了,這個對象URL就不再需要了,就被釋放掉,被釋放掉以后,這個對象URL就不再指向指定的文件了.
比如一張圖片,我創建了一個對象URL,然后通過這個對象URL,我頁面里加載了這張圖.既然已經被加載,並且不需要再次加載這張圖,那我就把這個對象URL釋放,然后這個URL就不再指向這張圖了.