How to insert a Build Number or Timestamp at build time in AngularCLI

前端 未结 8 1015
故里飘歌
故里飘歌 2020-12-04 08:51

I want to have a timestamp or build number somewhere on my Angular2 App so I can tell if a user is using an old cached version or not.

How to do this with AngularCLI

8条回答
  •  青春惊慌失措
    2020-12-04 09:05

    There's no need to replace-in-file.

    Simple Solution: Using Angular Environments

    Just inside your desired environment.*.ts file (For more information about environments read angular-2-and-environment-variables) require package.json like so:

    export const environment = {
        version: require('../package.json').version
    };
    

    Then inside your app import environment:

    import { environment } from '../environments/environment';
    

    And you have environment.version. If you get cannot find name 'require' error, Read this answer

    More info

    Note: As @VolodymyrBilyachat mentioned in comments, this will include your package.json file in the final bundle file.

提交回复
热议问题