代码之家  ›  专栏  ›  技术社区  ›  Tanzeel

无法使用typescript向对象文本添加新属性

  •  -1
  • Tanzeel  · 技术社区  · 6 年前

    stackoverflow上也有同样的问题,但它们接受的答案对我不起作用,因为它们都没有使用对象文本。不会浪费你的时间。这是我的密码。

    贡献.component.ts ( contribute 只是我用它创建的组件的名称 ng generate component contribute )

    import { Component, OnInit } from '@angular/core';
    import { FormGroup, FormControl } from '@angular/forms';
    import { HttpClient, HttpHeaders } from '@angular/common/http';
    import { AuthService } from '../auth.service';
    
    @Component({
      selector: 'app-contribute',
      templateUrl: './contribute.component.html',
      styleUrls: ['./contribute.component.css']
    })
    export class ContributeComponent implements OnInit {
      makeNewPost={}; //THI IS MY EMPTY OBJECT TO WHICH I WANT TO ADD PROPERTIES LATER IN THIS CODE
    
      constructor(private _auth: AuthService) { }
    
      ngOnInit() {
          //SOME CODE
      }
    
      //THIS FUNCTION WILL BE CALLED ON CLICK EVENT FROM `contribute.component.html`
      onSubmit() {
        var email = (<HTMLInputElement>document.getElementById("email")).value;
        var password = (<HTMLInputElement>document.getElementById("password")).value;
    
        console.log(email); //SEEMS GOOD ON CONSOLE
        console.log(password); //SEEMS GOOD ON CONSOLE
    
        //THESE TWO PROPERTIES I WANT TO ADD
        this.makeNewPost.email=email;
        this.makeNewPost.password=password;
    
        this._auth.registerNewt(this.makeNewPost)
        .subscribe (
          res => console.log(res),
          err => console.log(err)
        );
      }
    }
    

    但据我所知物体在 ts . 那我为什么会犯这个错误。

    错误TS2339:类型{}上不存在属性“email”。 错误TS2339:类型{}上不存在属性“password”。

    请告诉我是否对字体中的对象有错误。

    我还试图声明我的对象为:

    makeNewPost= {
      email: string;
      password: string;
    }
    

    PS:这是一个角度8的项目

    3 回复  |  直到 6 年前
        1
  •  2
  •   T.J. Crowder    6 年前

    typescript的主要功能是 静态类型 对于变量。当你这样做的时候

     makeNewPost={};
    

    …因为您没有为 makeNewPost ,typescript从 {} ,并使其成为没有属性的类型。当然,稍后您将尝试添加属性,但是尽管在javascript中这很好,但是对于typescript的静态类型,这是一个问题。

    解决方案是先包含属性,然后更改它们的值:

     makeNewPost = {
        email: "",
        password: ""
     };
    

    现在,typescript将类型推断为 email password 属性,两个字符串,以后可以指定给它们。


    你没有 最初添加属性,尽管这可能是最干净的解决方案。您可以定义 makenewpost公司 并制作属性 可选择的 (the ? 在属性定义中):

    interface NewPost {
        email?: string;
        password?: string;
    }
    

    然后使用该类型:

    makeNewPost: NewPost = {};
    

    稍后,您将能够分配这些属性,因为它们是允许存在的。

    不过,我不会这么做,因为当你真的需要写一篇新文章时,那些属性 不是 可选。

    第三种方法是定义没有可选属性的类型:

    interface NewPost {
        email: string;
        password: string;
    }
    

    …并宣布 makenewpost公司 成为 Partial<NewPost> :

    makeNewPost: Partial<NewPost> = {};
    

    你会有 this._auth.registerNewt 接受 NewPost 部分<newpost> ,但使用 类型断言 在你填好后(本质上)“我现在填好了:

    this.makeNewPost.email = email;
    this.makeNewPost.password = password;
    
    this._auth.registerNewt(this.makeNewPost as NewPost)
    // ...
    
        2
  •  1
  •   noamyg    6 年前

    你用的是typescript。可以为“post”创建类型:

    export interface Post{
      email : String
      password : String
    }
    

    然后声明 makeNewPost 类型 Post ,并立即用您的价值观启动它:

    let makeNewPost: Post = {
      email : email,
      password : password
    }
    
        3
  •  0
  •   volcanic    6 年前

    如果您事先不知道对象的属性,可以将自定义类型设置为:

    type NewObject = {[key: string]: any};
    let newObject: NewObject = {};
    newObject.property = 'value';
    
    推荐文章