Change value of static field from decorator (2018-09)? #15429
-
I'm wondering if its possible to change the value of a class variable when using legacy decorators version 2018-09?
Value on the descriptor is undefined and trying to add it causes the error I'm using Lit, and the latest version it supports is unfortunately 2018-09 so no chance of upgrading. My end goal is to make a decorator that can auto add my theme CSS files to the Lit elements static styles array. I did also try making a class decorator, but since the styles are static, I couldn't figure out a way to get that to work. Ideally I'd love to have a decorator like this, which would auto inject my theming into the styles array.
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
This should work: const addItem = (newValue) => {
return function (target) {
const { initializer } = target;
target.initializer = function () {
return [
...initializer.call(this),
newValue,
];
};
};
};
class Test {
@addItem(4)
static values = [1,2,3];
}
const test = new Test();
console.log(test.constructor.values) // [1,2,3,4] fields values are wrapped in an |
Beta Was this translation helpful? Give feedback.
This should work:
fields values are wrapped in an
initializer
function.