Angular 4.X開發(fā)實(shí)踐中的踩坑小結(jié)
本文主要給大家分享了關(guān)于Angular 4.X開發(fā)中與到的一些踩坑經(jīng)驗(yàn),分享出來供大家參考學(xué)習(xí),下面來一起看看詳細(xì)的介紹:
一、使用ngIf或者ngSwitch出錯(cuò)
在html文件中使用ngIf或者ngSwitch時(shí),會解析出錯(cuò),錯(cuò)誤提示如下:
Error: Template parse errors: Can't bind to 'ngSwitch' since it isn't a known property of 'div'.
這個(gè)是因?yàn)闆]有在此Component所在的Module中導(dǎo)入CommonModule,雖然你可能在AppModule中導(dǎo)入過了,但是還是需要導(dǎo)入一次,代碼如下:
import { CommonModule } from '@angular/common';
@NgModule(
{
declarations: [ ],
imports: [
CommonModule
],
exports: [ ],
providers: [ ]
}
)
export class MainModule { }
二、多級依賴注入器
Angular 4.X擁有多級依賴注入系統(tǒng),在一個(gè)注入器的范圍內(nèi),依賴都是單例的。它使用冒泡機(jī)制,當(dāng)一個(gè)組件申請獲得一個(gè)依賴時(shí),Angular 先嘗試用該組件自己的注入器來滿足它。 如果該組件的注入器沒有找到對應(yīng)的提供商,它就把這個(gè)申請轉(zhuǎn)給它父組件的注入器來處理。 如果那個(gè)注入器也無法滿足這個(gè)申請,它就繼續(xù)轉(zhuǎn)給它的父組件的注入器。
舉個(gè)例子,從登錄頁點(diǎn)擊登錄按鈕進(jìn)入主頁,LoginComponent和MainComponent都注入了LoginService。
登錄:
//login.service.ts
// 這個(gè)是登錄服務(wù)
import { Injectable } from '@angular/core';
@Injectable()
export class LoginService {
isLoggedIn: boolean = false;
login(){
this.isLoggedIn=true;
}
}
// login.component.ts
//登錄界面,只有一個(gè)登錄按鈕,點(diǎn)擊后登錄會把LoginService中的isLoggedIn變?yōu)閠rue
import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { LoginService } from '../login/login.service';
@Component({
selector: 'app-login',
template:`<button (click)=login()>Login</button>`,
providers: [LoginService]
})
export class LoginComponent implements OnInit {
constructor(private router: Router, private loginService: LoginService) { }
login() {
this.loginService.login();
console.log(this.loginService.isLoggedIn); //結(jié)果為true
this.router.navigate(['/main']);
}
}
// main.component.ts
// 這個(gè)是登陸后的主界面
import { Component} from '@angular/core';
import { LoginService } from '../login/login.service';
@Component({
selector: 'app-main',
template: `<h1>HOME</h1>`,
providers: [LoginService]
})
export class MainComponent implements OnInit {
private userType: string ;
constructor(private loginService: LoginService) {
console.log(this.loginService.isLoginIn); //結(jié)果為false
}
}
從上面的例子可以看出來,在不同的地方注入同樣的Service,但是會使用不同的實(shí)例,所以會導(dǎo)致結(jié)果可能不同,需要注意。
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。
相關(guān)文章
angular.js4使用 RxJS 處理多個(gè) Http 請求
本篇文章主要介紹了angular.js使用 RxJS 處理多個(gè) Http 請求,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-09-09
Angular 2應(yīng)用的8個(gè)主要構(gòu)造塊有哪些
這篇文章主要為大家詳細(xì)介紹了Angular 2應(yīng)用的8個(gè)主要構(gòu)造塊,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-10-10
angular2+node.js express打包部署的實(shí)戰(zhàn)
本篇文章主要介紹了angular2+node.js express打包部署的實(shí)戰(zhàn),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-07-07
AngularJS中controller控制器繼承的使用方法
這篇文章主要介紹了AngularJS中controller控制器繼承的使用方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-11-11
深究AngularJS如何獲取input的焦點(diǎn)(自定義指令)
本篇文章主要介紹了AngularJS如何獲取input的焦點(diǎn)(自定義指令),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06
AngularJS實(shí)踐之使用NgModelController進(jìn)行數(shù)據(jù)綁定
大家都知道AngularJS中的指令是其尤為復(fù)雜的一個(gè)部分,但是這也是其比較好玩的地方。這篇文章我們就來說一說如何在我們自定義的指令中,利用ngModel的controller來做雙向數(shù)據(jù)綁定,本文對大家學(xué)習(xí)AngularJS具有一定的參考借鑒價(jià)值,有需要的朋友們可以參考借鑒。2016-10-10

