Skip to content

Commit 0cf12a1

Browse files
committed
use aspnetcore-engine & misc updates and fixes
1 parent b36eca5 commit 0cf12a1

22 files changed

+67
-273
lines changed

ClientApp/app/app.component.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<div class="col-sm-3">
2-
<nav-menu></nav-menu>
2+
<app-nav-menu></app-nav-menu>
33
</div>
44
<div class="col-sm-9 body-content">
55
<router-outlet></router-outlet>

ClientApp/app/app.component.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Component, OnInit, OnDestroy, Inject, ViewEncapsulation, RendererFactory2, PLATFORM_ID } from '@angular/core';
1+
import { Component, OnInit, OnDestroy, Inject, ViewEncapsulation, RendererFactory2, PLATFORM_ID, Injector } from '@angular/core';
22
import { Router, NavigationEnd, ActivatedRoute, PRIMARY_OUTLET } from '@angular/router';
33
import { Meta, Title, DOCUMENT, MetaDefinition } from '@angular/platform-browser';
44
import { Subscription } from 'rxjs/Subscription';
@@ -7,10 +7,10 @@ import { LinkService } from './shared/link.service';
77

88
// i18n support
99
import { TranslateService } from '@ngx-translate/core';
10-
import { REQUEST } from './shared/constants/request';
10+
import { REQUEST } from '@nguniversal/aspnetcore-engine';
1111

1212
@Component({
13-
selector: 'app',
13+
selector: 'app-root',
1414
templateUrl: './app.component.html',
1515
styleUrls: ['./app.component.scss'],
1616
encapsulation: ViewEncapsulation.None
@@ -23,6 +23,7 @@ export class AppComponent implements OnInit, OnDestroy {
2323
private defaultPageTitle: string = 'My App';
2424

2525
private routerSub$: Subscription;
26+
private request;
2627

2728
constructor(
2829
private router: Router,
@@ -31,14 +32,16 @@ export class AppComponent implements OnInit, OnDestroy {
3132
private meta: Meta,
3233
private linkService: LinkService,
3334
public translate: TranslateService,
34-
@Inject(REQUEST) private request
35+
private injector: Injector
3536
) {
3637
// this language will be used as a fallback when a translation isn't found in the current language
3738
translate.setDefaultLang('en');
3839

3940
// the lang to use, if the lang isn't available, it will use the current loader to get them
4041
translate.use('en');
4142

43+
this.request = this.injector.get(REQUEST);
44+
4245
console.log(`What's our REQUEST Object look like?`);
4346
console.log(`The Request object only really exists on the Server, but on the Browser we can at least see Cookies`);
4447
console.log(this.request);

ClientApp/app/app.module.browser.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@ import { BrowserModule } from '@angular/platform-browser';
33
import { APP_BASE_HREF } from '@angular/common';
44
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
55

6-
import { ORIGIN_URL } from './shared/constants/baseurl.constants';
6+
import { ORIGIN_URL, REQUEST } from '@nguniversal/aspnetcore-engine';
77
import { AppModuleShared } from './app.module';
88
import { AppComponent } from './app.component';
9-
import { REQUEST } from './shared/constants/request';
109
import { BrowserTransferStateModule } from '../modules/transfer-state/browser-transfer-state.module';
1110

1211
import { BrowserPrebootModule } from 'preboot/browser';

ClientApp/app/app.module.server.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import { ServerPrebootModule } from 'preboot/server';
1717
appId: 'my-app-id' // make sure this matches with your Browser NgModule
1818
}),
1919
ServerModule,
20-
ServerPrebootModule.recordEvents({ appRoot: 'app' }),
20+
ServerPrebootModule.recordEvents({ appRoot: 'app-root' }),
2121
NoopAnimationsModule,
2222

2323
ServerTransferStateModule,

ClientApp/app/app.module.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import { NgxBootstrapComponent } from './containers/ngx-bootstrap-demo/ngx-boots
2424
import { LinkService } from './shared/link.service';
2525
import { UserService } from './shared/user.service';
2626
// import { ConnectionResolver } from './shared/route.resolver';
27-
import { ORIGIN_URL } from './shared/constants/baseurl.constants';
27+
import { ORIGIN_URL } from '@nguniversal/aspnetcore-engine';
2828
import { TransferHttpModule } from '../modules/transfer-http/transfer-http.module';
2929

3030
export function createTranslateLoader(http: HttpClient, baseHref) {
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
11
import { Component } from '@angular/core';
22

33
@Component({
4-
selector: 'nav-menu',
4+
selector: 'app-nav-menu',
55
templateUrl: './navmenu.component.html',
66
styleUrls: ['./navmenu.component.css']
77
})
88

99
export class NavMenuComponent {
10-
collapse: string = "collapse";
10+
collapse: string = 'collapse';
1111

1212
collapseNavbar(): void {
1313
if (this.collapse.length > 1) {
14-
this.collapse = "";
14+
this.collapse = '';
1515
} else {
16-
this.collapse = "collapse";
16+
this.collapse = 'collapse';
1717
}
1818
}
1919

2020
collapseMenu() {
21-
this.collapse = "collapse"
21+
this.collapse = 'collapse';
2222
}
2323
}

ClientApp/app/components/user-detail/user-detail.component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { IUser } from '../../models/User';
33
import { UserService } from '../../shared/user.service';
44

55
@Component({
6-
selector: 'user-detail',
6+
selector: 'app-user-detail',
77
templateUrl: './user-detail.component.html'
88
})
99
export class UserDetailComponent {

ClientApp/app/containers/counter/counter.component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Component } from '@angular/core';
22

33
@Component({
4-
selector: 'counter',
4+
selector: 'app-counter',
55
templateUrl: './counter.component.html'
66
})
77
export class CounterComponent {

ClientApp/app/containers/lazy/lazy.component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Component } from '@angular/core';
22

33
@Component({
4-
selector: 'lazy-view',
4+
selector: 'app-lazy-view',
55
template: `
66
<h1>Lazy-Loaded Component!</h1>
77
<blockquote>

ClientApp/app/containers/not-found/not-found.component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Component, OnInit } from '@angular/core';
22

33
@Component({
4-
selector: 'not-found',
4+
selector: 'app-not-found',
55
templateUrl: './not-found.component.html'
66
})
77
export class NotFoundComponent implements OnInit {

ClientApp/app/containers/users/users.component.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,4 @@ <h2>Users</h2>
2626
</button>
2727
</li>
2828
</ul>
29-
<user-detail [user]="selectedUser"></user-detail>
29+
<app-user-detail [user]="selectedUser"></app-user-detail>

ClientApp/app/containers/users/users.component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { IUser } from '../../models/User';
77
import { UserService } from '../../shared/user.service';
88

99
@Component({
10-
selector: 'users',
10+
selector: 'app-users',
1111
templateUrl: './users.component.html',
1212
styleUrls: ['./users.component.css'],
1313
animations: [

ClientApp/app/shared/constants/baseurl.constants.ts

Lines changed: 0 additions & 3 deletions
This file was deleted.

ClientApp/app/shared/constants/request.ts

Lines changed: 0 additions & 3 deletions
This file was deleted.

ClientApp/app/shared/user.service.ts

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,46 @@
1-
import { Injectable, Inject } from '@angular/core';
1+
import { Injectable, Inject, Injector } from '@angular/core';
22
import { Http, URLSearchParams } from '@angular/http';
33
import { APP_BASE_HREF } from '@angular/common';
4-
import { ORIGIN_URL } from './constants/baseurl.constants';
4+
import { ORIGIN_URL } from '@nguniversal/aspnetcore-engine';
55
import { IUser } from '../models/User';
66
import { TransferHttp } from '../../modules/transfer-http/transfer-http';
77
import { Observable } from 'rxjs/Observable';
88

99
@Injectable()
1010
export class UserService {
11-
constructor(
12-
private transferHttp: TransferHttp, // Use only for GETS that you want re-used between Server render -> Client render
13-
private http: Http, // Use for everything else
14-
@Inject(ORIGIN_URL) private baseUrl: string) {
1511

16-
}
12+
private baseUrl: string;
1713

18-
getUsers(): Observable<IUser[]> {
19-
// ** TransferHttp example / concept **
20-
// - Here we make an Http call on the server, save the result on the window object and pass it down with the SSR,
21-
// The Client then re-uses this Http result instead of hitting the server again!
14+
constructor(
15+
private transferHttp: TransferHttp, // Use only for GETS that you want re-used between Server render -> Client render
16+
private http: Http, // Use for everything else
17+
private injector: Injector
18+
) {
19+
this.baseUrl = this.injector.get(ORIGIN_URL);
20+
}
2221

23-
// NOTE : transferHttp also automatically does .map(res => res.json()) for you, so no need for these calls
24-
return this.transferHttp.get(`${this.baseUrl}/api/users`);
25-
}
22+
getUsers(): Observable<IUser[]> {
23+
// ** TransferHttp example / concept **
24+
// - Here we make an Http call on the server, save the result on the window object and pass it down with the SSR,
25+
// The Client then re-uses this Http result instead of hitting the server again!
2626

27-
getUser(user: IUser): Observable<IUser> {
28-
return this.transferHttp.get(`${this.baseUrl}/api/users/` + user.id);
29-
}
27+
// NOTE : transferHttp also automatically does .map(res => res.json()) for you, so no need for these calls
28+
return this.transferHttp.get(`${this.baseUrl}/api/users`);
29+
}
3030

31-
deleteUser(user: IUser): Observable<any> {
32-
return this.http.delete(`${this.baseUrl}/api/users/` + user.id);
33-
}
31+
getUser(user: IUser): Observable<IUser> {
32+
return this.transferHttp.get(`${this.baseUrl}/api/users/` + user.id);
33+
}
3434

35-
updateUser(user: IUser): Observable<any> {
36-
return this.http.put(`${this.baseUrl}/api/users/` + user.id, user);
37-
}
35+
deleteUser(user: IUser): Observable<any> {
36+
return this.http.delete(`${this.baseUrl}/api/users/` + user.id);
37+
}
3838

39-
addUser(newUserName: string): Observable<any> {
40-
return this.http.post(`${this.baseUrl}/api/users`, { name: newUserName })
41-
}
39+
updateUser(user: IUser): Observable<any> {
40+
return this.http.put(`${this.baseUrl}/api/users/` + user.id, user);
41+
}
42+
43+
addUser(newUserName: string): Observable<any> {
44+
return this.http.post(`${this.baseUrl}/api/users`, { name: newUserName });
45+
}
4246
}

ClientApp/boot.server.ts

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,24 @@
11
import 'zone.js/dist/zone-node';
22
import './polyfills/server.polyfills';
33
import { enableProdMode } from '@angular/core';
4-
import { INITIAL_CONFIG } from '@angular/platform-server';
5-
import { APP_BASE_HREF } from '@angular/common';
6-
import { createServerRenderer, RenderResult } from 'aspnet-prerendering';
4+
import { createServerRenderer } from 'aspnet-prerendering';
75

8-
import { ORIGIN_URL } from './app/shared/constants/baseurl.constants';
96
// Grab the (Node) server-specific NgModule
107
import { AppModule } from './app/app.module.server';
11-
// Temporary * the engine will be on npm soon (`@universal/ng-aspnetcore-engine`)
12-
import { ngAspnetCoreEngine, IEngineOptions, createTransferScript } from './polyfills/temporary-aspnetcore-engine';
8+
import { ngAspnetCoreEngine, IEngineOptions, createTransferScript } from '@nguniversal/aspnetcore-engine';
139

1410
enableProdMode();
1511

1612
export default createServerRenderer((params) => {
1713

1814
// Platform-server provider configuration
1915
const setupOptions: IEngineOptions = {
20-
appSelector: '<app></app>',
16+
appSelector: '<app-root></app-root>',
2117
ngModule: AppModule,
2218
request: params,
2319
providers: [
24-
// Optional - Any other Server providers you want to pass (remember you'll have to provide them for the Browser as well)
20+
// Optional - Any other Server providers you want to pass
21+
// (remember you'll have to provide them for the Browser as well)
2522
]
2623
};
2724

@@ -34,8 +31,8 @@ export default createServerRenderer((params) => {
3431
});
3532

3633
return ({
37-
html: response.html,
38-
globals: response.globals
34+
html: response.html, // our <app> serialized
35+
globals: response.globals // all of our styles/scripts/meta-tags/link-tags for aspnet to serve up
3936
});
4037
});
4138
});

0 commit comments

Comments
 (0)