2017-08-13 39 views

回答

0

@Injectable只是一個標記,告訴角引擎該類可以由注入器創建。在運行時的角度講述到噴油器讀取所有@Injectable類和實例化它們,使它們可被注入到引用它們的類。

例如,假設有一個名爲UserService的角度服務,並且您需要在名爲RegistrationComponent的組件中使用該服務。

@Injectable() 
export class UserService { 

    saverUser(User user) 
    ..... 
    } 

然後在RegistrationComponent構造聲明引用到UserService輸入參數,它告訴給角即UserService應注射到RegistrationComponent,當然先前的@Injectable標記應在UserService

RegistrationComponent聲明。 TS

export class RegistrationComponent 

    constructor(private userService: UserService) { } 

在Spring上下文的@Component起着比類似的工作當然,在實施過程中他們之間有很多不同之處,但他們都起着類似的作用。 @Component是一個註釋,告訴與Spring的一些具體的類必須被視爲自動檢測的候選,並且該類能生活在Spring容器。生活在Spring容器中的組件(bean)可以注入其他類。

@Autowired是不一樣的@Component@Autowired意味着一個特定類的成員應當提供或由Spring DI容器注入。

欲瞭解更多信息,請參見以下鏈接:

Angular Dependency Injection

@Autowired Spring Documentation

@Component Spring Documentation