如何在Angular中显示应用程序的版本
Angular是谷歌推出的一个基于TypeScript的客户端前端网络框架。Angular 8是一个伟大的、可重复使用的UI(用户界面)框架,对开发者来说有助于建立单页应用程序。一般来说,所有Angular 2以上的项目或应用程序都被称为Angular应用程序。早期的版本被称为Angular.js,而Angular是Angular.js的完全重写版,具有丰富和有用的功能。
步骤:
- 为了知道版本,首先,我们需要从’@angular/core’导入’VERSION’。
- 导入这个的原因是它是一个对象,它包含了我们可以用来了解版本的属性。
- 导入后,现在使用导入的版本中的 “完整 “密钥访问该版本。
- 检索到该值后,将其分配给一个变量,然后使用字符串插值在HTML文件中显示。
- 一旦你完成了上述实施工作,就开始项目。
代码实现:
- app.component.ts:
import { Component, VERSION } from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: [ './app.component.css' ]
})
export class AppComponent {
version = VERSION.full;
}
- app.component.html:
<h2> Welcome to GeeksforGeeks </h2>
<p> The version of the App is : {{ version }} </p>
- app.module.ts:
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppComponent } from './app.component';
@NgModule({
imports: [ BrowserModule ],
declarations: [ AppComponent ],
bootstrap: [ AppComponent ]
})
export class AppModule { }
输出:
注:我的版本号是6,因此显示为6。