欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Angular5中狀態(tài)管理的實現

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

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

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

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

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

然后定義Service,如下

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

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

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

配置了service一定要在ngmodule中導入,這樣才能在此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">特價展示</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();
 }
 //組件銷毀時執(zhí)行
 ngOnDestroy():void{
  this.userInfo= this.listsService.setUserInfo(true);
 }
}

好了,這樣就ok了。以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • 詳解Angular 4.x 動態(tài)創(chuàng)建組件

    詳解Angular 4.x 動態(tài)創(chuàng)建組件

    本篇文章主要介紹了詳解Angular 4.x 動態(tài)創(chuàng)建組件,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-04-04
  • Angular2利用組件與指令實現圖片輪播組件

    Angular2利用組件與指令實現圖片輪播組件

    這篇文章主要給大家介紹了Angular2中組件與指令的一個小實踐,利用組件和指令實現一個圖片輪播組件的相關資料,文中給出了詳細的介紹和示例代碼,需要的朋友可以參考學習,下面來一起看看吧。
    2017-03-03
  • AngularJS遍歷獲取數組元素的方法示例

    AngularJS遍歷獲取數組元素的方法示例

    這篇文章主要介紹了AngularJS遍歷獲取數組元素的方法,涉及AngularJS簡單的數組遍歷及元素獲取相關操作技巧,需要的朋友可以參考下
    2017-11-11
  • AngularJS使用ng-repeat遍歷二維數組元素的方法詳解

    AngularJS使用ng-repeat遍歷二維數組元素的方法詳解

    這篇文章主要介紹了AngularJS使用ng-repeat遍歷二維數組元素的方法,結合實例形式分析了AngularJS二維數組元素遍歷的相關操作技巧,需要的朋友可以參考下
    2017-11-11
  • Angular4.x通過路由守衛(wèi)進行路由重定向實現根據條件跳轉到相應的頁面(推薦)

    Angular4.x通過路由守衛(wèi)進行路由重定向實現根據條件跳轉到相應的頁面(推薦)

    這篇文章主要介紹了Angular4.x通過路由守衛(wèi)進行路由重定向,實現根據條件跳轉到相應的頁面,這個功能在網上商城項目上經常會用到,下面小編給大家?guī)砹私鉀Q方法一起看看吧
    2018-05-05
  • Angular.js中數組操作的方法教程

    Angular.js中數組操作的方法教程

    AngularJS是google在維護,其在國外已經十分火熱,可是國內的使用情況卻有不小的差距,參考文獻/網絡文章也很匱乏。下面這篇文章主要給大家介紹了關于Angular.js中數組操作的相關資料,需要的朋友可以參考借鑒,下面來一起看看吧。
    2017-07-07
  • AngularJS ng-repeat數組有重復值的解決方法

    AngularJS ng-repeat數組有重復值的解決方法

    不知道大家是否遇到過這個問題,在當Angular.JS ng-repeat數組中有重復項時,系統(tǒng)就會拋出異常,這是該怎么做?本文通過示例代碼介紹了詳細的解決方法,有需要的朋友們可以參考借鑒,下面來一起看看吧。
    2016-10-10
  • 動態(tài)創(chuàng)建Angular組件實現popup彈窗功能

    動態(tài)創(chuàng)建Angular組件實現popup彈窗功能

    這篇文章主要介紹了動態(tài)創(chuàng)建angular組件實現popup彈窗,需要的朋友可以參考下
    2017-09-09
  • 詳解利用Angular實現多團隊模塊化SPA開發(fā)框架

    詳解利用Angular實現多團隊模塊化SPA開發(fā)框架

    本篇文章主要介紹了詳解利用Angular實現多團隊模塊化SPA開發(fā)框架,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-11-11
  • Angular5中狀態(tài)管理的實現

    Angular5中狀態(tài)管理的實現

    這篇文章主要介紹了Angular5中狀態(tài)管理的實現,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-09-09

最新評論