2017-07-06 111 views
1

Im使用Angular 4和Firebase進行身份驗證。我收到錯誤「Property'auth'在類型'AngularFireModule'上不存在。」類型'AngularFireModule'上不存在屬性'auth'

的代碼如下

import { AngularFireModule} from 'angularfire2'; 
import { AngularFireAuthModule } from 'angularfire2/auth'; 
import { AngularFireDatabaseModule } from 'angularfire2/database'; 


@Component({ 
selector: 'app-login', 
templateUrl: './login.component.html', 
styleUrls: ['./login.component.css'], 
animations: [moveIn()], 
host: {'[@moveIn]': ''} 
}) 

export class LoginComponent implements OnInit { 
error: any; 
constructor(public af: AngularFireModule,private router: Router) { 
    this.af.auth.subscribe(auth => { 
    if(auth) { 
    this.router.navigateByUrl('/members'); 
    } 
}); 

loginFb() { 
this.af.auth.login({ 
    provider: AuthProviders.Facebook, 
    method: AuthMethods.Popup, 
}).then(
    (success) => { 
    this.router.navigate(['/members']); 
    }).catch(
    (err) => { 
    this.error = err; 
    }) 
    } 
    } 

版本信息: 「@角/路由器」: 「^ 4.0.0」,

"angularfire2": "^4.0.0-rc.1", 

"core-js": "^2.4.1", 

"firebase": "^4.1.3", 

"rxjs": "^5.1.0", 

"zone.js": "^0.8.4" 

UPDATE:

這個工作

constructor(public af: AngularFireAuth,private router: Router) { 
     this.af.authState.subscribe(auth => { 
     if(auth) { 
     this.router.navigateByUrl('/members'); 
     } 

回答

1

對於AngularFireAuth,你應該在你的rootmodule導入AngularFireAuthModule

import { AngularFireModule } from 'angularfire2'; 
import { AngularFireAuthModule } from 'angularfire2/auth'; 

@NgModule({ 
    imports: [ 
    ... 
    AngularFireModule.initializeApp(firebaseConfig), 
    AngularFireDatabaseModule, 
    AngularFireAuthModule,  // <------ mention here 
    ] 
}) 
export class AppModule { } 

然後在你的組件注入AngularFireAuth

import { AngularFireAuth } from 'angularfire2/auth'; 

constructor(private auth: AngularFireAuth, ... 

使用它象下面這樣:

this.auth.auth() 
0

數據庫和權威性,現在分開模塊,AngularFireAuth和AngularFireDatabase

所以在你的構造函數,你應該這樣做

constructor(private router: Router, public af: AngularFireAuth 
) { ... } 

另一個原因是,如果你只是使用FireAuthModule,你可以刪除2個進口(AngularFireModule和AngularFireDatabaseModule)

+0

@php_beginner是AngularFireAuth不AngularFireAuthModule :) – brijmcq

+0

@php_beginner能行嗎? – brijmcq

+0

是的,它的工作原理。謝謝 – sinthu225

相關問題