栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > Web开发 > JavaScript

angular组件继承的实现方法第1/2页

JavaScript 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

angular组件继承的实现方法第1/2页

Angular 2.3 版本中引入了组件继承的功能,该功能非常强大,能够大大增加我们组件的可复用性。

组件继承涉及以下的内容:

metadata:如 @Input()、@Output()、@ContentChild/Children、@ViewChild/Children 等。在派生类中定义的元数据将覆盖继承链中的任何先前的元数据,否则将使用基类元数据。

Constructor:如果派生类未声明构造函数,它将使用基类的构造函数。这意味着在基类构造函数注入的所有服务,子组件都能访问到。

Lifecycle hooks:如果基类中包含生命周期钩子,如 ngOnInit、ngonChanges 等。尽管在派生类没有定义相应的生命周期钩子,基类的生命周期钩子会被自动调用。

需要注意的是,模板是不能被继承的 ,因此共享的 DOM 结构或行为需要单独处理。了解详细信息,请查看 - properly support inheritance。

接下来我们来快速体验的组件继承的功能并验证以上的结论,具体示例如下(本文所有示例基于的 Angular 版本是 - 4.0.1):

exe-base.component.ts

import { Component, ElementRef, Input, HostBinding, HostListener, onInit } from '@angular/core'; @Component({
  selector: 'exe-base', // template will not be inherited template: `
  
    exe-base:我是base组件么? - {{isbase}}
  
 ` }) export class baseComponent implements onInit { @Input() isbase: boolean = true; @HostBinding('style.color') color = 'blue'; // will be inherited @HostListener('click', ['$event']) // will be inherited onClick(event: Event) { console.log(`I am baseComponent`);
  } constructor(protected eleRef: ElementRef) { }

  ngonInit() { console.dir('baseComponent:ngonInit method has been called');
  }
}

exe-inherited.component.ts

import { Component, HostListener, OnChanges, SimpleChanges } from '@angular/core'; import { baseComponent } from './exe-base.component'; @Component({
  selector: 'exe-inherited',
  template: `
  
   exe-inherited:我是base组件么? - {{isbase}}
  
 ` }) export class InheritedComponent extends baseComponent implements onChanges { @HostListener('click', ['$event']) // overridden onClick(event: Event) { console.log(`I am InheritedComponent`);
  }
  ngonChanges(changes: SimpleChanges) { console.dir(this.eleRef); // this.eleRef.nativeElement:exe-inherited }
}

app.component.ts

import { Component, onInit } from '@angular/core'; import {ManagerService} from "./manager.service"; @Component({
 selector: 'exe-app',
 template: `
  
  
` }) export class AppComponent { currentPage: number = 1; totalPage: number = 5; }

接下来我们简要讨论一个可能令人困惑的主题,@Component() 中元数据是否允许继承?答案是否定的,子组件是不能继承父组件装饰器中元数据。限制元数据继承,从根本上说,是有道理的,因为我们的元数据用是来描述组件类的,不同的组件我们是需要不同的元数据,如 selector、template 等。Angular 2 组件继承主要还是逻辑层的复用,具体可以先阅读完下面实战的部分,再好好体会一下哈。

现在我们先来实现一个简单的分页组件,simple-pagination.component.ts

import { Component, Input, Output, EventEmitter } from '@angular/core'; @Component({
  selector: 'simple-pagination',
  template: `
     
    
    

page {{ page }} of {{ pageCount }}

` }) export class SimplePaginationComponent { @Input() pageCount: number; @Input() page: number; @Output() pageChanged = new EventEmitter(); nextPage() { this.pageChanged.emit(++this.page); } previousPage() { this.pageChanged.emit(--this.page); } hasPrevious(): boolean { return this.page > 1; } hasNext(): boolean { return this.page < this.pageCount; } }

app.component.ts

import { Component, onInit } from '@angular/core'; import {ManagerService} from "./manager.service"; @Component({
 selector: 'exe-app',
 template: `
  12下一页阅读全文
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/82246.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号