angular4自定义组件详解

 更新时间:2017年09月28日 11:23:49   作者:不了无明  
这篇文章主要为大家详细介绍了angular4自定义组件的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

在 Angular 中,我们可以使用 {{}} 插值语法实现数据绑定。

新建组件

$ ng generate component simple-form --inline-template --inline-style
# Or
$ ng g c simple-form -it -is # 表示新建组件,该组件使用内联模板和内联样式
//会自动为simple-form生成simple-form.component.ts文件,文件中的selector为:app-simple-form,自动添加了app-前缀

输出:

installing component
 create src/app/simple-form/simple-form.component.spec.ts // 用于单元测试
 create src/app/simple-form/simple-form.component.ts // 新建的组件
 update src/app/app.module.ts //Angular CLI 会自动更新 app.module.ts 文件。把新建的组件添加到 NgModule 的 declarations 

数组中

app.module.ts更新后:

@NgModule({
 declarations: [
  AppComponent,
  SimpleFormComponent
 ],
 ...
})
export class AppModule { }

创建 UserComponent 组件

import { Component } from '@angular/core';

@Component({ //Component 装饰器来定义组件的元信息
 selector: 'sl-user',
 template: `
  <h2>大家好,我是{{name}}</h2>
  <p>我来自<strong>{{address.province}}</strong>省,
   <strong>{{address.city}}</strong>市
  </p>
   <p>{{address | json}}</p>//Angular 内置的 json 管道,来显示对象信息
`, }) 

//定义组件类

export class UserComponent { 
  name = 'name'; 
  address = { province: 'province', city: 'city' } 
}


//使用构造函数初始化数据
export class UserComponent {
  name: string;
  address: any;
  constructor() {
    this.name = 'name';
    this.address = {
      province: 'province',
      city: 'city'
    }
  }
}

//接口使用
interface Address {
  province: string;
  city: string;
}
export class UserComponent {
  name: string;
  address: Address;
  constructor(){
    this.name = 'name';
    this.address = {
      province: 'province',
      city: 'city'
    }
  }
}

定义数据接口( TypeScript 中的接口是一个非常灵活的概念,除了可用于对类的一部分行为进行抽象以外,也常用于对「对象的形状(Shape)」进行描述。)

interface Person {
 name: string;
 age: number;
}

let semlinker: Person = {
 name: 'semlinker',
 age: 31
};

声明 UserComponent 组件

// ...
import { UserComponent } from './user.component';//载入
@NgModule({
 imports:   [ BrowserModule ],
 declarations: [ AppComponent, UserComponent],//声明
 bootstrap:  [ AppComponent ]
})
export class AppModule { }

在AppComponent中使用 UserComponent 组件

import { Component } from '@angular/core';

@Component({
 selector: 'my-app',
 template: `
  <sl-user></sl-user> //UserComponent 的 selector
 `,
})
export class AppComponent {}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • Angular脚手架开发的实现步骤

    Angular脚手架开发的实现步骤

    这篇文章主要介绍了Angular脚手架开发的实现步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-04-04
  • AngularJS实现的输入框字数限制提醒功能示例

    AngularJS实现的输入框字数限制提醒功能示例

    这篇文章主要介绍了AngularJS实现的输入框字数限制提醒功能,涉及AngularJS事件监听与元素属性动态操作相关实现技巧,需要的朋友可以参考下
    2017-10-10
  • Angular使用 ng-img-max 调整浏览器中的图片的示例代码

    Angular使用 ng-img-max 调整浏览器中的图片的示例代码

    本篇文章主要介绍了Angular使用 ng-img-max 调整浏览器中的图片的示例代码,具有一定的参考价值,有兴趣的可以了解一下
    2017-08-08
  • 使用Angular CLI生成路由的方法

    使用Angular CLI生成路由的方法

    这篇文章主要介绍了使用Angular CLI生成路由的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-03-03
  • 动手写一个angular版本的Message组件的方法

    动手写一个angular版本的Message组件的方法

    本篇文章主要介绍了动手写一个angular版本的Message组件的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-12-12
  • AngularJS实现动态编译添加到dom中的方法

    AngularJS实现动态编译添加到dom中的方法

    这篇文章主要介绍了AngularJS实现动态编译添加到dom中的方法,结合实例形式分析了AngularJS动态编辑构建模板的相关操作技巧,需要的朋友可以参考下
    2016-11-11
  • 基于Angularjs实现分页功能

    基于Angularjs实现分页功能

    这篇文章主要介绍了基于Angularjs实现分页功能的相关资料,非常不错,具有参考借鉴价值,需要的朋友可以参考下
    2016-05-05
  • AngularJS内置指令

    AngularJS内置指令

    这篇文章主要详细介绍了AngularJS内置指令的使用方法,非常详细,常用的一些指令操作都做了总结,有需要的小伙伴参考下
    2015-02-02
  • Angular中的interceptors拦截器

    Angular中的interceptors拦截器

    这篇文章主要介绍了Angular中的interceptors拦截器,需要的朋友可以参考下
    2017-06-06
  • 学习Angularjs分页指令

    学习Angularjs分页指令

    这篇文章主要和大家一起学习Angularjs分页指令,代码很详细,文章结构紧凑,感兴趣的小伙伴们可以参考一下
    2016-07-07

最新评论