Angular CLI - How to share prototype functions across the application

前端 未结 3 912
长发绾君心
长发绾君心 2021-01-17 13:07

I need to have some global prototype functions on the string class. Eg.

string.prototype.trimWhiteSpaces = function () {
  return this.replace(/ +/g, \'\');
         


        
3条回答
  •  日久生厌
    2021-01-17 14:01

    I had the urge to prototype some methods too

    We're working on a project that uses thousands of classes and members, a large amount of these members are completely repetitive and could be shared among the components

    after a few minutes searching I came across these solution Angular Global or Shared Modules

    Generally there's 2 ways to handle it

    1. You need to share a particular code among specific components, and not globally

    this would happen to any medium to large web application

    Angular Docs explained how to do so

    1. You need to share some code globally (which this is your case i guess)

    Note that This option is different than just any ordinary global module , we want to populate some prototype with our own functionality

    This Article Easily Shows how to populate prototypes for our own use inside an Angular App

提交回复
热议问题