属性在离子打字稿上不存在

时间:2020-08-19 01:17:11

标签: typescript ionic-framework

我正在尝试使用成员的用户名和密码创建一个todo对象,以保存ngmodel带来的数据。但是,我无法成功声明该对象。我尝试在logForm函数上使用箭头表示法来实现这一点,但是它似乎不起作用。我认为正在发生的事情是todo对象没有正确声明。

import { Component, OnInit } from '@angular/core';

import {BackendDataService} from '../../backend-data.service';

import { map } from 'rxjs/operators';

import { Router } from '@angular/router';

import {LocalStorageService} from '../../local-storage.service';


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



  
  username: string;

  password: string;
  
  todo: Todo;

  /*
  todo = {

  }
  */


  constructor(private backendDataService: BackendDataService, private router: Router, private storage: LocalStorageService) { 
  //constructor = (private backendDataService: BackendDataService, private router: Router, private storage: LocalStorageService) => {
  
    this.todo = new Todo();
    
    
  
  
  }

  ngOnInit() {

  }
  


  

  
  





  //logForm() {
  logForm = () => {


    var obv;



    //var username;
    //var password;
    let username = "";
    let password = "";

    username = this.todo.username; //<<<<ERROR Property 'username' does not exist on type '{}'.
    password = this.todo.password;

    console.log("username: " + username);
    console.log("password: " + password);




    this.storage.set("username", username);
    this.storage.set("password", password);

    obv = this.backendDataService.postLogin(username, password);

    obv.pipe(map( res => res)).subscribe(data => {

        console.log( data.response);

        console.log("isAuthenticated: " + data.isAuthenticated);

        if (data.isAuthenticated == true)
        {
          console.log("response was successful");


            this.router.navigateByUrl('/home');
            //this.router.navigate(['/home']);

        }

        //console.log(data);

        //console.log(data.balance);

        //this.balance = data.balance;

        //this.name = "elephant";

        //this.bitcoinAddress = data.bitcoinAddress;

    });

    

    
  }

}


class Todo
{

  username: string;

  password: string;
  
  test() {
  
    console.log("TEST RAN<<<<");
  
  }


}

2 个答案:

答案 0 :(得分:1)

您不需要这样做。

this.todo = new Todo();

您可以直接在HTML文件中使用

<input [(ngModel)]="todo.username" />
<input [(ngModel)]="todo.password" />

您可以直接引导TS而无需初始化对象。

console.log("username: " + this.todo.username);
console.log("password: " + this.todo.password);




this.storage.set("username", this.todo.username);
this.storage.set("password", this.todo.password);

不需要这种代码。

let username = "";
let password = "";

username = this.todo.username; //<<<<ERROR Property 'username' does not exist on type '{}'.
password = this.todo.password;

最后一点,不需要在这里使用箭头功能,使用像这样的简单功能

loginForm(){
  // code here
}

答案 1 :(得分:0)

尝试一下。

先删除此。 todo: Todo;

然后声明这样的待办事项。

todo = {
 username: '',
 password: ''
}

尝试不使用Todo类。