[Angular 2] Understanding @Injectable
In order to resolve a dependency, Angular’s DI uses type annotations. To make sure these types are preserved when transpiled to ES5, TypeScript emits metadata. In this lesson we’ll explore how the @Injectable decorator ensures metadata generation for services that have their own dependencies.
For exmaple, the TodoService looks like this:
export class TodoService {
todos = [
{id: 0, name: "eat"},
{id: 1, name: "sleep"},
{id: 2, name: "running"},
]; getTodos(){
return this.todos;
}
}
Now we want to inject LoggerProvider into TodoService:
import {LoggerProvider} from './LoggerProvider'; export class TodoService {
todos = [
{id: 0, name: "eat"},
{id: 1, name: "sleep"},
{id: 2, name: "running"},
]; constructor(private logger: LoggerProvider){ } getTodos(){
this.logger.debug('Items', this.todos);
return this.todos;
}
}
If we want to the code, it will show errors:
Cannot resolve paramster in TodoService
The problem for this is because, when TypeScript code compile to ES5 code, 'LoggerProivder' is injected into the TodoService to decreator (Angular creates it).
We must provider decreator to the TodoService in order to let this class know how to handle DI. So to solve the problem, we just need to add '@Injectable' whcih provided by Angular:
import {LoggerProvider} from './LoggerProvider';
import {Injectable} from '@angular/core'; @Injectable
export class TodoService {
todos = [
{id: 0, name: "eat"},
{id: 1, name: "sleep"},
{id: 2, name: "running"},
]; constructor(private logger: LoggerProvider){ } getTodos(){
this.logger.debug('Items', this.todos);
return this.todos;
}
}
[Angular 2] Understanding @Injectable的更多相关文章
- [Angular 2] Understanding OpaqueToken
When using provider string tokens, there’s a chance they collide with other third-party tokens. Angu ...
- [Angular 2] Understanding Pure & Impure pipe
First, how to use a build in pipe: <div class="pipe-example"> <label>Uppercase ...
- 来自 Thoughtram 的 Angular 2 系列资料
Angular 2 已经正式 Release 了,Thoughtram 已经发布了一系列的文档,对 Angular 2 的各个方面进行深入的阐释和说明. 我计划逐渐将这个系列翻译出来,以便对大家学习 ...
- [Angular 2] Value Providers & @Inject
Dependecies aren’t always objects created by classes or factory functions. Sometimes, all we really ...
- [Angular 2] Handle Reactive Async opreations in Service
When you use ngrx/store and you want to fire a service request. When it sucessfully return the respo ...
- 使用angular的HttpClient搭配rxjs
一.原Http使用总结 使用方法 在根模块或核心模块引入HttpModule 即在AppModule或CoreModule中引入HttpModule: import { HttpModule } fr ...
- Angular路由——路由守卫
一.路由守卫 当用户满足一定条件才被允许进入或者离开一个路由. 路由守卫场景: 只有当用户登录并拥有某些权限的时候才能进入某些路由. 一个由多个表单组成的向导,例如注册流程,用户只有在当前路由的组件中 ...
- 【响应式编程的思维艺术】 (5)Angular中Rxjs的应用示例
目录 一. 划重点 二. Angular应用中的Http请求 三. 使用Rxjs构建Http请求结果的处理管道 3.1 基本示例 3.2 常见的操作符 四. 冷热Observable的两种典型场景 4 ...
- angular default project (angular.json的解读)
Change the default Angular project Understanding it's purpose and limits Klaus KazlauskasFollow Nov ...
随机推荐
- longest common str
#include <vector> #include <iostream> #include <string> using namespace std; int l ...
- TestNG传参的几种方式
1. 通过parameter传参 java代码部分: import org.testng.annotations.Parameters; import org.testng.annotations.T ...
- QT4.7.4-vs2008和vs2010的安装并编写测试程序
QT的安装着实费了我好大的劲,之前试过QT5.1.0与VS2010的安装,但是因为设置的地方太多,程序运行总是不成功,所以最后选用QT4.7.4和VS2008与VS2010来编写程序.写这篇文章来总结 ...
- WordPress的SEO技术
原文:http://blog.wpjam.com/article/wordpress-seo/ 文章目录[隐藏] 内容为王 页面优化 标题 链接(URL) Meta 标签 语义化 H1 H2 H3 等 ...
- 用VMware 8安装Ubuntu 12.04详细过程(图解)
转载 http://www.cnblogs.com/achillesyang/archive/2012/06/21/2557152.html
- Java每日一则-001
Java中类名与文件名的关系 1.Java保存的文件名必须与类名一致: 2.如果文件中只有一个类,文件名必须与类名一致: 3.一个Java文件中只能有一个public类: 4.如果文件中不止一个类,文 ...
- Chapter 1 初探Caffe
首先下载windows下源码: Microsoft 官方:GitHub - Microsoft/caffe: Caffe on both Linux and Windows 官方源码使用Visual ...
- Android 不同应用通过SharedPreference实现共享数据
Android不同应用之间数据的共享有许多方式,但是我觉得还是使用sharedPreference比较简单和轻量级.如果程序B想要访问程序A的sharedPreference可以通过下面的语句来实现: ...
- Windows下GNU之gcc体验方法
Windows 现在在Windows下开发C/C++程序一般都是用微软的编译器,当年的Borland已经成为传说.但是如果你不想付钱的话,也可以考虑Windows下的GCC. 在Windows下体验G ...
- work_8
1.把程序编译通过, 跑起来. 读懂程序,在你觉得比较难懂的地方加上一些注释,这样大家就能比较容易地了解这些程序在干什么. 把正确的 playPrev(GoMove) 的方法给实现了. 如果大家不会下 ...