Angular教程

Angular 第一个应用

以下是创建第一个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
Angular 7项目设置(创建第一个应用程序)
导航到第一个应用程序。
cd my-first-app
启动服务器以运行应用程序。
ng serve
Angular 7项目设置(创建第一个应用程序)
您的服务器正在localhost: 4200上运行。现在,转到浏览器并打开它。
Angular 7项目设置(创建第一个应用程序)
现在,您需要一个IDE来编辑和运行应用程序的代码。在这里,我们正在使用WebStorm。
打开WebStorm并在IDE中打开您的应用程序" my-first-app"。看起来像这样:
Angular 7项目设置(创建第一个应用程序)
在这里,转到到src文件夹,您将在其中看到app文件夹。展开应用文件夹。
Angular 7项目设置(创建第一个应用)
您将在此处看到5个组件:
app.component.css app.component.html app.component.spec.ts app.component.ts app.module.ts
您可以在不同组件中查看代码,以了解正在发生的事情以及哪个部分负责应用程序的外观。
app.component.css
此部分为空,因为我们在此处未指定任何CSS。
 Angular 7项目设置(创建第一个应用程序)
app.component.html
这是最重要的组件,即应用程序的首页。在这里,您可以更改应用名称之前使用的称呼。您还可以更改首页及其相应链接上的内容。
Angular 7项目设置(创建第一个应用)
代码:
<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 { }

昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4