财产' auth'类型' AngularFireModule'

时间:2017-07-06 06:02:54

标签: angular firebase firebase-authentication angularfire

我使用Angular 4和Firebase进行身份验证。我收到了错误"财产' auth'类型' AngularFireModule'。"

上不存在

守则如下

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;
  })
  }
   }

版本信息:     " @ angular / router":" ^ 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"

更新:

这有效

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

2 个答案:

答案 0 :(得分: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()

答案 1 :(得分:0)

数据库和auth现在是分离的模块,AngularFireAuth和AngularFireDatabase

所以在你的构造函数中,你应该这样做

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

另一个是如果您只是使用FireAuthModule,则可以删除其他2个导入(AngularFireModule和AngularFireDatabaseModule)