Skip to content

[BUG] Metadata defined on a component can't be retreived from a mixin #536

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

Open
amura11 opened this issue May 28, 2021 · 1 comment
Open

Comments

@amura11
Copy link

amura11 commented May 28, 2021

I wanted to create a decorator that would add metadata to my class component then be able to pull that metadata out using a mixin and the beforeCreate hook (I'm not sure if that's the right term). However, when the method runs and I try to get the metadata the metadata isn't there. Looking at the source for this library I saw a function that looks like it would copy the metadata (this file) which makes me think it should be possible.

Code
My code is much more complex than this but it boils down to essentially this:

const metadataKey: string = "myMetadataKey";

function MyDecorator(token: string) {
    return (target: any, property: string) => {
        Reflect.defineMetadata(metadataKey, token, target);
    };
}

@Component
export default class MyView extends Vue {
	
	@MyDecorator("test")
	private myProperty!: string;
}

Vue.mixin({
	beforeCreate() {
		console.log(Reflect.getMetadata(metadataKey, this)); //This doesn't work for me
	}
});

Is this something I can even achieve?

P.S. I'm not sure if this is even related to this library or Vue itself, this seemed like the best place to start

@amura11
Copy link
Author

amura11 commented Jun 18, 2021

I still haven't been able to find a solution to this so I created a test project that recreates the issue with a minimal amount of code. The test project can be found here.

While testing this I stepped through the code and found that at this line if I call Reflect.getMetadataKeys(Component.prototype) I see my metadata key there.

I may be wrong about this but is it possible that the metadata on the prototype should also be copied in the copyReflectionMetadata method?

** Edit **

I decided to throw a breakpoint just at the beginning of copyReflectionMetadata and call forwardMetadata(to.prototype, from.prototype) to see what would happen. When I did that, my code worked as expected.

Is there a reason for not doing this in copyReflectionMetadata that I'm missing?

@amura11 amura11 changed the title Unable to get metadata using a Mixin [BUG] Metadata defined on a component can't be retreived from a mixin Jun 18, 2021
amura11 added a commit to amura11/vue-class-component that referenced this issue Jun 18, 2021
Added a call to forward metadata from the provided object's prototype
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant