【问题标题】:How to pass values from HTML fields to component ts如何将值从 HTML 字段传递到组件 ts
【发布时间】:2019-09-09 12:47:58
【问题描述】:

我在不使用表单的情况下将值从 HTML 页面发送到 TS 组件,当我尝试 npm run build 时出现构建错误,这是我的示例:

我使用 >npm run dev 或 >ng build 没有问题 但是当我为生产构建它时,它会通过一个错误使用

npm 运行构建

src\app\messages\messages-new\messages-new.component.html(52,22): : 类型“MessagesNewComponent”上不存在属性“messageTitle”。

HTML:

<input matInput placeholder="Type a title" name="messageTitle" [(ngModel)]="messageTitle">


<button class="simple-form-button" color="primary" mat-raised-button type="submit"  value="submit" (click)="sendMessage(messageTitle)">
      <mat-icon>send</mat-icon> Send 
    </button>

【问题讨论】:

标签: angular typescript binding


【解决方案1】:

这意味着你需要在你的component.ts中声明变量

messageTitle : string;

【讨论】:

  • 那么 npm run build 和 ng build 有什么区别
  • 有什么区别?
  • 在组件中声明messageTitle解决了这个问题,但是如果我想将参数列表传递给组件,我必须先定义它才能绑定它吗?
  • 在开发模式下模板编译到浏览器中的类 Just In Time。在 prod 模式节点尝试将模板编译为类和 typescript 编译找不到必要的属性
  • 是的,无论您在 HTML 中使用什么,您都需要定义这些变量。
猜你喜欢
  • 2021-12-23
  • 1970-01-01
  • 1970-01-01
  • 2020-02-01
  • 2019-10-13
  • 2014-07-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多