How to structure utility class

前端 未结 5 755
深忆病人
深忆病人 2020-12-12 19:48

I have several utility functions. What is the best way to package these up, and then import them?

This is what I am trying to do:

import * as util f         


        
5条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-12 20:01

    You can also create a util.ts class which has a function to export

    export const formatDateOfBirth = 
    (dob: string) : string => `${dob.substring(0, 4)}-${dob.substring(4, 6)}-${dob.substring(6, 8)}`;
    

    Now you can import the method as below, shared folder structure is src > app > shared, I am calling this import inside src > app > shelf > shelf.component.ts file

    import { formatDateOfBirth } from '../shared/utils/util';
    public getFormattedDate(dob: string):string{
      return formatDateOfBirth(dob);
    }
    

提交回复
热议问题