Angular使用Service

      網(wǎng)友投稿 1009 2022-05-30

      Service不僅僅是數(shù)據(jù)獲取模塊,同時(shí)也是非常簡單容易的數(shù)據(jù)共享模塊。

      下面是是一個(gè)Service的定義:

      board.service.ts

      import?{?Injectable?}?from?'@angular/core';

      import?{?HttpClient?}?from?'@angular/common/http';

      @Injectable({

      providedIn:?'root'

      })

      export?class?BoardService?{

      constructor(private?http:?HttpClient)?{?}

      backlog?=?[];

      todo?=?[

      'Task?1',

      在Angular中使用Service

      'Task?2',

      'Task?3'

      ];

      inProgess?=?[

      'Task?4',

      'Task?5',

      ];

      done?=?[

      'Task?6',

      'Task?7',

      'Task?8',

      'Task?9'

      ];

      boards?=?[

      {id:?1,?name:?"Board?1"},

      {id:?2,?name:?"Board?2"},

      {id:?3,?name:?"Board?3"},

      ];

      checkInvalidName(name:?string):?boolean?{

      if(this.todo.findIndex(x=>x.toLowerCase()?==?name.toLowerCase())?!=?-1){

      return?true;

      }

      if(this.inProgess.findIndex(x=>x.toLowerCase()?==?name.toLowerCase())?!=?-1){

      return?true;

      }

      if(this.done.findIndex(x=>x.toLowerCase()?==?name.toLowerCase())?!=?-1){

      return?true;

      }

      return?false;

      }

      checkInvalidBoardName(name:?string):?boolean?{

      if(this.boards.findIndex(x=>x.name.toLowerCase()?==?name.toLowerCase())?!=?-1){

      return?true;

      }

      return?false;

      }

      addBoard(name:?string)?{

      return??this.http.post('/api/board',?{?name:?name?});

      }

      addTask(boardId:?number,?name:?string)?{

      return??this.http.post('/api/task',?{?boardId:?boardId,?name:?name?});

      }

      getBoards()?{

      return??this.http.get('/api/board');

      }

      }

      使用例子:

      export?class?BoardComponent?extends?BaseComponent?implements?OnInit?{

      constructor(public?dialog:?MatDialog,?private?boardService:?BoardService)?{

      super();

      }

      ngOnInit():?void?{

      this.backlog?=?this.boardService.backlog;

      this.todo?=?this.boardService.todo;

      this.inProgess?=?this.boardService.inProgess;

      this.done?=?this.boardService.done;

      this.subspritions.push(this.boardService.getBoards().subscribe(x=>{

      this.boards?=?x;

      if(this.boards.length?>?0)?{

      this.currentBoardId?=?this.boards[0].id;

      this.currentBoard?=?this.boards[0];

      }

      }));

      }?...

      可以在多個(gè)組件中使用:

      export?class?AddBoardComponent?{

      constructor(@Inject(MAT_DIALOG_DATA)?public?data:?any,?public?dialogRef:?MatDialogRef,private?boardService:?BoardService)?{

      }

      private?isInvalidName?=?false;

      closeDialogOk()?{

      //?check?unique

      this.isInvalidName?=?this.boardService.checkInvalidBoardName(this.data.name);

      if(this.isInvalidName)?{

      return;

      }

      this.dialogRef.close(this.data.name);

      }

      ...

      Angular

      版權(quán)聲明:本文內(nèi)容由網(wǎng)絡(luò)用戶投稿,版權(quán)歸原作者所有,本站不擁有其著作權(quán),亦不承擔(dān)相應(yīng)法律責(zé)任。如果您發(fā)現(xiàn)本站中有涉嫌抄襲或描述失實(shí)的內(nèi)容,請聯(lián)系我們jiasou666@gmail.com 處理,核實(shí)后本網(wǎng)站將在24小時(shí)內(nèi)刪除侵權(quán)內(nèi)容。

      上一篇:Connected Car一款適合家庭客戶使用的車聯(lián)網(wǎng)產(chǎn)品
      下一篇:React Native 組件生命周期
      相關(guān)文章
      国产精品亚洲二区在线| 亚洲午夜精品久久久久久app | 亚洲偷偷自拍高清| 亚洲毛片在线免费观看| 久久久久亚洲精品天堂| 久久精品国产亚洲av高清漫画| 亚洲AV日韩AV天堂久久| 亚洲国产精品无码久久一线| 亚洲综合伊人久久综合| 国产亚洲精品不卡在线| 亚洲综合伊人久久综合| 国产V亚洲V天堂A无码| 亚洲VA中文字幕无码毛片 | 亚洲精品夜夜夜妓女网| 亚洲精品中文字幕无码蜜桃| 亚洲区小说区激情区图片区 | 亚洲男人天堂2022| 中文字幕亚洲码在线| 亚洲综合小说另类图片动图| 亚洲高清有码中文字| 亚洲色欲啪啪久久WWW综合网| 亚洲精品国产av成拍色拍| 亚洲精品无码一区二区| 99亚洲乱人伦aⅴ精品| 国产精品亚洲精品日韩动图| 亚洲高清国产拍精品青青草原| 亚洲精品99久久久久中文字幕 | 亚洲精品和日本精品| 亚洲色无码专区在线观看| 亚洲无线观看国产精品| 亚洲AV无码国产丝袜在线观看| 亚洲精品私拍国产福利在线| 亚洲毛片免费视频| 亚洲免费福利在线视频| 337P日本欧洲亚洲大胆精品| 亚洲日本韩国在线| 国产AV无码专区亚洲A∨毛片| 久久久无码精品亚洲日韩蜜臀浪潮 | 亚洲AV无码一区二区三区牲色 | 国产亚洲蜜芽精品久久| 国产精品亚洲二区在线观看|