中文亚洲精品无码_熟女乱子伦免费_人人超碰人人爱国产_亚洲熟妇女综合网

當(dāng)前位置: 首頁 > news >正文

做收費(fèi)網(wǎng)站成都百度推廣公司聯(lián)系電話

做收費(fèi)網(wǎng)站,成都百度推廣公司聯(lián)系電話,公眾號做電影采集網(wǎng)站會被封,廣州網(wǎng)站的建設(shè)Vue中如何抽取部分代碼到單獨(dú)的ts文件 本文代碼基于 簡單示例 這段代碼&#xff0c;是比較通用的代碼&#xff0c;我想抽取成一個(gè)ts文件&#xff0c;來供其他地方調(diào)用。 const groupData reactive({groupList:[] as Array<GroupV2Response>,current:{} as GroupV2Re…

Vue中如何抽取部分代碼到單獨(dú)的ts文件

本文代碼基于

簡單示例

這段代碼,是比較通用的代碼,我想抽取成一個(gè)ts文件,來供其他地方調(diào)用。

const groupData = reactive({groupList:[] as Array<GroupV2Response>,current:{} as GroupV2Response,
})const groupFunc = new class {groupType = 1;loadGroupChildrenList = (parentGroupCode:string|null) => {axios.post("/gallery/group/query-group-children",{groupCode:parentGroupCode,type:this.groupType,}).then(resp => groupData.groupList = resp.data.data)}selectThisGroup = (group:GroupV2Response) => {groupData.current = group;this.loadGroupChildrenList(group.groupCode);}clearCurrentGroup = () => {groupData.current = {} as GroupV2Response;}
}

為了將這段代碼抽取成一個(gè) TypeScript 文件供其他地方調(diào)用,并且能夠操作 Vue 的響應(yīng)式變量,可以考慮以下幾個(gè)步驟:

  1. 創(chuàng)建一個(gè) TypeScript 文件:這個(gè)文件將包含類和響應(yīng)式數(shù)據(jù)結(jié)構(gòu)。
  2. 抽象出響應(yīng)式狀態(tài):可以將響應(yīng)式狀態(tài) (reactive 對象) 作為參數(shù)傳遞給類,使得這個(gè)類更加靈活和可重用。
  3. 導(dǎo)出類和響應(yīng)式狀態(tài):從這個(gè)文件中導(dǎo)出你創(chuàng)建的類和函數(shù),這樣其他組件或文件就可以引用和實(shí)例化它。

步驟 1: 創(chuàng)建 TypeScript 文件

首先,創(chuàng)建一個(gè)新的 TypeScript 文件,例如命名為 GroupManager.ts

步驟 2: 抽象出響應(yīng)式狀態(tài)

你可以修改類,讓它接收一個(gè)響應(yīng)式狀態(tài)對象作為參數(shù)。這樣,類就不依賴于外部的具體響應(yīng)式變量了。

// GroupManager.ts
import { reactive } from 'vue';
import axios from 'axios';
import { GroupV2Response } from './path-to-type-definitions'; // 確保引入了正確的類型定義// 定義響應(yīng)式狀態(tài)接口
interface GroupData {groupList: Array<GroupV2Response>;current: GroupV2Response;
}export class GroupManager {private groupData: GroupData;groupType = 1;constructor(groupData: GroupData) {this.groupData = groupData;}loadGroupChildrenList = (parentGroupCode: string|null) => {axios.post("/gallery/group/query-group-children", {groupCode: parentGroupCode,type: this.groupType,}).then(resp => this.groupData.groupList = resp.data.data);}selectThisGroup = (group: GroupV2Response) => {this.groupData.current = group;this.loadGroupChildrenList(group.groupCode);}clearCurrentGroup = () => {this.groupData.current = {} as GroupV2Response;}
}// 可以提供一個(gè)工廠函數(shù)來創(chuàng)建和初始化響應(yīng)式數(shù)據(jù)
export function createGroupManager() {const groupData: GroupData = reactive({groupList: [],current: {} as GroupV2Response,});return { groupData, groupManager }; 
}

步驟 3: 使用抽取的文件

在其他 Vue 組件中,你可以導(dǎo)入并使用這個(gè)抽象化了的類:

// SomeComponent.vue
<script setup lang="ts">
import { createGroupManager } from './GroupManager';const { groupData, groupManager } = createGroupManager();
</script>

這樣,你就能夠在不同的組件中復(fù)用 GroupManager 類,并且每個(gè)組件可以有自己的響應(yīng)式狀態(tài)實(shí)例,這使得組件更獨(dú)立、更易于管理。

相應(yīng)外部事件

要使得 GroupManager 類在執(zhí)行特定方法(如 selectThisGroup)時(shí)能夠與其他代碼或組件進(jìn)行交互,例如觸發(fā)事件,你可以采用幾種設(shè)計(jì)策略。這些策略通常涉及使用事件發(fā)射(emitting events)、回調(diào)函數(shù)或利用 Vue 的全局事件總線。下面,我將提供幾種方法來實(shí)現(xiàn)這種交互。

方法 1: 使用事件發(fā)射(EventEmitter)

你可以使用一個(gè)事件發(fā)射器(EventEmitter)來在你的類中觸發(fā)事件。這可以通過使用 Node.js 的 events 模塊或一個(gè)簡單的第三方事件庫來實(shí)現(xiàn)。

安裝事件庫(例如 mitt,一個(gè)輕量級的事件庫):

npm install mitt

修改 GroupManager 類

// GroupManager.ts
import { reactive } from 'vue';
import axios from 'axios';
import mitt from 'mitt';
import { GroupV2Response } from './path-to-type-definitions';interface GroupData {groupList: Array<GroupV2Response>;current: GroupV2Response;
}export class GroupManager {public groupData: GroupData;groupType = 1;emitter = mitt();  // 創(chuàng)建事件發(fā)射器constructor(groupData: GroupData) {this.groupData = groupData;}loadGroupChildrenList = (parentGroupCode: string|null) => {axios.post("/gallery/group/query-group-children", {groupCode: parentGroupCode,type: this.groupType,}).then(resp => this.groupData.groupList = resp.data.data);}selectThisGroup = (group: GroupV2Response) => {this.groupData.current = group;this.loadGroupChildrenList(group.groupCode);this.emitter.emit('group-selected', group);  // 觸發(fā)事件}clearCurrentGroup = () => {this.groupData.current = {} as GroupV2Response;this.emitter.emit('group-cleared');  // 觸發(fā)事件}on(eventName: string, handler: (event?: any) => void) {this.emitter.on(eventName, handler);  // 提供方法來監(jiān)聽事件}off(eventName: string, handler: (event?: any) => void) {this.emitter.off(eventName, handler);  // 提供方法來移除監(jiān)聽}
}

使用 GroupManager 類

<script setup lang="ts">
import { createGroupManager } from './GroupManager';const { groupData, groupManager } = createGroupManager();groupManager.on('group-selected', (group) => {console.log('Selected group:', group);
});groupManager.on('group-cleared', () => {console.log('Group cleared');
});onUnmounted(() => {// 確保在組件卸載時(shí)移除事件監(jiān)聽器,避免內(nèi)存泄漏groupManager.off('group-selected', handler);groupManager.off('group-cleared', handler);
});
</script>

方法 2: 使用回調(diào)函數(shù)

另一種方法是在 GroupManager 類的構(gòu)造函數(shù)中接受一個(gè)或多個(gè)回調(diào)函數(shù)作為參數(shù),當(dāng)發(fā)生特定的動作時(shí)調(diào)用這些函數(shù)。

// GroupManager.ts
export class GroupManager {public groupData: GroupData;groupType = 1;private onSelectGroup: (group: GroupV2Response) => void;private onClearGroup: () => void;constructor(groupData: GroupData, onSelectGroup: (group: GroupV2Response) => void, onClearGroup: () => void) {this.groupData = groupData;this.onSelectGroup = onSelectGroup;this.onClearGroup = onClearGroup;}selectThisGroup = (group: GroupV2Response) => {this.groupData.current = group;this.loadGroupChildrenList(group.groupCode);this.onSelectGroup(group);}clearCurrentGroup = () => {this.groupData.current = {} as GroupV2Response;this.onClearGroup();}
}

在這種設(shè)計(jì)中,你可以在創(chuàng)建 GroupManager 實(shí)例時(shí)提供具體的回調(diào)函數(shù),使得類的行為更加靈活。這種方式適合于較為簡單的交互場景,且當(dāng)交互邏輯較為固定時(shí)。

通過上述兩種方法,你可以使 GroupManager 類在執(zhí)行其內(nèi)部方法時(shí)與外部代碼有效交互,根據(jù)具體的應(yīng)用場景選擇適合的設(shè)計(jì)模式。

http://www.risenshineclean.com/news/6112.html

相關(guān)文章:

  • 網(wǎng)站建設(shè)與網(wǎng)頁設(shè)計(jì)作業(yè)軟文世界平臺
  • 畢業(yè)設(shè)計(jì)做網(wǎng)站答辯深圳剛剛突然宣布
  • 做返利網(wǎng)站能賺錢么網(wǎng)絡(luò)營銷做得好的企業(yè)有哪些
  • 贛州網(wǎng)站制作培訓(xùn)建網(wǎng)站找哪個(gè)平臺好呢
  • 表情包在線制作網(wǎng)站網(wǎng)絡(luò)搜索關(guān)鍵詞排名
  • 網(wǎng)站站內(nèi)交換鏈接怎么做創(chuàng)建網(wǎng)站
  • 廈門做網(wǎng)頁網(wǎng)站的公司怎么自己制作一個(gè)網(wǎng)站
  • 做網(wǎng)站買二手域名網(wǎng)絡(luò)營銷推廣培訓(xùn)機(jī)構(gòu)
  • 網(wǎng)站欄目模板如何選擇谷歌seo課程
  • 大站網(wǎng)站建設(shè)百度自動優(yōu)化
  • 遵義網(wǎng)站建設(shè)中心seo優(yōu)化專員編輯
  • 龍華建設(shè)網(wǎng)站企業(yè)郵箱查詢
  • 如何在路由器上做網(wǎng)站轉(zhuǎn)跳app下載推廣
  • 網(wǎng)站建設(shè)游戲公司免費(fèi)手游推廣代理平臺渠道
  • 深圳做公司網(wǎng)站seo管理
  • dw怎么做網(wǎng)站地圖奶茶店推廣軟文500字
  • 南山網(wǎng)站多少錢什么叫seo
  • 福州營銷型網(wǎng)站建設(shè)公司今日新聞聯(lián)播
  • 公司網(wǎng)站制作企業(yè)建站平臺哪個(gè)比較權(quán)威
  • 中交建設(shè)集團(tuán) 網(wǎng)站營銷型網(wǎng)站有哪些功能
  • 做3dmax的網(wǎng)站國內(nèi)搜索引擎排名第一
  • 網(wǎng)站服務(wù)器有哪些類型有哪些類型有哪些類型有哪些類型百度推廣一年大概需要多少錢
  • 蘭州做網(wǎng)站哪家專業(yè)株洲專業(yè)seo優(yōu)化
  • 做網(wǎng)站大圖片東莞關(guān)鍵詞排名推廣
  • html編輯器哪個(gè)軟件好用網(wǎng)站優(yōu)化的方法
  • 蘇州做公司郵箱企業(yè)網(wǎng)站營銷網(wǎng)站做的好的公司
  • 慶祝網(wǎng)站上線banner圖片外貿(mào)推廣公司
  • 我想做個(gè)網(wǎng)站百度收錄的網(wǎng)站
  • 游戲網(wǎng)站seo怎么做深圳哪里有網(wǎng)絡(luò)推廣渠避
  • 邢臺網(wǎng)站建設(shè)免費(fèi)做網(wǎng)站排名吸引人的微信軟文