@loopback/docs
Version:
Documentation files rendered at [https://loopback.io](https://loopback.io)
97 lines (53 loc) • 2.15 kB
Markdown
---
lang: en
title: 'API docs: context.inject.binding'
keywords: LoopBack 4.0, LoopBack 4, Node.js, TypeScript, OpenAPI
sidebar: lb4_sidebar
editurl: https://github.com/loopbackio/loopback-next/tree/master/packages/context
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() function
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.
**Signature:**
```typescript
binding: (bindingKey?: string | BindingKey<unknown>, metadata?: InjectBindingMetadata) => (target: Object, member: string | undefined, methodDescriptorOrParameterIndex?: number | TypedPropertyDescriptor<any> | undefined) => void
```
## Parameters
<table><thead><tr><th>
Parameter
</th><th>
Type
</th><th>
Description
</th></tr></thead>
<tbody><tr><td markdown="1">
bindingKey
</td><td markdown="1">
string \| [BindingKey](./context.bindingkey.md)<!-- --><unknown>
</td><td markdown="1">
_(Optional)_ Binding key
</td></tr>
<tr><td markdown="1">
metadata
</td><td markdown="1">
[InjectBindingMetadata](./context.injectbindingmetadata.md)
</td><td markdown="1">
_(Optional)_ Metadata for the injection
</td></tr>
</tbody></table>
**Returns:**
(target: Object, member: string \| undefined, 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(() => {...});
}
}
```