我要做的是制作一个调用https://swapi.co/ API 的下拉菜单,一旦选择了一个项目,它将在页脚组件中显示信息。我对 Angular 真的很陌生,在花了最后 8 个小时试图学习这一点之后,我有点迷失了。
body.component.html
<nav class="navbar navbar-default">
<div class="container-fluid">
<ul class="nav nav-pills nav-fill">
<li class="nav-item col-md-3">
<a href="/people" routerLink="/people" routerLinkActive="active">People</a>
</li>
<li class="nav-item col-md-3">
<a href="/planets" routerLink="/planets" routerLinkActive="active">Planets</a>
</li>
<li class="nav-item col-md-3">
<a href="/starships" routerLink="/starships" routerLinkActive="active">Starships</a>
</li>
</ul>
</div>
</nav>
<select>
<option *ngFor="let people of peoples">
{{people.name}}
</option>
</select>
body.component.ts
import { Component, Output, EventEmitter, OnInit } from '@angular/core';
import { StarwarsService } from '../starwars.service';
@Component({
selector: 'app-body',
templateUrl: './body.component.html',
styleUrls: ['./body.component.scss']
})
export class BodyComponent implements OnInit {
peoples: unknown[];
constructor(private starwarsService: StarwarsService) { }
ngOnInit() {
this.starwarsService.getPeoples().subscribe(results => {
console.log(results);
this.peoples = results.results;
});
}
}
页脚.component.ts
import { Component, OnInit, Input } from '@angular/core';
@Component({
selector: 'app-footer',
templateUrl: './footer.component.html',
styleUrls: ['./footer.component.scss']
})
export class FooterComponent implements OnInit {
constructor() { }
ngOnInit() {
}
}
app.component.html
<app-header></app-header>
<hr>
<router-outlet></router-outlet>
<hr>
<app-footer></app-footer>
app.component.ts
import { Component } from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss']
})
export class AppComponent {
title = 'Angular-Routing';
}
慕森王
相关分类