blob: 7b7a821bec1c26b1fb2528b97a7bb4609c38766c (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
|
import app from '../main.js'
//异步写法 暂时不用
export async function getParentAsync(context, componentPath = '') {
await app.$nextTick(() => {
}, 10)
return getParent(context, componentPath = '')
}
export function getParent(context, componentPath = '') {
var currentIndex = null;
var currentName = null
!context ? context = this : ''
//获取父级节点
var parent = context && context.$parent || null;
var siblings = (parent && parent.$children) || [];
currentIndex = siblings.findIndex((item, index) => {
return item._uid === context._uid
});
currentName = context.$options.name || 'anonymousComponent'
// var currentPath = currentName + currentIndex //加index,异步组件 会导致索引变化
var currentPath = currentName
componentPath = componentPath ? componentPath + '_' + currentPath : currentPath
if (parent) {
return getParent(parent, componentPath)
}
return componentPath
}
export const idscope = {
bind: function (el, binding, vnode) {
var context = vnode.context;
var id = el.getAttribute('id') || null;
// 同步写法
var prefix = getParent(context);
if (id) {
var scopedId = id + '-_' + prefix;
el.setAttribute('id', scopedId)
}
//异步写法
// getParentAsync(context).then((prefix) => {
// if (id) {
// var scopedId = id + '-_' + prefix;
// el.setAttribute('id', scopedId)
// }
// });
}
}
|