Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 35 additions & 3 deletions src/components/HelloWorld.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<template>
<div class="hello" data-spma="aa">
<div class="hello" data-spma="aa" @click="clickDivSpam">
<span>show spm:{{spmText}}</span>
<div data-spmb="bb">
<button data-spmc="cc">Click it</button>
Expand All @@ -16,9 +16,41 @@ export default {
name: 'HelloWorld',
data: ()=>{
return {
spmText: 'xx.xx.xx'
spmText: 'xx.xx.xx',
}
}
},
/** 事件监听无法跑通用例
mounted() {
const helloNode = document.querySelector('.hello');
if (!helloNode) return;
helloNode.addEventListener('click', (e) => {
const arr = this.findMark(e.target);
this.spmText = arr.reverse().join('.')
})
},
beforeUnmount() {
},
*/
methods: {
clickDivSpam(e) {
const arr = this.findMark(e.target);
this.spmText = arr.reverse().join('.')
},
findMark(target, res = []) {
const attributes = target.dataset;
if (!attributes) return res;
for(let k in attributes) {
if(k.startsWith('spm')) {
res.push(attributes[k]);
}
}

if (target.parentNode) {
this.findMark(target.parentNode, res);
}
return res;
},
},
}
</script>

Expand Down