// app.controller.ts
import { Controller, Get } from '@nestjs/common';
import { AppService } from './app.service';
@Controller()
export class AppController {
// Public member variable
public message: string;
// Private member variable
private service: AppService;
// Constructor with dependency injection
constructor(private readonly appService: AppService) {
this.message = 'Hello World'; // Initialize public member
this.service = appService; // Initialize private member
}
@Get()
async getHello(): Promise<string> {
const data = await this.service.getData(); // Access private member
return `${this.message} - ${data}`;
}
}
// app.service.ts
import { Injectable } from '@nestjs/common';
@Injectable()
export class AppService {
async getData(): Promise<string> {
return 'Data from the service';
}
}
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交