Vue3中埋點指令封裝詳解
前言
對于Vue項目來說,如果遇到一個埋點的需求,我們最好的解決方案就是封裝一個指令,然后再有埋點需求的地方使用,接下來就來封裝一個埋點指令吧。
代碼實現(xiàn)
track.ts文件
import HttpAxios from '@/utils/axios'
?
//定義埋點請求
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 {
? ? ? ? ? ? ?// 若未定義回調函數(shù),則默認調用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文件
// 引入埋點
import VTrack from '@monorepo/shared/utils/track'
?
// 創(chuàng)建vue實例
const app = createApp(App)
?
// 1.掛載埋點,沒有回調函數(shù)的方式
app.use(VTrack, { systemName: '基礎平臺', debug: false })
?
// 2.掛載埋點,回調函數(shù)的方式
app.use(VTrack, {
?callback(data, e) {
? ?//可以自定義埋點請求
? ?console.log(data, e);
},
?systemName: '基礎平臺',
?debug: false,
});使用:
<template>
?<button v-track="{ menuName: '按鈕' }">DEBUG</button>
</template>關于指令項目規(guī)范化
在src目錄下,創(chuàng)建directives文件夾存放該項目所需的指令,如圖所示:

index.ts文件統(tǒng)一注冊指令:
import type { App } from 'vue'
import { hasRole } from './permission/hasRole'
import { hasPermi } from './permission/hasPermi'
?
/**
* 導出指令:v-xxx
* @methods hasRole 用戶權限,用法: v-hasRole
* @methods hasPermi 按鈕權限,用法: v-hasPermi
*/
export const setupAuth = (app: App<Element>) => {
?hasRole(app)
?hasPermi(app)
}main.ts文件
// 指令
import * as directives from '@/directives'
?
// 創(chuàng)建vue實例
const app = createApp(App)
?
// 注冊指令
for (const key in directives) {
?directives[key](app)
}以上就是Vue3中埋點指令封裝詳解的詳細內容,更多關于Vue3埋點指令封裝的資料請關注腳本之家其它相關文章!
相關文章
vue2.0使用Sortable.js實現(xiàn)的拖拽功能示例
本篇文章主要介紹了vue2.0使用Sortable.js實現(xiàn)的拖拽功能示例,具有一定的參考價值,感興趣的小伙伴們可以參考一下。2017-02-02
詳解Vue的computed(計算屬性)使用實例之TodoList
本篇文章主要介紹了詳解Vue的computed(計算屬性)使用實例之TodoList,具有一定的參考價值,有興趣的可以了解一下2017-08-08
Vue如何使用ElementUI對表單元素進行自定義校驗及踩坑
有一些驗證不是通過input select這樣的受控組件來觸發(fā)驗證條件的 ,可以通過自定義驗證的方法來觸發(fā),下面這篇文章主要給大家介紹了關于Vue如何使用ElementUI對表單元素進行自定義校驗及踩坑的相關資料,需要的朋友可以參考下2023-02-02
elementUI實現(xiàn)下拉選項加多選框的示例代碼
因產品需求和UI樣式調整,本文主要實現(xiàn)elementUI下拉選項加多選框的示例代碼,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-10-10
vue-router如何實時動態(tài)替換路由參數(shù)(地址欄參數(shù))
這篇文章主要介紹了vue-router如何實時動態(tài)替換路由參數(shù)(地址欄參數(shù)),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-09-09

