个性化阅读
专注于IT技术分析

angular 7第一个app

以下是用于创建第一个Angular应用程序的Angular CLI命令。

npm install -g @angular/cli
ng new my-dream-app
cd my-dream-app
ng serve

运行以下命令以创建第一个Angular应用。

ng new my-first-app

导航到你的第一个应用程序。

cd my-first-app

启动服务器以运行应用程序。

ng serve

你的服务器正在localhost:4200上运行。现在,转到浏览器并打开它。

现在,你需要一个IDE来编辑和运行你的应用程序的代码。在这里,我们正在使用WebStorm。

打开WebStorm并在IDE中打开你的应用程序“ my-first-app”。它看起来像这样:

在这里,转到src文件夹,你将在其中看到app文件夹。展开应用文件夹。

你将在此处看到5个组件:

  • app.component.css
  • app.component.html
  • app.component.spec.ts
  • app.component.ts
  • app.module.ts

你可以在不同组件中查看代码,以了解发生了什么以及哪个部分负责应用程序的外观。

app.component.css

这部分是空的,因为我们在这里没有指定任何CSS。

app.component.html

这是最重要的组件,即应用程序的首页。在这里,你可以更改应用名称之前使用的称呼。你还可以更改首页及其相应链接上的内容。

代码:

<div style="text-align:center">
  <h1>
    Welcome to {{ title }}!
  </h1>
  <img width="300" alt="Angular Logo" src="data:image/svg+xml;base64, PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgMCAyNTAgMjUwIj4KICAgIDxwYXRoIGZpbGw9IiNERDAwMzEiIGQ9Ik0xMjUgMzBMMzEuOSA2My4ybDE0LjIgMTIzLjFMMTI1IDIzMGw3OC45LTQzLjcgMTQuMi0xMjMuMXoiIC8+CiAgICA8cGF0aCBmaWxsPSIjQzMwMDJGIiBkPSJNMTI1IDMwdjIyLjItLjFWMjMwbDc4LjktNDMuNyAxNC4yLTEyMy4xTDEyNSAzMHoiIC8+CiAgICA8cGF0aCAgZmlsbD0iI0ZGRkZGRiIgZD0iTTEyNSA1Mi4xTDY2LjggMTgyLjZoMjEuN2wxMS43LTI5LjJoNDkuNGwxMS43IDI5LjJIMTgzTDEyNSA1Mi4xem0xNyA4My4zaC0zNGwxNy00MC45IDE3IDQwLjl6IiAvPgogIDwvc3ZnPg==">
</div>
<h2>Here are some links to help you start: </h2>
<ul>
  <li>
    <h2><a target="_blank" rel="noopener" href="https://angular.io/tutorial">Tour of Heroes</a></h2>
  </li>
  <li>
    <h2><a target="_blank" rel="noopener" href="https://angular.io/cli">CLI Documentation</a></h2>
  </li>
  <li>
    <h2><a target="_blank" rel="noopener" href="https://blog.angular.io/">Angular blog</a></h2>
  </li>
</ul>

app.component.spec.ts:

该文件仅用于测试目的。

import { TestBed, async } from '@angular/core/testing';
import { AppComponent } from './app.component';

describe('AppComponent', () => {
  beforeEach(async(() => {
    TestBed.configureTestingModule({
      declarations: [
        AppComponent
      ], }).compileComponents();
  }));

  it('should create the app', () => {
    const fixture = TestBed.createComponent(AppComponent);
    const app = fixture.debugElement.componentInstance;
    expect(app).toBeTruthy();
  });

  it(`should have as title 'my-first-app'`, () => {
    const fixture = TestBed.createComponent(AppComponent);
    const app = fixture.debugElement.componentInstance;
    expect(app.title).toEqual('my-first-app');
  });

  it('should render title in a h1 tag', () => {
    const fixture = TestBed.createComponent(AppComponent);
    fixture.detectChanges();
    const compiled = fixture.debugElement.nativeElement;
    expect(compiled.querySelector('h1').textContent).toContain('Welcome to my-first-app!');
  });
});

app.component.ts

你可以在此处更改应用的名称。你只需要更改标题。

import { Component } from '@angular/core';

@Component({
  selector: 'app-root', templateUrl: './app.component.html', styleUrls: ['./app.component.css']
})
export class AppComponent {
  title = 'my-first-app';
}

app.module.ts

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';

import { AppComponent } from './app.component';

@NgModule({
  declarations: [
    AppComponent
  ], imports: [
    BrowserModule
  ], providers: [], bootstrap: [AppComponent]
})
export class AppModule { }

赞(0)
未经允许不得转载:srcmini » angular 7第一个app

评论 抢沙发

评论前必须登录!