Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

提交作业:事件代理实现spm #6

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
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
32 changes: 32 additions & 0 deletions src/components/HelloWorld.vue
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,38 @@ export default {
return {
spmText: 'xx.xx.xx'
}
},
mounted() {
// 理论上相关代码都应该在单独的sdk js文件内部,但是由于测试用例demo期望结果在和vue的data数据对比,
// 需要设置vue组件的data所以代码先放在了组件生命周期了

function findSpmAttr(dataset = {}) {
for(let key in dataset) {
if (key.startsWith('spm')) {
return key;
}
}
}

document.body.addEventListener('click', (e) => {
let node = e.target;
let spmAttr = findSpmAttr(node.dataset);

if (node && spmAttr) {
let logDataArr = [];
logDataArr.unshift(node.dataset[spmAttr]);
while (node && node.parentNode && node.parentNode.nodeName.toUpperCase() !== 'BODY') {
node = node.parentNode;
let dataset = node.dataset;
spmAttr = findSpmAttr(dataset);
if (spmAttr) {
logDataArr.unshift(node.dataset[spmAttr]);
}
}

this.spmText = logDataArr.join('.');
}
}, false)
}
}
</script>
Expand Down