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

vue如何自定義封裝API組件

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

自定義封裝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>
? ?//多個(gè)提示框命名
? ? let seed = 0;
? ? function getUuid() {
? ? ? ? return 'alert_' + (seed++);
? ? }
?? ?export default {
?? ??? ?data() {
?? ??? ??? ?return {
? ? ? ? ? ? ? ? notices: []//多個(gè)提示框保存至數(shù)組
?? ??? ??? ?}
?? ??? ?},
?? ??? ?methods:{
?? ??? ? ?add(notice) {
? ? ? ? ? ? ? ? const name = getUuid();//獲取當(dāng)前提示框名稱
? ? ? ? ? ? ? ? //Object.assign 淺拷貝不會(huì)跳過(guò)那些值為 [null] 或 [undefined]的源對(duì)象
? ? ? ? ? ? ? ? // Object.assign(target, ...sources);target: 目標(biāo)對(duì)象,sources:源對(duì)象
? ? ? ? ? ? ? ? let _notice = Object.assign({
? ? ? ? ? ? ? ? ? ? name: name
? ? ? ? ? ? ? ? }, notice);
? ? ? ? ? ? ? ? //將當(dāng)前提示框標(biāo)簽保存到notices
? ? ? ? ? ? ? ? this.notices.push(_notice);
? ? ? ? ? ? ? ? // 定時(shí)移除,單位:秒
? ? ? ? ? ? ? ? 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是個(gè)函數(shù)需求參數(shù)為properties
Alter.newInstance=properties=>{
?? ?const props=properties || {};
?? ?//創(chuàng)建一個(gè)Vue組件對(duì)象
?? ?const Instance=new Vue({
?? ??? ?data:props,
?? ??? ?render(h){
?? ??? ??? ?return h(Alter,{
?? ??? ??? ??? ?props:props
?? ??? ??? ?})
?? ??? ?}
?? ?});
?? ?//等待接口調(diào)用的時(shí)候在實(shí)例化組件,避免進(jìn)入頁(yè)面就直接掛載到body上
?? ?const component=Instance.$mount();
?? ?//實(shí)例化一個(gè)組件,然后掛載到body上
?? ?document.body.appendChild(component.$el);
?? ?//通過(guò)閉包維護(hù)alter組件的引用
?? ?const alter=Instance.$children[0];
?? ?return{
?? ??? ?//Alter組件對(duì)外暴露的兩個(gè)方法
?? ??? ?add(noticeProps){
?? ??? ??? ?alter.add(noticeProps);
?? ??? ?},
?? ??? ?remove(name){
?? ??? ??? ?alter.remove(name);
?? ??? ?}
?? ?}
}
//提示單例
let messageInstance;
function getMessageInstance(){
?? ?messageInstance=messageInstance || Alter.newInstance();
?? ?return messageInstance;
}
//定義函數(shù)實(shí)現(xiàn)
function notice({time='',content=''}){
?? ?let instance=getMessageInstance();
?? ?instance.add({
?? ??? ?time:1.5,
?? ??? ?content:''
?? ?})
}
//公布方法
export default{
?? ?info(options){
?? ??? ?return notice(options);
?? ?}
}

3.導(dǎo)入Vue

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

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

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

子組件

父組件

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

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

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

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

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

相關(guān)文章

  • 如何基于vue-cli3.0構(gòu)建功能完善的移動(dòng)端架子

    如何基于vue-cli3.0構(gòu)建功能完善的移動(dòng)端架子

    這篇文章主要介紹了基于vue-cli3.0構(gòu)建功能完善的移動(dòng)端架子,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-04-04
  • Element ui table表格內(nèi)容超出隱藏顯示省略號(hào)問(wèn)題

    Element ui table表格內(nèi)容超出隱藏顯示省略號(hào)問(wèn)題

    這篇文章主要介紹了Element ui table表格內(nèi)容超出隱藏顯示省略號(hào)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,
    2023-11-11
  • Vue中的ESLint配置方式

    Vue中的ESLint配置方式

    這篇文章主要介紹了Vue中的ESLint配置方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-03-03
  • Vue樹(shù)表格分頁(yè)的實(shí)現(xiàn)方法詳解

    Vue樹(shù)表格分頁(yè)的實(shí)現(xiàn)方法詳解

    這篇文章主要介紹了Vue樹(shù)表格分頁(yè)的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧
    2022-10-10
  • vue中用qrcode庫(kù)將超鏈接生成二維碼圖片的示例代碼

    vue中用qrcode庫(kù)將超鏈接生成二維碼圖片的示例代碼

    生成二維碼是一種常見(jiàn)的需求,無(wú)論是用于商業(yè)宣傳還是個(gè)人分享,二維碼都可以提供快速方便的方式來(lái)傳遞信息,在Vue框架中,我們可以使用qrcode庫(kù)來(lái)輕松地生成二維碼,本篇博文將介紹如何安裝qrcode庫(kù),并通過(guò)一個(gè)實(shí)際例子來(lái)展示如何生成二維碼,需要的朋友可以參考下
    2023-12-12
  • Vue實(shí)現(xiàn)數(shù)據(jù)篩選與搜索功能的示例代碼

    Vue實(shí)現(xiàn)數(shù)據(jù)篩選與搜索功能的示例代碼

    在許多Web應(yīng)用程序中,數(shù)據(jù)篩選和搜索是關(guān)鍵的用戶體驗(yàn)功能,本文將深入探討在Vue中如何進(jìn)行數(shù)據(jù)篩選與搜索的實(shí)現(xiàn),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2023-10-10
  • vue3實(shí)戰(zhàn)教程之a(chǎn)xios的封裝和環(huán)境變量

    vue3實(shí)戰(zhàn)教程之a(chǎn)xios的封裝和環(huán)境變量

    這篇文章主要給大家介紹了關(guān)于vue3實(shí)戰(zhàn)教程之a(chǎn)xios的封裝和環(huán)境變量的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2022-02-02
  • 詳解vue-cli 構(gòu)建項(xiàng)目 vue-cli請(qǐng)求后臺(tái)接口 vue-cli使用axios、sass、swiper

    詳解vue-cli 構(gòu)建項(xiàng)目 vue-cli請(qǐng)求后臺(tái)接口 vue-cli使用axios、sass、swiper

    本文通過(guò)實(shí)例代碼給大家詳細(xì)介紹了vue-cli 構(gòu)建項(xiàng)目 vue-cli請(qǐng)求后臺(tái)接口 vue-cli使用axios、sass、swiper的相關(guān)知識(shí),需要的朋友可以參考下
    2018-05-05
  • 關(guān)于vue3編寫(xiě)掛載DOM的插件問(wèn)題

    關(guān)于vue3編寫(xiě)掛載DOM的插件問(wèn)題

    這篇文章主要介紹了vue3編寫(xiě)掛載DOM的插件的問(wèn)題,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-07-07
  • 使用vuex的state狀態(tài)對(duì)象的5種方式

    使用vuex的state狀態(tài)對(duì)象的5種方式

    本文給大家介紹了使用vuex的state狀態(tài)對(duì)象的5種方式,給大家貼出了我的vuex的結(jié)構(gòu),感興趣的朋友跟隨腳本之家小編一起學(xué)習(xí)吧
    2018-04-04

最新評(píng)論