angular笔记

1. 怎么在多个component中使用 自定义管道

先建好管道

import {Pipe, PipeTransform } from '@angular/core';

@Pipe({
    name: 'subStr'
})
export class SubStrPipe implements PipeTransform {

    transform(str: string, num: number): string {
        if(str.length > num){
            return str.substring(0, num) + '...';
        }else{
            return str;
        }

    }
}

  

然后再随便哪一个,最好的等级高一点的module中去定义它,我这里直接两个管道一期定义了。

最重要的是在

declarations
exports
两个地方加上管道名
@NgModule({
    declarations: [
        SubStrPipe,
        TimeZonePipe,
        DialogElementsExampleComponent
    ],
    imports: [
        RouterModule.forChild(routes),

    ],
    exports: [
        SubStrPipe,
        TimeZonePipe
    ],
    entryComponents: [
        DialogElementsExampleComponent
    ]
})
export class TeacherModule
{
}

  

2. 让组件component在不同的moudle中可以引用,

如上图,

DialogElementsExampleComponent
所在的位置,
declarations
entryComponents
这两个位置家进去



-----------------------------------以上的内容全部都是在父组件,也就是上面提到的高级一点的module

然后,我们在哪里需要用到它,就在那个moudle中

 

 import进来就可以了

 

posted @ 2019-12-13 18:56  fpc  阅读(228)  评论(0编辑  收藏  举报