@loopback/docs
Version:
Documentation for LoopBack 4
75 lines (54 loc) • 1.79 kB
Markdown
---
lang: en
title: 'API docs: core.application.service'
keywords: LoopBack 4.0, LoopBack 4
sidebar: lb4_sidebar
editurl: https://github.com/strongloop/loopback-next/tree/master/packages/core
permalink: /doc/en/lb4/apidocs.core.application.service.html
---
<!-- Do not edit this file. It is automatically generated by API Documenter. -->
[Home](./index.md) > [/core](./core.md) > [Application](./core.application.md) > [service](./core.application.service.md)
## Application.service() method
Add a service to this application.
<b>Signature:</b>
```typescript
service<S>(cls: Constructor<S> | Constructor<Provider<S>>, name?: string | ServiceOptions): Binding<S>;
```
## Parameters
| Parameter | Type | Description |
| --- | --- | --- |
| cls | <code>Constructor<S> | Constructor<Provider<S>></code> | The service or provider class |
| name | <code>string | ServiceOptions</code> | |
<b>Returns:</b>
`Binding<S>`
## Example
```ts
// Define a class to be bound via ctx.toClass()
export class LogService {
log(msg: string) {
console.log(msg);
}
}
// Define a class to be bound via ctx.toProvider()
import {v4 as uuidv4} from 'uuid';
export class UuidProvider implements Provider<string> {
value() {
return uuidv4();
}
}
// Register the local services
app.service(LogService);
app.service(UuidProvider, 'uuid');
export class MyController {
constructor(
private uuid: string,
private log: LogService,
) {
}
greet(name: string) {
this.log(`Greet request ${this.uuid} received: ${name}`);
return `${this.uuid}: ${name}`;
}
}
```