LoginSignup
1
1

More than 5 years have passed since last update.

angular-cliのserviceの呼び出し方

Posted at

angular/cliで生成するserviceテンプレートの使い方の備忘録

生成

ng g service manage/unit-util

app/services/manage/unit-util.service.ts (と.spec)が生成される

Componentに注入

import { UnitUtilService } from '../../services/manage/unit-util.service';

.service まで忘れずに記述する。

Providerに登録

@Component({
  selector: 'app-dere-list',
  templateUrl: './dere-list.component.html',
  styleUrls: ['./dere-list.component.css'],
  providers: [UnitUtilService]                 // この行を追記
})
export class DereListComponent ...

これを書かないと No provider for {service_name} と怒られる。

コンストラクタでインスタンスを生成

  constructor(private db: AngularFireDatabase, private unitUtil: UnitUtilService) {

呼び出し

unitUtil.getFoo();

実ソース

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1