其他分享
首页 > 其他分享> > angular笔记

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进来就可以了

 

标签:declarations,笔记,管道,num,DialogElementsExampleComponent,str,SubStrPipe,angular
来源: https://www.cnblogs.com/fpcing/p/12036841.html