亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

vue如何自定義封裝API組件

 更新時間:2022年03月31日 10:20:05   作者:吃檸檬的貓  
這篇文章主要介紹了vue如何自定義封裝API組件,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

自定義封裝API組件

1.創(chuàng)建vue組件

<template>
?? ?<div >
?? ??? ?<div class="alert">
? ? ? ? <div class="alert-main" v-for="item in notices" :key="item.name">
? ? ? ? ? ? <div class="alert-content">{{ item.content }}</div>
? ? ? ? </div>
? ? </div>
?? ?</div >
</template>
<script>
? ?//多個提示框命名
? ? let seed = 0;
? ? function getUuid() {
? ? ? ? return 'alert_' + (seed++);
? ? }
?? ?export default {
?? ??? ?data() {
?? ??? ??? ?return {
? ? ? ? ? ? ? ? notices: []//多個提示框保存至數(shù)組
?? ??? ??? ?}
?? ??? ?},
?? ??? ?methods:{
?? ??? ? ?add(notice) {
? ? ? ? ? ? ? ? const name = getUuid();//獲取當前提示框名稱
? ? ? ? ? ? ? ? //Object.assign 淺拷貝不會跳過那些值為 [null] 或 [undefined]的源對象
? ? ? ? ? ? ? ? // Object.assign(target, ...sources);target: 目標對象,sources:源對象
? ? ? ? ? ? ? ? let _notice = Object.assign({
? ? ? ? ? ? ? ? ? ? name: name
? ? ? ? ? ? ? ? }, notice);
? ? ? ? ? ? ? ? //將當前提示框標簽保存到notices
? ? ? ? ? ? ? ? this.notices.push(_notice);
? ? ? ? ? ? ? ? // 定時移除,單位:秒
? ? ? ? ? ? ? ? const time= notice.time|| 1.5;
? ? ? ? ? ? ? ? //1.5s后調(diào)用移除方法
? ? ? ? ? ? ? ? setTimeout(() => {
? ? ? ? ? ? ? ? ? ? this.remove(name);
? ? ? ? ? ? ? ? }, time* 1000);
? ? ? ? ? ?},
? ? ? ? ? remove(name) {
? ? ? ? ? ? ? ? const notices = this.notices;
? ? ? ? ? ? ? ? for (let i = 0; i < notices.length; i++) {
? ? ? ? ? ? ? ? ? ? if (notices[i].name === name) {
? ? ? ? ? ? ? ? ? ? ? ? this.notices.splice(i, 1);
? ? ? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? ?}
?? ?}
</script>
<style lang="scss">
</style>

2.創(chuàng)建Alter.js生成組件

import Vue from 'vue'
import Alter from '/components/Alter/Alter.vue'
//Alter添加新屬性,newInstance是個函數(shù)需求參數(shù)為properties
Alter.newInstance=properties=>{
?? ?const props=properties || {};
?? ?//創(chuàng)建一個Vue組件對象
?? ?const Instance=new Vue({
?? ??? ?data:props,
?? ??? ?render(h){
?? ??? ??? ?return h(Alter,{
?? ??? ??? ??? ?props:props
?? ??? ??? ?})
?? ??? ?}
?? ?});
?? ?//等待接口調(diào)用的時候在實例化組件,避免進入頁面就直接掛載到body上
?? ?const component=Instance.$mount();
?? ?//實例化一個組件,然后掛載到body上
?? ?document.body.appendChild(component.$el);
?? ?//通過閉包維護alter組件的引用
?? ?const alter=Instance.$children[0];
?? ?return{
?? ??? ?//Alter組件對外暴露的兩個方法
?? ??? ?add(noticeProps){
?? ??? ??? ?alter.add(noticeProps);
?? ??? ?},
?? ??? ?remove(name){
?? ??? ??? ?alter.remove(name);
?? ??? ?}
?? ?}
}
//提示單例
let messageInstance;
function getMessageInstance(){
?? ?messageInstance=messageInstance || Alter.newInstance();
?? ?return messageInstance;
}
//定義函數(shù)實現(xiàn)
function notice({time='',content=''}){
?? ?let instance=getMessageInstance();
?? ?instance.add({
?? ??? ?time:1.5,
?? ??? ?content:''
?? ?})
}
//公布方法
export default{
?? ?info(options){
?? ??? ?return notice(options);
?? ?}
}

3.導入Vue

import alert from './alert.js'
// 掛載到Vue原型上
Vue.prototype.$Alert = alert
// 然后在組件中使用
this.$Alert.info({time: 1.5, content: '提示'})

如何封裝使用api形式調(diào)用的vue組件

在實際開發(fā)中一般有兩種封裝vue組件的方法:一種就是常用的的通過props父組件傳值給子組件的方法:

子組件

父組件

還有一種就是通過調(diào)用api的形式,下面例子是本人在實際項目中封裝的一個自定義圖標的彈窗組件

首先實現(xiàn)組件的UI頁面(css部分截圖不完整)

在vue文件的同目錄下新建alertTips.js文件

頁面中調(diào)用方法:

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。 

相關文章

最新評論