如何在Angular应用中显示应用版本?版本应该从包中取出。json文件。
{
"name": "angular-app",
"version": "0.0.1",
...
}
在Angular 1中。x,我有这个html:
<p><%=version %></p>
在Angular中,它不会被渲染为版本号,而是直接打印出来(<%=version %>而不是0.0.1)。
如何在Angular应用中显示应用版本?版本应该从包中取出。json文件。
{
"name": "angular-app",
"version": "0.0.1",
...
}
在Angular 1中。x,我有这个html:
<p><%=version %></p>
在Angular中,它不会被渲染为版本号,而是直接打印出来(<%=version %>而不是0.0.1)。
当前回答
打印稿
import { Component, OnInit } from '@angular/core';
declare var require: any;
@Component({
selector: 'app-version',
templateUrl: './version.component.html',
styleUrls: ['./version.component.scss']
})
export class VersionComponent implements OnInit {
version: string = require( '../../../../package.json').version;
constructor() {}
ngOnInit() {
}
}
HTML
<div class="row">
<p class="version">{{'general.version' | translate}}: {{version}}</p>
</div>
其他回答
如果你想在你的angular应用中使用/显示版本号,请执行以下操作:
先决条件:
通过Angular CLI创建的Angular文件和文件夹结构
Angular 6.1 (TS 2.9+)到Angular 11的步骤
在/tsconfig中。Json(有时在/src/tsconfig.app.json中也需要)启用以下选项(之后需要重新启动webpack开发服务器):
"compilerOptions": {
...
"resolveJsonModule": true,
...
然后在你的组件中,例如/src/app/app.component.ts使用这样的版本信息:
import { version } from '../../package.json';
...
export class AppComponent {
public version: string = version;
}
当在Angular 12+中使用这段代码时,你可能会得到:错误:不应该从default- exports模块导入命名的export 'version'(导入为'version')(很快只有default export可用)。 在这种情况下,请使用以下代码:
Angular 12+的步骤
在/tsconfig中。/src/tsconfig.app.json(有时在/src/tsconfig.app.json中也需要)启用以下选项(之后需要重新启动webpack开发服务器):
"compilerOptions": {
...
"resolveJsonModule": true,
"allowSyntheticDefaultImports": true,
...
然后在你的组件中,例如/src/app/app.component.ts使用这样的版本信息:
import packageJson from '../../package.json';
...
export class AppComponent {
public version: string = packageJson.version;
}
在您的环境中也可以执行第2步。Ts文件,使版本信息可从那里访问。
谢谢@Ionaru和@MarcoRinck的帮助。
这个解决方案不应该包含包。json内容——只有版本号(在Angular 8和13中勾选)。
为了确保这一点,请检查生成的main.#hash#.js文件!
这个https://github.com/angular/angular-cli/issues/5190有一个有趣的解决方案,使用shell文件来生成版本。ts文件。@sanjeev在这个论坛上有一个类似的解决方案。
这个解决方案使用Node, javascript和fs模块来实现相同的功能。在linux中工作是肯定的。在Windows中应该工作正常,因为Node/fs是跨平台的…对吧?
add a line to package.json: { "version": "0.0.1", ... "scripts": { "version": "node gen_version.js $npm_package_version", ... }, ... } create gen_version.js (note: it's not typescript) var fs = require('fs'); // arg[0]: node path // arg[1]: this script's path // arg[2]: 1st arg on command line console.log('current version: ' + process.argv[2]) path = 'src/environments/version.ts' script = 'export class Constants {' script += ' static readonly version = \'' + process.argv[2] + '\'' script += '}' fs.writeFile(path, script, function (err, file) { if (err) throw err; console.log('file saved:', path); }); Run it from npm: npm run version > omnitool@0.0.1 version ... > node gen_version.js $npm_package_version current version: 0.0.1 file saved: src/environments/version.ts use from your app: import {Constants} from "../environments/version" ... console.log('Version:', Constants.version)
为angular cli用户提供简单的解决方案。
添加声明模块'*.json';在src / typings.d.ts
然后在src/environments/environment.ts上:
import * as npm from '../../package.json';
export const environment = {
version: npm.version
};
:完成)
你可以看包装。Json就像任何其他文件一样,带有http。像这样:
import {Component, OnInit} from 'angular2/core';
import {Http} from 'angular2/http';
@Component({
selector: 'version-selector',
template: '<div>Version: {{version}}</div>'
})
export class VersionComponent implements OnInit {
private version: string;
constructor(private http: Http) { }
ngOnInit() {
this.http.get('./package.json')
.map(res => res.json())
.subscribe(data => this.version = data.version);
}
}
如果你正在使用webpack或angular-cli(使用webpack),你可以只需要package。Json,然后显示那个道具。
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
然后是分量
@Component({
selector: 'stack-overflow',
templateUrl: './stack-overflow.component.html'
})
export class StackOverflowComponent {
public appVersion
constructor() {
this.appVersion = appVersion
}
}