35
loading...
This website collects cookies to deliver better user experience
tour-server
.npx create-strapi-app tour-server --quickstart
Tour Event
content type. This content type will serve as the structure for the event data you will add. Click the Content-Types Builder link in the side nav. tour-app
. To create it, run:ng new tour-app
ng g m core
ng g m features
environments/environment.ts
file, you will add an apiUrl
to the environment
constant. This URL will point to the Strapi server.// src/environments/environment.ts
export const environment = {
production: false,
apiUrl: 'http://localhost:1337/'
};
TourEvent
interface to the model's folder in the core module. You will generate it by running:ng g interface core/models/tour-event
// src/app/core/models/tour-event.ts
export interface TourEvent {
id: string;
city: string;
country: string;
date: Date;
region: string;
ticketsLink: string;
venue: string;
}
HttpClientModule
as an import to AppModule
.// src/app/app.module.ts
import { HttpClientModule } from '@angular/common/http';
...
@NgModule({
...
imports: [
...
HttpClientModule
]
})
export class AppModule { }
ng g s core/services/tour-events
getEvents
method that will hit the http://localhost:1337/tour-events route and return all the events. This service will look like this:// src/app/core/services/tour-events.service.ts
import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { environment } from 'src/environments/environment';
import { TourEvent } from '../models/tour-event';
@Injectable({
providedIn: 'root'
})
export class TourEventsService {
private eventsPath = 'tour-events';
constructor(private http: HttpClient) { }
getEvents(){
return this.http.get<TourEvent[]>(environment.apiUrl+this.eventsPath);
}
}
TourEventsComponent
. This component will serve as the page that will display all the tour events from the service. You will generate it by running:ng g c features/tour-events
TourEventsService
in the constructor and call its getEvents
method and assign the results to the events$
property, which you will use in the template.// src/app/features/tour-events/tour-events.component.ts
import { Component } from '@angular/core';
import { TourEventsService } from 'src/app/core/services/tour-events.service';
@Component({
selector: 'app-tour-events',
templateUrl: './tour-events.component.html',
styleUrls: ['./tour-events.component.css']
})
export class TourEventsComponent {
events$ = this.eventService.getEvents();
constructor(private eventService: TourEventsService) { }
}
<!-- src/app/features/tour-events/tour-events.component.html -->
<h1>Tour Events</h1>
<table *ngIf="events$ | async as events">
<tr>
<th>Date</th>
<th>Venue</th>
<th>City</th>
<th>Time</th>
<th>Tickets</th>
</tr>
<tr *ngFor="let event of events">
<td>{{event.date | date: 'fullDate'}}</td>
<td>{{event.venue | titlecase}}</td>
<td>
<span *ngIf="event.region">{{event.city | titlecase}}, {{event.region | uppercase}} ({{event.country |
uppercase}})</span>
<span *ngIf="!event.region">{{event.city | titlecase}} ({{event.country | uppercase}})</span>
</td>
<td>{{event.date | date: 'shortTime'}}</td>
<td><a href="{{event.ticketsLink}}">Tickets</a></td>
</tr>
</table>
/* src/app/features/tour-events/tour-events.component.css */
* {
font-family: arial, sans-serif;
}
h1 {
text-align: center;
}
table {
border-collapse: collapse;
width: 100%;
}
td, th {
border-bottom: 1px solid lightgray;
text-align: left;
padding: 8px;
}
tr:nth-child(even) {
background-color: #f5f5f5;
}
TourEventsComponent
so you can preview it.// src/app/app-routing.module.ts
...
import { TourEventsComponent } from './tour-events/tour-events.component';
import { Routes, RouterModule } from '@angular/router';
const routes: Routes = [
{ path: '', component: TourEventsComponent}
];
@NgModule({
declarations: [
TourEventsComponent
],
imports: [
...
RouterModule.forChild(routes)
]
})
export class FeaturesModule { }
app.component.html
with just:<!-- src/app/app.component.html -->
<router-outlet></router-outlet>
ng serve
ng add @nguniversal/express-engine
npm run dev:ssr