最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Angular5中狀態(tài)管理的實(shí)現(xiàn)

 更新時(shí)間:2018年09月03日 13:46:12   作者:weistar103  
這篇文章主要介紹了Angular5中狀態(tài)管理的實(shí)現(xiàn),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧

前面學(xué)習(xí)了vue,react 都有狀態(tài)管理,如vue中的vuex是全局狀態(tài)管理,在任何組件里都可以引用狀態(tài)管理中的數(shù)據(jù),同樣,react中的redux和mbox也是,但遇到angular5卻不知道了。

一年前使用過angular1.x做過項(xiàng)目,那時(shí)全局狀態(tài)可以使用$rootscope,也可以使用服務(wù)Service實(shí)現(xiàn),下面就用Service方式在angular5中實(shí)現(xiàn)下吧

先定義狀態(tài)管理對(duì)象,需要存什么數(shù)據(jù),自己定義

export class UserInfo {
 public userInfo: boolean;
 constructor(){
   this.userInfo = true; //設(shè)置全局的控制導(dǎo)航是否顯示
 }
}

然后定義Service,如下

import { Injectable} from '@angular/core';
import { Headers, Http } from '@angular/http';
import { UserInfo } from './user-info.model';

@Injectable() //注入服務(wù)
export class ListsService{
 private userInfo;
 constructor(private http: Http) { 
  this.userInfo = new UserInfo();
 }

 //設(shè)置路由顯示的狀態(tài)
 setUserInfo(v) {
  this.userInfo.userInfo = v;
 }
 //獲取路由顯示的狀態(tài)
 getUserInfo() {
  return this.userInfo;
 }
}

配置了service一定要在ngmodule中導(dǎo)入,這樣才能在此module中有效

import { BrowserModule } from '@angular/platform-browser'; 
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';  
import { HttpModule }  from '@angular/http';

import { AppComponent } from './app.component'; 

import { AppRouterModule } from './router.module'; 
import { ViewComponent } from './view.component';
import { ListComponent } from './list.component';
import { OtherComponent } from './other.component';
import { DetailComponent } from './detail.component'; 
import { ListsService } from './app.service';

@NgModule({
 declarations: [
  AppComponent,
  DetailComponent,
  ViewComponent,
  ListComponent,
  OtherComponent
 ],
 imports: [ 
  BrowserModule,
  FormsModule ,
  AppRouterModule,
  HttpModule
 ],
 providers: [ListsService], 
 bootstrap: [AppComponent] 
})
export class AppModule { } 

然后就可以在component中使用了

@Component({
 selector: 'app-root',
 template: `
 <div >
   <div class="lists" *ngIf='userInfo.userInfo'>
    <a routerLink="/view" routerLinkActive ="active">特價(jià)展示</a>
    <a routerLink="/list" routerLinkActive ="active">列表展示</a>
  </div>
  <router-outlet></router-outlet>
 </div>
 `,
 styles:[`
 .lists a{
  padding:0 10px;
 }
 .active{
  color: #f60;
 }
 `]
})
export class AppComponent {
 private userInfo;
 constructor(private listsService: ListsService) { 
   this.userInfo= this.listsService.getUserInfo();
 }
}

在詳情頁中通過改變狀態(tài)來改變頁面

@Component({
 selector: 'app-detail',
 template: `
  <div>
   詳情頁{{id}}
   <button (click)="goBack()">返回</button>
  </div>

 `,
})
export class DetailComponent {
 private userInfo;
 constructor(
  private route: ActivatedRoute,
  private location: Location,
  private listsService: ListsService
 ) {
  this.userInfo= this.listsService.setUserInfo(false);
 }
 goBack(): void {
  this.location.back();
 }
 //組件銷毀時(shí)執(zhí)行
 ngOnDestroy():void{
  this.userInfo= this.listsService.setUserInfo(true);
 }
}

好了,這樣就ok了。以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 深究AngularJS如何獲取input的焦點(diǎn)(自定義指令)

    深究AngularJS如何獲取input的焦點(diǎn)(自定義指令)

    本篇文章主要介紹了AngularJS如何獲取input的焦點(diǎn)(自定義指令),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-06-06
  • 基于Angular.js實(shí)現(xiàn)的觸摸滑動(dòng)動(dòng)畫實(shí)例代碼

    基于Angular.js實(shí)現(xiàn)的觸摸滑動(dòng)動(dòng)畫實(shí)例代碼

    這篇文章主要介紹了基于Angular.js實(shí)現(xiàn)的觸摸滑動(dòng)動(dòng)畫實(shí)例代碼,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下
    2017-02-02
  • 詳解Angular4 路由設(shè)置相關(guān)

    詳解Angular4 路由設(shè)置相關(guān)

    本篇文章主要介紹了詳解Angular4 路由設(shè)置相關(guān),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-08-08
  • Angular5集成eventbus的示例代碼

    Angular5集成eventbus的示例代碼

    這篇文章主要介紹了Angular5集成eventbus的示例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-07-07
  • Angular.js中處理頁面閃爍的方法詳解

    Angular.js中處理頁面閃爍的方法詳解

    我們在應(yīng)用的頁面或者組件需要加載數(shù)據(jù)時(shí),瀏覽器和angular渲染頁面都需要消耗一定的時(shí)間。這里的間隔可能很小,甚至讓人感覺不到區(qū)別;但也可能很長,這樣會(huì)導(dǎo)致讓我們的用戶看到了沒有被渲染過的頁面。本文將介紹Angular.js中處理頁面閃爍的方法。
    2017-03-03
  • 詳解Angular 4.x NgTemplateOutlet

    詳解Angular 4.x NgTemplateOutlet

    這篇文章主要介紹了詳解Angular 4.x NgTemplateOutlet,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-05-05
  • Angular 2父子組件數(shù)據(jù)傳遞之@ViewChild獲取子組件詳解

    Angular 2父子組件數(shù)據(jù)傳遞之@ViewChild獲取子組件詳解

    這篇文章主要給大家介紹了關(guān)于Angular 2父子組件數(shù)據(jù)傳遞之@ViewChild獲取子組件的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家具有一定參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起看看吧。
    2017-07-07
  • 使用Chrome瀏覽器調(diào)試AngularJS應(yīng)用的方法

    使用Chrome瀏覽器調(diào)試AngularJS應(yīng)用的方法

    這篇文章主要介紹了使用Chrome瀏覽器調(diào)試AngularJS應(yīng)用的方法,AngularJS是非常熱門的JavaScript庫,需要的朋友可以參考下
    2015-06-06
  • AngularJS實(shí)現(xiàn)給動(dòng)態(tài)生成的元素綁定事件的方法

    AngularJS實(shí)現(xiàn)給動(dòng)態(tài)生成的元素綁定事件的方法

    這篇文章主要介紹了AngularJS實(shí)現(xiàn)給動(dòng)態(tài)生成的元素綁定事件的方法,結(jié)合實(shí)例形式分析了AngularJS動(dòng)態(tài)生成元素與事件綁定相關(guān)操作技巧,需要的朋友可以參考下
    2016-12-12
  • 詳解AngularJS控制器的使用

    詳解AngularJS控制器的使用

    這篇文章主要為大家詳細(xì)介紹了AngularJS控制器的使用方法,感興趣的小伙伴們可以參考一下
    2016-03-03

最新評(píng)論

六安市| 杂多县| 宁阳县| 巍山| 陆川县| 琼海市| 石城县| 天津市| 东莞市| 宁晋县| 犍为县| 太和县| 嘉黎县| 曲阳县| 纳雍县| 通化市| 德昌县| 库尔勒市| 五家渠市| 土默特右旗| 汨罗市| 礼泉县| 吐鲁番市| 尼木县| 丽江市| 广河县| 山阳县| 科技| 台北县| 二连浩特市| 昂仁县| 金塔县| 尼玛县| 象山县| 河曲县| 喀喇| 乳山市| 镇宁| 丰原市| 图片| 新巴尔虎右旗|