角度4错误:预期找到1个参数0

时间:2018-08-14 07:23:27

标签: javascript angular firebase syntax-error angularfire2

我正在使用带有SB Admin模板的angular 4的Web应用程序。我正在将Firebase与AngularFire 2一起使用。现在,我试图创建用户从应用程序写入,删除和读取表的权限。我将在下面保留代码:

import { Component, OnInit } from '@angular/core';
import {ActivatedRoute, Params} from "@angular/router";
import {Location} from "@angular/common";
import {UsersService} from "../../../shared/services/users.service";
import {User} from "../../../shared/services/user";
import {AuthService} from "../../../login/auth.service";
import * as firebase from "firebase";
import { AngularFireDatabaseModule } from 'angularfire2/database';


@Component({
  selector: 'app-profile',
  templateUrl: './profile.component.html',
  styleUrls: ['./profile.component.scss']
})
export class ProfileComponent implements OnInit {

    user: User;
    hasProfile = true;
    user_key: string;

  constructor(private route: ActivatedRoute,
              private location: Location,
              private userService: UsersService,
                private authService: AuthService) {
  }

  ngOnInit() {
      this.route.params.subscribe(p => {
          this.userService.getItem(p['key']).subscribe(user => {
              this.user_key = p['key'];
              if (user) {
                  this.user = user;
                  this.hasProfile = true;
              } else {
                  this.user = new User();
              }
          })
      });
  }

    createUserProfile() {
        const data = this.user;
        console.log(data);
        this.userService.createItem(this.user_key, data);
    }

}

问题出在:

 ngOnInit() {
          this.route.params.subscribe(p => {
              this.userService.getItem(p['key']).subscribe(user => {
                  this.user_key = p['key'];
                  if (user) {
                      this.user = user;
                      this.hasProfile = true;
                  } else {
                      this.user = new User();
                  }
              })
          });
      }
  

this.user = new User()错误:预期找到1个参数为0。

我试图像this.user = new User(1)一样放置它,并且编译成功,但是应用程序无法启动。你们中有人发现过这种情况吗?你可以解释吗?

我不是正在学习的Angular Expert,所以我可能会错过一些小东西。

更新: 这是User类的代码:

export class Roles {
    editor: boolean;
    admin: boolean;
}



export class User {
    $key: string;
    email: string;
    name: string;
    city: string;
    profile = false;
    admin = false;
    roles: Roles;
    uid: 'string';

constructor(authData) {
    this.name = '';
    this.city = ''
    this.email = authData.email;
    this.roles = { editor: true, admin: false };
}


}

1 个答案:

答案 0 :(得分:0)

如果constructor类的User参数是可选的(根据您的代码,这可能是可选的),则可以按如下所示修改构造函数:

export class User {
  ..............

  constructor(authData?: any) {
     ...........
  }
}