@provide
ts
provide<T>(key: ContextKey<T>): ValueDecorator<T>Field sugar over $provide().
Usage
ts
@component({ name: 'Counter' })
class Counter extends Base {
@provide(CountContext)
count = signal(0);
increment() {
this.count.value += 1;
}
}The field's value is provided to the subtree, as it is. Nothing is wrapped, so the type of the key is the contract from end to end.
It is instance-scoped
Like $provide(), it is never released and dies with the element. A component whose declaration is withdrawn keeps providing until its element goes.
A plain field or an accessor
ts
@component({ name: 'Counter' })
class Counter extends Base {
@provide(CountContext) a = signal(0);
@provide(CountContext) accessor b = signal(0);
}The function form
ts
class Counter extends Base {
static config = { name: 'Counter' };
count = this.$provide(CountContext, signal(0));
}Identical behaviour, no build step — and $provide() returns the value, which is why the field initializer reads the same way.
See also
@inject— the other half- Shared state