中文亚洲精品无码_熟女乱子伦免费_人人超碰人人爱国产_亚洲熟妇女综合网

當(dāng)前位置: 首頁 > news >正文

網(wǎng)站icp備案認(rèn)證怎么做世界足球排名最新

網(wǎng)站icp備案認(rèn)證怎么做,世界足球排名最新,java與php哪個(gè)做網(wǎng)站簡單,關(guān)于加強(qiáng)網(wǎng)站建設(shè)的意見在Angular中有很多方式可以將服務(wù)類注冊(cè)到注入器中: Injectable 元數(shù)據(jù)中的providedIn屬性 NgModule 元數(shù)據(jù)中的 providers屬性 Component 元數(shù)據(jù)中的 providers屬性 創(chuàng)建一個(gè)文件名叫名 hero.service.ts叫 hero 的服務(wù) hero.service.ts import { Injectable } from angular…

在Angular中有很多方式可以將服務(wù)類注冊(cè)到注入器中:

@Injectable 元數(shù)據(jù)中的providedIn屬性
@NgModule 元數(shù)據(jù)中的 providers屬性
@Component 元數(shù)據(jù)中的 providers屬性
創(chuàng)建一個(gè)文件名叫名 hero.service.ts叫 hero 的服務(wù)
?hero.service.ts

import { Injectable } from '@angular/core';@Injectable({providedIn: 'root',
})
export class HeroService {constructor() { }// 新增加setName方法setName(name:string):string{return `姓名:${name}`;}}


1.@Injectable 元數(shù)據(jù)中的providedIn屬性

providedIn: 'root' 告訴 Angular在根注入器中注冊(cè)這個(gè)服務(wù),這也是使用CLI生成服務(wù)時(shí)默認(rèn)的方式.
這種方式注冊(cè),不需要再@NgModule裝飾器中寫providers,而且在代碼編譯打包時(shí),可以執(zhí)行搖樹優(yōu)化,會(huì)移除所有沒在應(yīng)用中使用過的服務(wù)。推薦使用此種方式注冊(cè)服務(wù)

使用providedIn的話,后面直接在項(xiàng)目中使用了。

使用:heroes.component.ts

import { Component, OnInit } from '@angular/core';
import { HeroService } from '../hero.service'@Component({selector: 'app-heroes',templateUrl: './heroes.component.html',styleUrls: ['./heroes.component.css']
})
export class HeroesComponent implements OnInit {constructor(private heroService:HeroService) { }ngOnInit() {this.heroService.setName('張三');}
}


2.@NgModule 元數(shù)據(jù)中的 providers屬性

改寫 hero.service.ts里面的@Injectable,如下

import { Injectable } from '@angular/core';@Injectable() // 刪掉了 {providedIn: 'root'}
export class HeroService {...}


?xx.module.ts , 例如app.module.ts

...@NgModule({providers: [HeroService,// { provide: HeroService, useValue: HeroService }],
})...


然后就可以在使用拉,使用方法,同1 heroes.component.ts文件

3.@Component 元數(shù)據(jù)中的 providers屬性

?hero.service.ts里面的@Injectable,刪掉 {providedIn: 'root'},同2 hero.service.ts文件

改寫heroes.component.ts

import { Component, OnInit } from '@angular/core';
import { HeroService } from '../hero.service'@Component({selector: 'app-heroes',templateUrl: './heroes.component.html',styleUrls: ['./heroes.component.css'],providers: [HeroService] // 新增 providers: [HeroService]
})
export class HeroesComponent implements OnInit {constructor(private heroService:HeroService) { }ngOnInit() {this.heroService.setName('張三');}
}


?

三種用法總結(jié):

@Injectable 元數(shù)據(jù)中的providedIn屬性 ?

//service.ts
@Injectable({providedIn:'root'})//component.ts
constructor(private heroService:HeroService) { }


@NgModule 元數(shù)據(jù)中的 providers屬性

// service.ts
@Injectable()?//module.ts
@NgModule({providers: [HeroService ]
})


@Component 元數(shù)據(jù)中的 providers屬性

// service.ts
@Injectable()?// component.ts
@Component({...selector: 'app-heroes',providers: [ HeroService ]
})


原文鏈接:https://blog.csdn.net/sllailcp/article/details/102548144

http://www.risenshineclean.com/news/62106.html

相關(guān)文章:

  • 做畢業(yè)網(wǎng)站的周記西安seo優(yōu)化公司
  • 個(gè)人網(wǎng)站引導(dǎo)頁源碼友情鏈接怎么購買
  • 定制網(wǎng)站制作公司怎么樣營銷軟文范例大全300字
  • 做視頻網(wǎng)站違法么色盲
  • 網(wǎng)站建設(shè)都有什么功能企業(yè)網(wǎng)站seo托管怎么做
  • 常州建站網(wǎng)站模板seo百度刷排名
  • 商貿(mào)公司的網(wǎng)站建設(shè)上海高端網(wǎng)站定制
  • jsp網(wǎng)站開發(fā)的兩種模式企業(yè)營銷策劃包括哪些內(nèi)容
  • 阿里云個(gè)人網(wǎng)站備案做淘客網(wǎng)絡(luò)營銷推廣方案怎么寫
  • 網(wǎng)站建設(shè)畢業(yè)答辯ppt模板公司網(wǎng)址有哪些
  • 做黑網(wǎng)站嗎b站24小時(shí)自助下單平臺(tái)網(wǎng)站
  • 制作網(wǎng)頁可以使用的網(wǎng)頁元素包括seo查詢友情鏈接
  • 怎么刪除寶塔其他版本建設(shè)的網(wǎng)站seo權(quán)威入門教程
  • 長沙做網(wǎng)站湖南微聯(lián)訊點(diǎn)靠譜百度廣告搜索推廣
  • 網(wǎng)站域名價(jià)值查詢獨(dú)立站推廣
  • 北京高端品牌網(wǎng)站建設(shè)寧波外貿(mào)網(wǎng)站推廣優(yōu)化
  • 做網(wǎng)站可不可以模仿鄭州網(wǎng)絡(luò)推廣專業(yè)公司
  • 培訓(xùn)教育行業(yè)網(wǎng)站建設(shè)方案app推廣拉新一手渠道代理
  • 普陀集團(tuán)網(wǎng)站建設(shè)網(wǎng)站注冊(cè)
  • 站長工具大全企業(yè)網(wǎng)站的作用
  • 教育類網(wǎng)站怎么做優(yōu)化搜索引擎優(yōu)化排名培訓(xùn)
  • bbs網(wǎng)站模板下載網(wǎng)站運(yùn)營策劃書范文
  • 天寧常州做網(wǎng)站長沙市seo百度關(guān)鍵詞
  • 新昌網(wǎng)站制作網(wǎng)站投放廣告費(fèi)用
  • 注冊(cè)功能網(wǎng)站建設(shè)icp備案查詢官網(wǎng)
  • 外貿(mào)網(wǎng)站建設(shè)公司渠道安卓系統(tǒng)優(yōu)化軟件
  • 營銷型網(wǎng)站建設(shè)要懂代碼嗎西安網(wǎng)站制作工作室
  • 房地產(chǎn)公司 網(wǎng)站建設(shè)seo網(wǎng)絡(luò)優(yōu)化師
  • 做網(wǎng)站用sql和mysql域名批量查詢
  • 獨(dú)立外貿(mào)網(wǎng)站建設(shè)電商運(yùn)營自學(xué)全套教程