properties - 具有属性的自定义组件
问题描述
我想知道如何在 Ionic 3 中创建我们自己的具有某些属性的组件。一个例子是离子按钮的“完整”属性。我想要做的是渲染我自己的组件,它看起来像这样
<my-component my-property></my-component>
解决方案
我强烈推荐 angular.io 网站上的任何基础教程。
您想要的是 Ionic 正在利用的 Angular 的基础。
看这里我为你构建了一个简单的例子: https ://stackblitz.com/edit/ionic-f9bq9h
您在 ts 文件中定义组件:
import { Component, Input } from '@angular/core';
@Component({
selector: 'my-component',
template: `<div>{{ myProperty }}</div>`
})
export class MyComponent {
@Input('myProperty') myProperty: string;
constructor() {
}
}
确保它被适当地添加到 app.module.ts
然后您可以在导入它的其他组件中使用它:
<ion-content padding>
<h2>Welcome to Ionic!</h2>
<p>
This starter project comes with simple tabs-based layout for apps
that are going to primarily use a Tabbed UI.
</p>
<p>
Take a look at the <code>pages/</code> directory to add or change tabs,
update any existing page or create new pages.
</p>
<my-component [myProperty]="'I am a custom component'"></my-component>
</ion-content>
推荐阅读
- django - 如何使用 Django Rest Framework 接受来自特定域的请求?
- python - Python视频到帧?
- java - 多级比较器
- c - 在 C 编程中使用 t1dsasm 分解类型 1 .pfa 字体文件格式
- php - laravel 集合中的某些对象与用户模型无关
- ruby - Sorbet 没有报告缺少的方法实现(与 RBI 签名相比),我在这里做错了吗?
- arrays - 我正在尝试从数组中转移到我在 Visual Basic 中的房间搜索系统的列表
- codenameone - 当 VKB 打开时保持工具栏可见
- php - 如何在 laravel 中对不同的表使用多个 JWT 身份验证
- reactjs - 如何在 Prod 中使用 Docker 和 nginx 运行 React?