使用ng build --prod编译Angular项目时出现错误

时间:2018-10-04 07:19:05

标签: angular

这是我遇到的错误:

ERROR in src\app\admin\product-form\product-form.component.html(8,43): : Property 'title' does not exist on type '{}'.
src\app\admin\product-form\product-form.component.html(17,74): : Property 'price' does not exist on type '{}'.
src\app\admin\product-form\product-form.component.html(26,76): : Property 'category' does not exist on type '{}'.
src\app\admin\product-form\product-form.component.html(41,73): : Property 'imageUrl' does not exist on type '{}'.
src\app\admin\product-form\product-form.component.html(8,43): : Property 'title' does not exist on type '{}'.
src\app\admin\product-form\product-form.component.html(17,74): : Property 'price' does not exist on type '{}'.
src\app\admin\product-form\product-form.component.html(26,76): : Property 'category' does not exist on type '{}'.
src\app\admin\product-form\product-form.component.html(41,73): : Property 'imageUrl' does not exist on type '{}'.
src\app\shipping-form\shipping-form.component.html(5,42): : Property 'name' does not exist on type '{}'.
src\app\shipping-form\shipping-form.component.html(12,44): : Property 'email' does not exist on type '{}'.
src\app\shipping-form\shipping-form.component.html(19,56): : Property 'phonenumber' does not exist on type '{}'.
src\app\shipping-form\shipping-form.component.html(26,58): : Property 'addressLine1' does not exist on type '{}'.
src\app\shipping-form\shipping-form.component.html(33,58): : Property 'addressLine2' does not exist on type '{}'.
src\app\shipping-form\shipping-form.component.html(40,42): : Property 'city' does not exist on type '{}'.
src\app\shipping-form\shipping-form.component.html(5,42): : Property 'name' does not exist on type '{}'.
src\app\shipping-form\shipping-form.component.html(12,44): : Property 'email' does not exist on type '{}'.
src\app\shipping-form\shipping-form.component.html(19,56): : Property 'phonenumber' does not exist on type '{}'.
src\app\shipping-form\shipping-form.component.html(26,58): : Property 'addressLine1' does not exist on type '{}'.
src\app\shipping-form\shipping-form.component.html(33,58): : Property 'addressLine2' does not exist on type '{}'.
src\app\shipping-form\shipping-form.component.html(40,42): : Property 'city' does not exist on type '{}'.

4 个答案:

答案 0 :(得分:0)

您需要使用* ngIf(在模板文件中),也可以使用三元运算符。

模板

<div *ngIf="user">
   <span>Hello {{user.fname}}</span>
</div>

<div>
   <span>Hello {{user?.fname}}</span>
</div>

答案 1 :(得分:0)

解决方案1: 创建接口并在组件类中将接口分配为对象类型

interface myObjInterface {
   title : string;
   price : string;
   category : string;
   imageUrl : string;
   title : string;
   price : string;
   category : string;
   imageUrl : string;
   name : string;
   email : string;
   phonenumber : string;
   addressLine1 : string;
   addressLine2 : string;
   city : string;
   name : string;
   email : string;
   phonenumber : string; 
   addressLine1 : string;
   addressLine2 : string;
   city : string;
}

在组件类中:

yourObject  : myObjInterface;

解决方案2: 使用 * ngIf 并在对象为空时隐藏父div

<div *ngIf="yourObject && yourObject.title">
      /* Your html code should be here*/
   </div>

解决方案3:     在Component class中:

yourObject  : any = {
   title : "",
   price : "",
   category : "",
   imageUrl : "",
   title : "",
   price : "",
   category : "",
   imageUrl : "",
   name : "",
   email : "",
   phonenumber : "",
   addressLine1 : "",
   addressLine2 : "",
   city : "",
   name : "",
   email : "",
   phonenumber : "",
   addressLine1 : "",
   addressLine2 : "",
   city : ""
};

答案 2 :(得分:0)

这样做,问题就会消失,

在 product-form.component.ts 中:

product = {}; => product: any = {};

在shipping-form.component.ts

shipping = {}; => shipping: any = {};

答案 3 :(得分:0)

如果您编写 <span >{{user.name}}</span>,编译器会验证以下内容:

  • user 是组件类的一个属性。
  • user 是一个具有 name 属性的对象。

重要的是,它不会检查嵌入视图,例如 *ngIf*ngFor、其他 <ng-template> 嵌入视图。 它没有弄清楚#refs 的类型、管道的结果、事件绑定中 $event 的类型等等。

话虽如此,您可以执行以下解决方案之一:

  • 使用强类型类/接口/类型
  • 将您的绑定包裹在 *ngIf
  • 将变量声明为 any

我建议使用前两个选项,定义您的类型,因为这是正确的做法,仅当数据可用时才使用 *ngIf 呈现视图。 如果您不使用 *ngIf,请确保使用空合并运算符 '?' like 遵循 <span >{{user?.name}}</span> 到模板中的空引用异常。