-
Notifications
You must be signed in to change notification settings - Fork 49
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
@target returns undefined using Vite #307
Comments
I think I see the problem. In the compiled output the class looks like this: export let HelloWorldElement = class extends HTMLElement {
output1;
get output2() {
return findTarget(this, "output2");
}
greet() {
if (this.output1)
this.output1.textContent = `Hello, output 1!`;
if (this.output2)
this.output2.textContent = `Hello, output 2!`;
}
}; The line This is a bug with Catalyst that should be fixed - but it won't be especially easy, sadly. We could check for defined properties using One solution for now, if you're using typescript, is to use import { controller, target, findTarget } from '@github/catalyst';
@controller
export class HelloWorldElement extends HTMLElement {
- @target output1: HTMLElement;
@target declare output1: HTMLElement;
get output2() {
return findTarget(this, 'output2') as HTMLElement;
}
greet() {
if (this.output1) this.output1.textContent = `Hello, output 1!`;
if (this.output2) this.output2.textContent = `Hello, output 2!`;
}
}
Another solution is to transpile class field syntax, which avoids the need for You are right about 2.0 - it will break away from non-standard decorators and use the Stage 3 Decorators proposal which allows for field initializers, which would fix this problem... but that'll be a while off. |
I will explore both options but I can tell that using |
Hi !
I am experiencing some issues with a simple example using vite. When I use @target it returns undefined as if the Object.defineProperty wasn't working properly. However, it works well without a decorator. I used the example provided here in the docs and added a button to trigger the action.
I created a minimal reproduction here, let me know if you prefer a github repo. We can see that output1 does not exists but the second one is here.
I believe this is fixed in the upcoming V2 but it appears that the V2 will be experiencing some delay because of the new decorator standards added to ECMAScript so I wanted to make it work with the stable release as we aim to use it in production.
Thanks in advance for any help and let me know if I can provide more details or context.
The text was updated successfully, but these errors were encountered: