How to display the app version in Angular?

后端 未结 10 1544
暗喜
暗喜 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:23

    If you're using webpack or angular-cli (which uses webpack), you can just require package.json in your component and display that prop.

    const { version: appVersion } = require('../../package.json')
    // this loads package.json
    // then you destructure that object and take out the 'version' property from it
    // and finally with ': appVersion' you rename it to const appVersion
    

    And then you have your component

    @Component({
      selector: 'stack-overflow',
      templateUrl: './stack-overflow.component.html'
    })
    export class StackOverflowComponent {
      public appVersion
    
      constructor() {
        this.appVersion = appVersion
      }
    }
    

提交回复
热议问题