是否可以覆盖内置的Angular 2管道,以便可以在全局范围内使用它们? [英] Is it possible to override the built-in Angular 2 pipes so they can be used globally?

查看:86
本文介绍了是否可以覆盖内置的Angular 2管道,以便可以在全局范围内使用它们?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想覆盖日期"管道,并像内置管道一样享受遍及全球的访问权限,也就是避免在每个组件注释中导入和使用pipe []数组.这可能吗?

I would like to override the "date" pipe and enjoy the benefit of global access everywhere just like the built-in pipe--aka, avoid having to import and use pipes[] array in every component annotation. Is this possible?

推荐答案

是的,您可以使用

Yes, you can use PLATFORM_PIPES to add a custom pipe and name that pipe date to hijack it.

@Pipe({
   name : 'date' // Hijacks the 'date' pipe
})
class CustomDatePipe {
  transform(val, args) {
    return /* do something new with the value */;
  }
}

@Component({
  selector: 'my-app',
  template : '{{mydate | date}}',
})
export class App {
  mydate = Date.now();
}

// Provides the CustomDatePipe globally
bootstrap(App, [provide(PLATFORM_PIPES, {useValue: [CustomDatePipe], multi: true})]);

这样,您不必每次都在组件的pipes属性中添加指定它.

This way you won't have to add specify it every time in pipes property in your components.

这是一个 plnkr ,并带有示例.

Here's a plnkr with an example working.

这篇关于是否可以覆盖内置的Angular 2管道,以便可以在全局范围内使用它们?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆