Vue3中埋點(diǎn)指令封裝詳解
前言
對(duì)于Vue項(xiàng)目來(lái)說(shuō),如果遇到一個(gè)埋點(diǎn)的需求,我們最好的解決方案就是封裝一個(gè)指令,然后再有埋點(diǎn)需求的地方使用,接下來(lái)就來(lái)封裝一個(gè)埋點(diǎn)指令吧。
代碼實(shí)現(xiàn)
track.ts文件
import HttpAxios from '@/utils/axios' ? //定義埋點(diǎn)請(qǐng)求 export function track(params) { ?return HttpAxios.post( ? ?'http://xxxxxx.xxxxx', ? ?params, ? { ? ? ?isCloseLoading: true ? } ) } ? export default { ?install(Vue, options) { ? ?options = options || {} ? ?/** ? ? * 格式化綁定到dom上的數(shù)據(jù) ? ? * @param {*} binding ? ? */ ? ?function formatBinding(binding) { ? ? ?let trackData = '' ? ? ?let eventMode = 'click' ? ? ?if (typeof binding.value === 'object') { ? ? ? ?if ('event' in binding.value) { ? ? ? ? ?eventMode = binding.value.event ? ? ? } ? ? ? ?if ('data' in binding.value) { ? ? ? ? ?trackData = binding.value.data ? ? ? } else { ? ? ? ? ?trackData = binding.value ? ? ? } ? ? } else { ? ? ? ?trackData = binding.value ? ? } ? ? ?return { ? ? ? ?eventMode, ? ? ? ?trackData ? ? } ? } ? ?// 初始化 ? ?if ('init' in options && options.init instanceof Function) { ? ? ?try { ? ? ? ?options.init() ? ? } catch (error) { ? ? ? ?if (options.debug) { ? ? ? ? ?console.log(error) ? ? ? } ? ? } ? } ? ?Vue.directive('track', { ? ? ?mounted(el, binding) { ? ? ? ?const format = formatBinding(binding) ? ? ? ?el.trackData = format.trackData ? ? ? ?const params = { ? ? ? ? ?systemName: options.systemName, ? ? ? ? ?...el.trackData //指令綁定的數(shù)據(jù) ? ? ? } ? ? ? ?el.addEventListener(format.eventMode, e => { ? ? ? ? ?try { ? ? ? ? ? ?if ('callback' in options && options.callback instanceof Function) { ? ? ? ? ? ? ?options.callback(params) ? ? ? ? ? } else { ? ? ? ? ? ? ?// 若未定義回調(diào)函數(shù),則默認(rèn)調(diào)用track方法 ? ? ? ? ? ? ?track(params) ? ? ? ? ? } ? ? ? ? ? ?if (options.debug) { ? ? ? ? ? ? ?console.log(el.trackData) ? ? ? ? ? } ? ? ? ? } catch (error) { ? ? ? ? ? ?if (options.debug) { ? ? ? ? ? ? ?console.log(error) ? ? ? ? ? } ? ? ? ? } ? ? ? }) ? ? }, ? ? ?update(el, binding) { ? ? ? ?const format = formatBinding(binding) ? ? ? ?el.trackData = format.trackData ? ? } ? }) } }
main.ts文件
// 引入埋點(diǎn) import VTrack from '@monorepo/shared/utils/track' ? // 創(chuàng)建vue實(shí)例 const app = createApp(App) ? // 1.掛載埋點(diǎn),沒(méi)有回調(diào)函數(shù)的方式 app.use(VTrack, { systemName: '基礎(chǔ)平臺(tái)', debug: false }) ? // 2.掛載埋點(diǎn),回調(diào)函數(shù)的方式 app.use(VTrack, { ?callback(data, e) { ? ?//可以自定義埋點(diǎn)請(qǐng)求 ? ?console.log(data, e); }, ?systemName: '基礎(chǔ)平臺(tái)', ?debug: false, });
使用:
<template> ?<button v-track="{ menuName: '按鈕' }">DEBUG</button> </template>
關(guān)于指令項(xiàng)目規(guī)范化
在src
目錄下,創(chuàng)建directives
文件夾存放該項(xiàng)目所需的指令,如圖所示:
index.ts
文件統(tǒng)一注冊(cè)指令:
import type { App } from 'vue' import { hasRole } from './permission/hasRole' import { hasPermi } from './permission/hasPermi' ? /** * 導(dǎo)出指令:v-xxx * @methods hasRole 用戶權(quán)限,用法: v-hasRole * @methods hasPermi 按鈕權(quán)限,用法: v-hasPermi */ export const setupAuth = (app: App<Element>) => { ?hasRole(app) ?hasPermi(app) }
main.ts
文件
// 指令 import * as directives from '@/directives' ? // 創(chuàng)建vue實(shí)例 const app = createApp(App) ? // 注冊(cè)指令 for (const key in directives) { ?directives[key](app) }
以上就是Vue3中埋點(diǎn)指令封裝詳解的詳細(xì)內(nèi)容,更多關(guān)于Vue3埋點(diǎn)指令封裝的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
vue2.0使用Sortable.js實(shí)現(xiàn)的拖拽功能示例
本篇文章主要介紹了vue2.0使用Sortable.js實(shí)現(xiàn)的拖拽功能示例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-02-02詳解Vue的computed(計(jì)算屬性)使用實(shí)例之TodoList
本篇文章主要介紹了詳解Vue的computed(計(jì)算屬性)使用實(shí)例之TodoList,具有一定的參考價(jià)值,有興趣的可以了解一下2017-08-08Vue如何使用ElementUI對(duì)表單元素進(jìn)行自定義校驗(yàn)及踩坑
有一些驗(yàn)證不是通過(guò)input select這樣的受控組件來(lái)觸發(fā)驗(yàn)證條件的 ,可以通過(guò)自定義驗(yàn)證的方法來(lái)觸發(fā),下面這篇文章主要給大家介紹了關(guān)于Vue如何使用ElementUI對(duì)表單元素進(jìn)行自定義校驗(yàn)及踩坑的相關(guān)資料,需要的朋友可以參考下2023-02-02vue.js選中動(dòng)態(tài)綁定的radio的指定項(xiàng)
這篇文章主要介紹了vue.js選中動(dòng)態(tài)綁定的radio的指定項(xiàng),需要的朋友可以參考下2017-06-06elementUI實(shí)現(xiàn)下拉選項(xiàng)加多選框的示例代碼
因產(chǎn)品需求和UI樣式調(diào)整,本文主要實(shí)現(xiàn)elementUI下拉選項(xiàng)加多選框的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-10-10vue-router如何實(shí)時(shí)動(dòng)態(tài)替換路由參數(shù)(地址欄參數(shù))
這篇文章主要介紹了vue-router如何實(shí)時(shí)動(dòng)態(tài)替換路由參數(shù)(地址欄參數(shù)),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-09-09