最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - get v-id-xx value for scoped css on Vue Single File Component - Stack Overflow

programmeradmin2浏览0评论

When adding elements via pure js on Vue Single File Component, the added elements don't have v-id-xx attribute for scoped css.

How can I get THE ponent's v-id-hash value by pure js?

When adding elements via pure js on Vue Single File Component, the added elements don't have v-id-xx attribute for scoped css.

How can I get THE ponent's v-id-hash value by pure js?

Share Improve this question asked Apr 19, 2018 at 11:23 yokoiwayokoiwa 1071 silver badge5 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 14

The scoped-style data ID is added to the ponent instance as:

this.$options._scopeId // returns something like 'data-v-763db97b'

This way you could add it as attribute using:

somElement.setAttribute(this.$options._scopeId, "");

Here's a CodeSandbox demo showing an example.

In a Vue 3 directive, we can get the ponent's scope ID using the binding parameter available in directive lifecycle hooks.

Example:

In mounted hooks:

mounted(el, binding, vnode, prevVnode) {
  const span = document.createElement("span");
  console.log("scope_id", binding.instance.$options.__scopeId);
  span.setAttribute(binding.instance.$options.__scopeId, "");
  el.appendChild(span);
},

In the DOM, the element we will have the scope ID attached. Now we don’t need to use :deep() or /deep/ anymore for the directive-created element.

<span data-v-0fb92912=""></span>
发布评论

评论列表(0)

  1. 暂无评论