简体   繁体   中英

How to access the object that used Decorator?

I want to inject some HTML Elements that requires from many components by using decorator. This is the usage I want to implement:

const serializedCSS = '...';    // Serialized CSS

@injectStyle(serializedCSS);
class MyComponent extends HTMLElement {
   ...

I want to send serialized css to decorator, however I can't found how to access the "MyComponent" class, I have to access it's own property to create new style element and append as child.

function injectStyle(target: any) {
    console.log(target);

    // ??? HOW TO ACCESS THE OBJECT? ///

    function f(...args:any) {

    }

    return f;
}

I can't find any related solution about this. Any advice will very appreciate it!

Here's the my solution.

export function injectStyle(css:string, ...args:any) {
    return function(target:any) {
        // Now I can access target object with "target"!
        target.doSomething ... 
        ...
    };
}

Now I can use injectStyle decorator something like this:

@injectStyle(require('./style.css'))
class App extends HTMLElement {
    ...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM