打字稿错误属性在类型上不存在

打字稿错误属性在类型上不存在

本文介绍了打字稿错误属性在类型上不存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是angularjs 2和ionic 2的新手。我正在使用带有Validators,FormControl和FormGroup的angularjs表单。当我使用ionic serve --lab执行项目时,一切都很好。但是当我构建该项目时,它会给出错误:类型中不存在属性用户名。

I am new to angularjs 2 and ionic 2. I am working with angularjs form with Validators, FormControl and FormGroup. Everything is good when I execute the project using ionic serve --lab. but when I build that project it gives error: Property 'username' does not exist on type.

login.ts

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

import { LoadingController, NavController } from 'ionic-angular';

import { AuthService } from '../../service/authService';

import {Validators, FormBuilder, FormGroup, FormControl } from '@angular/forms';

@Component({
  selector: 'page-login',
  templateUrl: 'login.html'
})
export class LoginPage {

  user: any;

  submitAttempt: boolean = false;

  slideTwoForm : FormGroup;

  constructor(public navCtrl: NavController,
              public authService: AuthService,
              public loadingCtrl: LoadingController,
              private formBuilder: FormBuilder) {

    this.slideTwoForm = formBuilder.group({
      username: ['', Validators.compose([Validators.minLength(5),          Validators.required])],
      description: ['', Validators.required],
      age: ['']
    });
  }

  logForm() {
    if (!this.slideTwoForm.valid) {
      this.submitAttempt = true;
    }
  }

}

login.html

<ion-content padding>
    <form [formGroup]="slideTwoForm" (ngSubmit)="logForm()" novalidate>
        <ion-item>
            <ion-label floating>Username</ion-label>
            <ion-input #username formControlName="username" type="text"></ion-input>
        </ion-item>
        <ion-item *ngIf="!slideTwoForm.controls.username.valid  && submitAttempt">
            <p>Please enter a valid name.</p>
        </ion-item>
        <ion-item>
          <ion-label>Description</ion-label>
          <ion-textarea formControlName="description"></ion-textarea>
        </ion-item>
        <ion-item>
            <ion-label floating>Age</ion-label>
            <ion-input formControlName="age" type="number"></ion-input>
        </ion-item>
        <ion-item *ngIf="!slideTwoForm.controls.age.valid  && submitAttempt">
            <p>Please enter a valid age.</p>
        </ion-item>
        <button ion-button type="submit">Submit</button>
    </form>
</ion-content>

在运行 ionic build android 命令生成apk文件之前一切正常。

有人可以告诉我为什么会收到这些错误吗?

Everything is fine until I run ionic build android command to generate apk file.
Can someone please tell me why I am receiving these errors?

我也引用了这个链接:但它无法帮助我:

I also refers this link: Property does not exist on type. TypeScript but it can't help me:

推荐答案

Ionic2在构建到android时提前使用编译。 AoT要求:

Ionic2 uses ahead of time compile when building to android. AoT requires:

A)要公开的属性

B)html中提到的属性是在组件中声明。

B) properties that are mentioned in the html to be declared in the component.

所以要解决你的问题,请声明
public username:string;在组件中并且还要声明您在表单中访问的其他名称。

So to fix your problem declarepublic username:string; in the component and aswell declare other names you are accessing in your form.

或者......在html中你可以编写formControlName ='user.username'并使用你已宣布的财产。

Or ... in the html you can write formControlName='user.username' and use the property you already declared.

不要忘记在组件中声明你的elementref

Don't forget to declare your elementref in the component aswell

这篇关于打字稿错误属性在类型上不存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 05:51