How to display the app version in Angular?

后端 未结 10 1527
暗喜
暗喜 2020-12-02 04:55

How do I display the app version in angular application? the version should be taken from package.json file

{
  \"name\": \"angular-app\",
  \"v         


        
10条回答
  •  既然无缘
    2020-12-02 05:16

    If you want to use/show the version number in your angular app please do the following:

    Prerequisites:

    • Angular file and folder structure created via Angular CLI

    • TypeScript 2.9 or later! (Supported from Angular 6.1 upwards)

    Steps:

    1. In your /tsconfig.json (sometimes also necessary in /src/tsconfig.app.json) enable the resolveJsonModule option (webpack dev server restart required afterwards):
        "compilerOptions": {
          ...
          "resolveJsonModule": true
          ...
    
    1. Then in your component, for example /src/app/app.component.ts use the version info:
        import { version } from '../../package.json';
        ...
        export class AppComponent {
          public version: string = version;
        }
    

    It's also possible to do step 2 in your environment.ts file, making the version info accessible from there.

    Thx @Ionaru and @MarcoRinck for helping out.

    This solution will not include the package.json contents, only the version number.
    Tested w/Angular8/Node10/TypeScript3.4.3.

    Please update your apps to use this solution cause depending on the contents of your package.json the original solution may implicate security issues.

提交回复
热议问题