@loopback/docs
Version:
Documentation for LoopBack 4
41 lines (27 loc) • 1.28 kB
Markdown
lang: en
title: 'API docs: context.inject.binding'
keywords: LoopBack 4.0, LoopBack 4
sidebar: lb4_sidebar
permalink: /doc/en/lb4/apidocs.context.inject.binding.html
<!-- Do not edit this file. It is automatically generated by API Documenter. -->
[Home](./index.md) > [@loopback/context](./context.md) > [inject](./context.inject.md) > [binding](./context.inject.binding.md)
## inject.binding variable
Inject the binding object for the given key. This is useful if a binding needs to be set up beyond just a constant value allowed by `@inject.setter`<!-- -->. The injected binding is found or created based on the `metadata.bindingCreation` option. See `BindingCreationPolicy` for more details.
<b>Signature:</b>
```typescript
binding: (bindingKey: BindingAddress<unknown>, metadata?: InjectBindingMetadata | undefined) => (target: Object, member: string, methodDescriptorOrParameterIndex?: number | TypedPropertyDescriptor<any> | undefined) => void
```
## Example
```ts
class MyAuthAction {
@inject.binding('current-user', {
bindingCreation: BindingCreationPolicy.ALWAYS_CREATE,
})
private userBinding: Binding<UserProfile>;
async authenticate() {
this.userBinding.toDynamicValue(() => {...});
}
}
```