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

Methods exposed with defineExpose are not available on custom element #5540

Closed
mgibas opened this issue Mar 7, 2022 · 15 comments · Fixed by #6256
Closed

Methods exposed with defineExpose are not available on custom element #5540

mgibas opened this issue Mar 7, 2022 · 15 comments · Fixed by #6256
Labels
has PR A pull request has already been submitted to solve the issue scope: custom elements ✨ feature request New feature or request

Comments

@mgibas
Copy link

mgibas commented Mar 7, 2022

Version

3.2.31

Reproduction link

stackblitz.com

Steps to reproduce

Open repro and you should see 'hello' in the console instead of Uncaught TypeError: ce.value.hello is not a function

What is expected?

Methods exposed with defineExpose are available on given component instance

What is actually happening?

They are not exposed

@LinusBorg
Copy link
Member

LinusBorg commented Mar 7, 2022

Is there anything in the docs for defineCusromElement giving you the impression that they should be?

I don't think we planned for that, it's simply not supported. We don't expose anything about the component on the custom element wrapper except its props.

@mgibas
Copy link
Author

mgibas commented Mar 10, 2022

I was just following this documentation and assumed that if defineExpose default behavior is not documented there us not supported (like slots) it means that this is a bug. Which also makes sense when properties are exposed.

Either way if it's like a triage issue how can we turn this into feature?

@hcg1023
Copy link
Contributor

hcg1023 commented Apr 24, 2022

I think it's useful for expose to allow certain things to be exposed, such as' input.click() ', which I currently handle with object.defineProperty

@m-ghaoui
Copy link

m-ghaoui commented May 5, 2022

I too have this problem but I got to say that it's not consistent. Some components work and other don't. I can't put a finger on a reason why. They all use defineExpose normally.

It seems to affect components that are conditionally rendered via a v-if but the stackblitz example does not do this, so I have no clue why this is happening.

@LinusBorg LinusBorg added the ✨ feature request New feature or request label May 5, 2022
@m-ghaoui
Copy link

m-ghaoui commented May 18, 2022

Why is his marked as as feature request? This is most definitely a bug. Right now we work around it by defining a useless property and listen to it in order to trigger the method that does not get exposed.

I stand corrected. See below.

@mgibas
Copy link
Author

mgibas commented May 18, 2022

@m-ghaoui even though I created this issue I can see how this is simply a new feature :)

for time being I use this composable to get to the host:

import { getCurrentInstance, ref, onMounted } from 'vue'

export function useHost() {
  const host = ref(null)
  onMounted(() => {
    host.value = getCurrentInstance()?.proxy?.$el?.parentNode?.host
  })
  return host
}

and inside your custom element you can expose your method with simple assignment:

function foo () { … }
host.value.foo = foo

@m-ghaoui
Copy link

m-ghaoui commented May 18, 2022

Oh I get it now. defineExpose() only exposes properties. Exposing a method is a new feature.

Okay 🙂 I stand corrected.

@hcg1023
Copy link
Contributor

hcg1023 commented Jul 12, 2022

I created a PR @LinusBorg #6256

@j4hangir
Copy link

I too have this problem but I got to say that it's not consistent. Some components work and other don't. I can't put a finger on a reason why. They all use defineExpose normally.

It seems to affect components that are conditionally rendered via a v-if but the stackblitz example does not do this, so I have no clue why this is happening.

I confirm, it's been bugging me that why it works on one Component and not another.

@rdsteiner
Copy link

Are there any plans on adding this new feature? The PR from @LinusBorg is getting pretty old.

@itwondersteam
Copy link

itwondersteam commented May 10, 2023

facing the same issue, temporary workaround to access the exposed method via $.exposed, e.g.

const myRef = ref()
// <myElement ref="myRef"></myElement>

myRef.value.$.exposed.ExposedMethod()

@haoqunjiang haoqunjiang added the has PR A pull request has already been submitted to solve the issue label Aug 31, 2023
@padcom
Copy link

padcom commented Nov 30, 2023

Any progress on that over-a-year-old shortcoming of Vue's implementation of webcomponents? Is that ever going to land in Vue or should we just create and use a fork to get it implemented?

@shery
Copy link

shery commented Mar 20, 2024

myRef.value.$.exposed.ExposedMethod()

Worked for me

@zzw105
Copy link

zzw105 commented Apr 26, 2024

I cannot obtain exposed in $, but I obtained exposed in _instance

myComponent.value._instance.exposed.xxx()

vue2 and vue3 can be obtained through _instance

@yyx990803
Copy link
Member

closed via #6256

@github-actions github-actions bot locked and limited conversation to collaborators Aug 18, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
has PR A pull request has already been submitted to solve the issue scope: custom elements ✨ feature request New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.