Subscribe on changes!

The value of this.$el when use Teleport in SFC

avatar
Dec 10th 2020

Version

3.0.0

Reproduction link

https://codepen.io/hyjiacan/pen/ZEppqay

Steps to reproduce

Just open the DevTools Window to see the output.

What is expected?

The value of this.$el to be the actual element p#content

What is actually happening?

Got <!--teleport start--> instead

avatar
Dec 10th 2020

The <teleport> will render <!--teleport start--> & <!--teleport end--> in dom to mark which doms need to teleport.
As I see, The root element is containing a <teleport>, so this.$el must be the <teleport> rendered product.

avatar
Dec 10th 2020

The <teleport> will render <!--teleport start--> & <!--teleport end--> in dom to mark which doms need to teleport. As I see, The root element is containing a <teleport>, so this.$el must be the <teleport> rendered product.

Is there any suggestion to get the div#content without ref ?

avatar
Dec 10th 2020

document.getElementById

avatar
Dec 10th 2020

As a workaround, you can use a custom directive to get the element of the inner vNode (you need to write render function if it is a slot).

// using render function
import { withDirectives, h, Teleport } from 'vue'

const dir = {
  mounted (el) {
    console.log(el)
  },
  updated (el) {
    console.log(el)
  }
}

export default {
  render () {
    const firstSlotVNode = this.$slots.default()[0]
    return h(Teleport, null, [
      withDirectives(
        firstSlotVNode,
        [[dir]]
      )
    ])
  }
}

image

image

avatar
Dec 10th 2020

Closing as this is working as intended

avatar
Jan 12th 2021

As a workaround, you can use a custom directive to get the element of the inner vNode (you need to write render function if it is a slot).

// using render function
import { withDirectives, h, Teleport } from 'vue'

const dir = {
  mounted (el) {
    console.log(el)
  },
  updated (el) {
    console.log(el)
  }
}

export default {
  render () {
    const firstSlotVNode = this.$slots.default()[0]
    return h(Teleport, null, [
      withDirectives(
        firstSlotVNode,
        [[dir]]
      )
    ])
  }
}

image

image

can't saw your image

avatar
Jan 12th 2021

As a workaround, you can use a custom directive to get the element of the inner vNode (you need to write render function if it is a slot).

// using render function
import { withDirectives, h, Teleport } from 'vue'

const dir = {
  mounted (el) {
    console.log(el)
  },
  updated (el) {
    console.log(el)
  }
}

export default {
  render () {
    const firstSlotVNode = this.$slots.default()[0]
    return h(Teleport, null, [
      withDirectives(
        firstSlotVNode,
        [[dir]]
      )
    ])
  }
}

image image

can't saw your image

i don't have to use render function. do you have another methods ?