打字稿错误类型上不存在属性 [英] Typescript error Property does not exist on type

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

问题描述

我是 angularjs 2 和 ionic 2 的新手.我正在使用带有 Validators、FormControl 和 FormGroup 的 angularjs 表单.当我使用 ionic serve --lab 执行项目时,一切都很好.但是当我构建该项目时,它给出了 error: Property 'username' does not exist on type.

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?

我还参考了这个链接:类型上不存在属性.TypeScript 但它帮不了我:

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.

所以要解决您的问题,请声明公共用户名:字符串;在组件中并声明您在表单中访问的其他名称.

So to fix your problem declare public 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

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

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆