useScopedAttrs.ts 568 字节
Newer Older
fxy060608's avatar
fxy060608 已提交
1
import { onMounted, getCurrentInstance, reactive } from 'vue'
Q
qiang 已提交
2 3 4 5 6 7 8 9 10 11

interface State {
  attrs: Record<string, string>
}
export function useScopedAttrs() {
  const state: State = reactive({
    attrs: {},
  })

  onMounted(() => {
fxy060608's avatar
fxy060608 已提交
12 13 14
    let instance = getCurrentInstance()
    while (instance) {
      const scopeId = (instance.type as any).__scopeId
Q
qiang 已提交
15
      if (scopeId) {
fxy060608's avatar
fxy060608 已提交
16
        state.attrs[scopeId] = ''
Q
qiang 已提交
17
      }
fxy060608's avatar
fxy060608 已提交
18 19 20 21
      instance =
        instance.proxy && instance.proxy.$mpType === 'page'
          ? null
          : instance.parent
Q
qiang 已提交
22 23 24 25 26 27 28
    }
  })

  return {
    state,
  }
}