【问题标题】:Property 'name' does not exist on type 'AppComponent'“AppComponent”类型上不存在属性“名称”
【发布时间】:2017-09-19 22:27:30
【问题描述】:

下面是我的表格。在ng serve -o 期间工作正常。没有提出任何问题。没有出现错误。

<form #f="ngForm" (ngSubmit)="onSubmit(f.value)">
    <div class="form-group">
        <label for="customerName">Your Mobile Money Account Name</label>
        <input type="text" class="form-control" id="customerName" aria-describedby="emailHelp" placeholder="e.g Pantang Francias"
            name="name" [(ngModel)]="name">
        <small id="emailHelp" class="form-text text-muted">This can be name used in registering the Mobile Money</small>
    </div>
    <div class="form-group">
        <label for="network">Mobile Money Network</label>
        <select class="form-control" name="network" id="network" aria-describeby="networkHelp" [(ngModel)]="network" required>
                  <option value="airtel-gh">Airtel Mobile Money</option>
                  <option value="tigo-gh">Tigo Cash</option>
                  <option value="mtn-gh">MTN Mobile Money</option>
                </select>
        <small id="networkHelp" class="form-text text-muted">We currently support only Airtel Mobile Money</small>
    </div>
    <div class="form-group">
        <label for="number">Your Mobile Number</label>
        <input type="number" name="phone_number" class="form-control" id="phone_number" placeholder="e.g 0269201707" aria-describeby="phone_numberHelp"
            [(ngModel)]="phone_number" required maxlength="10">
        <small id="phone_numberHelp">The Mobile Money number payment will come from. It should belong to you.</small>
    </div>
    <div class="form-group">
        <label for="internet_package">Internet Package</label>
        <select class="form-control" name="internet_package" id="internet_package" aria-describeby="packageHelp" [(ngModel)]="internet_package"
            required>
                  <option value="1gig">1 Gig - 30 Days - 5 Cedis</option>
                  <option value="3gig">3 Gig - 30 Days - 10 Cedis</option>
                  <option value="10gig">10 Gig - 30 Days - 30 Cedis</option>
                </select>
        <small id="packageHelp" class="form-text text-muted">Choose your package</small>
    </div>
    <a class="btn btn-primary text-white" role="button" data-toggle="modal" data-target="#reviewPurchase">Review Purchase</a>
    <!-- Review Purchase Modal -->
    <div class="modal fade" id="reviewPurchase" tabindex="-1" role="dialog" aria-labelledby="reviewPurchase" aria-hidden="true">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title" id="reviewPurchase">Review your Purchase</h5>
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                    </button>
                </div>
                <div class="modal-body">
                    <ul class="list-group">
                        <li class="list-group-item">Name: <strong>{{ f.value.name}}</strong></li>
                        <li class="list-group-item">Mobile Money Network: <strong>{{ f.value.network }}</strong></li>
                        <li class="list-group-item">Your Number: <strong>0{{ f.value.phone_number }}</strong></li>
                        <li class="list-group-item">Internet Package: <strong>{{ f.value.internet_package }}</strong></li>
                    </ul>
                    <hr>
                    <p class="lead text-center text-danger" *ngIf="!f.valid">
                        You have not fully completed the form. Go back and fill all the fields.
                    </p>
                    <p class="lead text-center" *ngIf="f.valid">
                        Proceed below if details above is correct.
                    </p>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                    <button type="submit" [disabled]="!f.valid" role="button" class="btn btn-primary">Make Payment</button>
                </div>
            </div>
        </div>
    </div>
</form>

但是..

我马上ng build --prod,我得到这个错误:

它到底在抱怨什么样的错误?为什么它现在抱怨,但当我运行ng serve 时却没有?

错误 ng:///home/khophi/Developments/Angular/BuyUnifi/src/app/app.component.html (18,151):“AppComponent”类型上不存在属性“名称”。 ng:///home/khophi/Developments/Angular/BuyUnifi/src/app/app.component.html 中的错误 (23,104):“AppComponent”类型上不存在属性“network”。 ng:///home/khophi/Developments/Angular/BuyUnifi/src/app/app.component.html 中的错误 (32,161):类型上不存在属性“phone_number” '应用组件'。 ng:///home/khophi/Developments/Angular/BuyUnifi/src/app/app.component.html 中的错误 (37,122):类型上不存在属性“internet_package” '应用组件'。 ng:///home/khophi/Developments/Angular/BuyUnifi/src/app/app.component.html 中的错误 (18,139):“AppComponent”类型上不存在属性“名称”。 ng:///home/khophi/Developments/Angular/BuyUnifi/src/app/app.component.html 中的错误 (23,46):“AppComponent”类型上不存在属性“网络”。 ng:///home/khophi/Developments/Angular/BuyUnifi/src/app/app.component.html 中的错误 (32,38): 类型上不存在属性“phone_number” '应用组件'。 ng:///home/khophi/Developments/Angular/BuyUnifi/src/app/app.component.html 中的错误 (37,46):类型上不存在属性“internet_package” '应用组件'。

编辑

这是我的AppComponent

import { Component } from '@angular/core';
import { NgForm } from '@angular/forms';
import { PurchaseService } from './purchase.service';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {

  busy: boolean;

  constructor(public purchase: PurchaseService) {}

  onSubmit(data) {
    console.log(data);
  };
}

【问题讨论】:

  • 组件上都存在吗?
  • @AlexanderLeonov 编辑添加我的AppComponent
  • 这就是你的答案。您必须在组件中显式声明所有这些属性。 JIT 允许这种技巧,而 AOT 不允许。

标签: angular


【解决方案1】:

在这一行中,您尝试绑定到名为 name 的变量

<input type="text" class="form-control" id="customerName" aria-describedby="emailHelp" placeholder="e.g Pantang Francias"
        name="name" [(ngModel)]="name">

您的组件中没有name 变量,因此出现错误。

【讨论】:

  • 它抱怨的所有其他属性也是如此。
【解决方案2】:

我通过在实现 OnInit 的 TS 文件的类中声明 [(ngModel)] 的属性得到它。

HTML 文件

 <input type="text" 
        class="form-control"
        id="customerName"
        aria-describedby="emailHelp"
        placeholder="e.g Pantang Francias"
        name="name" 
        [(ngModel)]="name_test">

TS 文件

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
  name_test: String = "";
}

只需关注name_test,您就会得到答案。

学习愉快!

【讨论】:

    【解决方案3】:

    在您的 app.component.html 中,您已经创建了一个带有输入字段的表单,并且您已经声明了模型来绑定您的输入字段数据。

    <input type="text" class="form-control" id="customerName" aria-describedby="emailHelp" placeholder="e.g Pantang Francias"
            name="name" [(ngModel)]="name">
    
    <select class="form-control" name="network" id="network" aria-describeby="networkHelp" [(ngModel)]="network" required>
    
    <input type="number" name="phone_number" class="form-control" id="phone_number" placeholder="e.g 0269201707" aria-describeby="phone_numberHelp"
            [(ngModel)]="phone_number" required maxlength="10">
    

    这些models-name、network、phone_number需要添加到app.component.ts中

    【讨论】:

      【解决方案4】:

      最近遇到了同样的问题,也许这对你有帮助。

      1. FormsModule 添加到您的app.modules.ts by
      import { FormsModule } from '@angular/forms';
      
      @NgModule({
        declarations: [
          AppComponent
        ],
        imports: [
      ....
          FormsModule
        ],
      ...
      })
      
      1. 在您的 ts 文件中声明这些变量,方法是添加 name!: string;network!: any; 的其他类型

      【讨论】:

        【解决方案5】:

        很明显,我们可以看到,您没有在 AppComponent 文件中声明变量 name、network、phone_number 等。

        【讨论】:

          猜你喜欢
          • 2021-05-07
          • 1970-01-01
          • 1970-01-01
          • 2021-11-30
          • 2018-01-23
          • 1970-01-01
          • 1970-01-01
          • 2019-12-10
          • 2020-11-20
          相关资源
          最近更新 更多