-
Notifications
You must be signed in to change notification settings - Fork 1
A mechanism to read the package.json version
Rodrigo Silveira edited this page Jul 5, 2019
·
1 revision
It is common to render the web application version in the application header. The common approach has been to copy and past the package.json.version
value to a component attribute and refer to it on a template:
...
"version": "0.3.3",
...
package.json
...
import { Component } from '@angular/core';
...
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
title = 'living-style-guide';
version = `0.33`;
...
projects/living-style-guide/src/app/app.component.ts
...
... <span style="font-size: 60%;"> v{{version}}</span> ...
...
projects/living-style-guide/src/app/app.component.html
I often update the package.json
version, but forget to copy and paste to projects/living-style-guide/src/app/app.component.ts
.
My app looks bad!
The following mechanism reads the package.json
version, freeing me from having to copy and paste it.
Add the following lines to your
{
"extends": "../../tsconfig.json",
"compilerOptions": {
...
"resolveJsonModule": true,
"esModuleInterop": true,
"types": []
...
}
projects/living-style-guide/tsconfig.app.json
...
import { version } from '../../../../package.json';
...
public version: string = version;
...
projects/living-style-guide/src/app/app.component.ts
Voila!