Skip to content
This repository was archived by the owner on Apr 8, 2020. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions templates/Angular2Spa/ClientApp/app/app.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { NgModule } from '@angular/core';
import { RouterModule } from '@angular/router';
import { UniversalModule } from 'angular2-universal';
import { AppComponent } from './components/app/app.component'
import { NavMenuComponent } from './components/navmenu/navmenu.component';
import { HomeComponent } from './components/home/home.component';
import { FetchDataComponent } from './components/fetchdata/fetchdata.component';
import { CounterComponent } from './components/counter/counter.component';

@NgModule({
bootstrap: [ AppComponent ],
declarations: [
AppComponent,
NavMenuComponent,
CounterComponent,
FetchDataComponent,
HomeComponent
],
imports: [
UniversalModule,

@MarkPieszak MarkPieszak Sep 19, 2016 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we leave a Note that UniversalModule should be the first Import? (Just want to prevent common errors that can creep up from this)

Also, we should point out that BrowserModule HttpModule JsonpModule are all automatically included here.
(The same goes for the Node versions on the main.server.ts side, NodeModule / NodeHttpModule etc)
Things can error out if they include those. We're working on looking into fixing that.

Patrick also thought it'd be nice to keep the main files separate since that allows someone to have specific platform logic injected for either browser/server, but it's up to you. It's definitely cleaner/simpler this way!

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please keep server and client main files separate.
For me they diverged very early in development. And I am pretty sure it will for most users.

@SteveSandersonMS SteveSandersonMS Sep 19, 2016 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added the comment about UniversalModule (and BrowserModule/HttpModule/JsonpModule)

main files separate

I'd hope that the boot files serve that purpose well enough (this is where you configure DI, after all), and am reluctant to add more duplicate files. Having one root @NgModule makes things much clearer, especially given how much tedious config goes into that file. We can monitor this and see if people often report issues that are best solved by having separate root modules.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we have logic in main file based on platform? Then we don't need 2 files.

if (server) {
declarations: [......]
} else if (client) {
declarations: [.....]
}

?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@hheexx Yes, just import { isNode, isBrowser } from 'angular2-universal'; and then do if (isNode) { ... } etc.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is sweet. I would add it as a comment so people know.

RouterModule.forRoot([
{ path: '', redirectTo: 'home', pathMatch: 'full' },
{ path: 'home', component: HomeComponent },
{ path: 'counter', component: CounterComponent },
{ path: 'fetch-data', component: FetchDataComponent },
{ path: '**', redirectTo: 'home' }
])
]
})
export class AppModule {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { Component } from '@angular/core';

@Component({
selector: 'app',
template: require('./app.component.html')
})
export class AppComponent {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Component } from '@angular/core';

@Component({
selector: 'counter',
template: require('./counter.component.html')
})
export class CounterComponent {
public currentCount = 0;

public incrementCounter() {
this.currentCount++;
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import * as ng from '@angular/core';
import { Component } from '@angular/core';
import { Http } from '@angular/http';

@ng.Component({
selector: 'fetch-data',
template: require('./fetch-data.html')
@Component({
selector: 'fetchdata',
template: require('./fetchdata.component.html')
})
export class FetchData {
export class FetchDataComponent {
public forecasts: WeatherForecast[];

constructor(http: Http) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { Component } from '@angular/core';

@Component({
selector: 'home',
template: require('./home.component.html')
})
export class HomeComponent {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { Component } from '@angular/core';

@Component({
selector: 'nav-menu',
template: require('./navmenu.component.html')
})
export class NavMenuComponent {
}
20 changes: 7 additions & 13 deletions templates/Angular2Spa/ClientApp/boot-client.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,15 @@
import 'angular2-universal-polyfills';
import 'es6-shim';
require('zone.js');
import 'zone.js';
import 'bootstrap';
import 'reflect-metadata';
import './styles/site.css';
import { enableProdMode} from '@angular/core';
import { platformUniversalDynamic } from 'angular2-universal';
import { AppModule } from './app/app.module';

import { bootstrap } from '@angular/platform-browser-dynamic';
import { FormBuilder } from '@angular/common';
import { provideRouter } from '@angular/router';
import { HTTP_PROVIDERS } from '@angular/http';
import { App } from './components/app/app';
import { routes } from './routes';

bootstrap(App, [
...HTTP_PROVIDERS,
FormBuilder,
provideRouter(routes)
]);
enableProdMode();
platformUniversalDynamic().bootstrapModule(AppModule);

// Basic hot reloading support. Automatically reloads and restarts the Angular 2 app each time
// you modify source files. This will not preserve any application state other than the URL.
Expand Down
56 changes: 24 additions & 32 deletions templates/Angular2Spa/ClientApp/boot-server.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,28 @@
import 'angular2-universal/polyfills';
import * as ngCore from '@angular/core';
import { APP_BASE_HREF } from '@angular/common';
import { provideRouter } from '@angular/router';
import * as ngUniversal from 'angular2-universal';
import { BASE_URL, ORIGIN_URL, REQUEST_URL } from 'angular2-universal/common';
import { App } from './components/app/app';
import { routes } from './routes';
import 'angular2-universal-polyfills';
import 'zone.js';
import { enableProdMode } from '@angular/core';
import { platformNodeDynamic } from 'angular2-universal';
import { AppModule } from './app/app.module';

const bootloader = ngUniversal.bootloader({
async: true,
preboot: false,
platformProviders: [
ngCore.provide(APP_BASE_HREF, { useValue: '/' }),
]
});
enableProdMode();
const platform = platformNodeDynamic();

export default function (params: any): Promise<{ html: string, globals?: any }> {
const config: ngUniversal.AppConfig = {
directives: [App],
providers: [
ngCore.provide(ORIGIN_URL, { useValue: params.origin }),
ngCore.provide(REQUEST_URL, { useValue: params.url }),
...ngUniversal.NODE_HTTP_PROVIDERS,
provideRouter(routes),
...ngUniversal.NODE_LOCATION_PROVIDERS,
],
// TODO: Render just the <app> component instead of wrapping it inside an extra HTML document
// Waiting on https://github.com/angular/universal/issues/347
template: '<!DOCTYPE html>\n<html><head></head><body><app></app></body></html>'
};

return bootloader.serializeApplication(config).then(html => {
return { html };
export default function (params: any) : Promise<{ html: string, globals?: any }> {
const requestZone = Zone.current.fork({
name: 'angular-universal request',
properties: {
baseUrl: '/',
requestUrl: params.url,
originUrl: params.origin,
preboot: false,
// TODO: Render just the <app> component instead of wrapping it inside an extra HTML document
// Waiting on https://github.com/angular/universal/issues/347
document: '<!DOCTYPE html><html><head></head><body><app></app></body></html>'
}
});

return requestZone.run<Promise<string>>(() => platform.serializeModule(AppModule))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Much cleaner I like it 👍

.then(html => {
return { html: html };
});
}
11 changes: 0 additions & 11 deletions templates/Angular2Spa/ClientApp/components/app/app.ts

This file was deleted.

13 changes: 0 additions & 13 deletions templates/Angular2Spa/ClientApp/components/counter/counter.ts

This file was deleted.

8 changes: 0 additions & 8 deletions templates/Angular2Spa/ClientApp/components/home/home.ts

This file was deleted.

10 changes: 0 additions & 10 deletions templates/Angular2Spa/ClientApp/components/nav-menu/nav-menu.ts

This file was deleted.

12 changes: 0 additions & 12 deletions templates/Angular2Spa/ClientApp/routes.ts

This file was deleted.

56 changes: 36 additions & 20 deletions templates/Angular2Spa/package.json
Original file line number Diff line number Diff line change
@@ -1,38 +1,54 @@
{
"name": "WebApplicationBasic",
"name": "Angular2Spa",
"version": "0.0.0",
"devDependencies": {
"@types/body-parser": "0.0.29",
"@types/compression": "0.0.29",
"@types/cookie-parser": "^1.3.29",
"@types/express": "^4.0.32",
"@types/express-serve-static-core": "^4.0.33",
"@types/hammerjs": "^2.0.32",
"@types/mime": "0.0.28",
"@types/node": "^6.0.38",
"@types/serve-static": "^1.7.27"
},
"dependencies": {
"@angular/common": "2.0.0-rc.4",
"@angular/compiler": "2.0.0-rc.4",
"@angular/core": "2.0.0-rc.4",
"@angular/http": "2.0.0-rc.4",
"@angular/platform-browser": "2.0.0-rc.4",
"@angular/platform-browser-dynamic": "2.0.0-rc.4",
"@angular/platform-server": "2.0.0-rc.4",
"@angular/router": "3.0.0-beta.2",
"angular2-universal": "^0.104.5",
"aspnet-prerendering": "^1.0.2",
"aspnet-webpack": "^1.0.6",
"bootstrap": "^3.3.6",
"@angular/common": "2.0.0",
"@angular/compiler": "2.0.0",
"@angular/core": "2.0.0",
"@angular/forms": "2.0.0",
"@angular/http": "2.0.0",
"@angular/platform-browser": "2.0.0",
"@angular/platform-browser-dynamic": "2.0.0",
"@angular/platform-server": "2.0.0",
"@angular/router": "3.0.0",
"@types/node": "^6.0.38",
"angular2-express-engine": "~2.0.7",
"angular2-platform-node": "~2.0.7",
"angular2-universal": "~2.0.7",
"angular2-universal-polyfills": "~2.0.7",
"aspnet-prerendering": "^1.0.6",
"aspnet-webpack": "^1.0.11",
"bootstrap": "^3.3.7",
"css": "^2.2.1",
"css-loader": "^0.23.1",
"css-loader": "^0.25.0",
"es6-shim": "^0.35.1",
"expose-loader": "^0.7.1",
"extendify": "^1.0.0",
"extract-text-webpack-plugin": "^1.0.1",
"file-loader": "^0.8.5",
"file-loader": "^0.9.0",
"isomorphic-fetch": "^2.2.1",
"jquery": "^2.2.1",
"preboot": "^2.0.10",
"preboot": "^4.5.2",
"raw-loader": "^0.5.1",
"rxjs": "5.0.0-beta.6",
"rxjs": "5.0.0-beta.12",
"style-loader": "^0.13.0",
"ts-loader": "^0.8.1",
"typescript": "^1.8.2",
"ts-loader": "^0.8.2",
"typescript": "^2.0.0",
"url-loader": "^0.5.7",
"webpack": "^1.12.14",
"webpack-externals-plugin": "^1.0.0",
"webpack-hot-middleware": "^2.10.0",
"zone.js": "^0.6.12"
"zone.js": "^0.6.21"
}
}
10 changes: 5 additions & 5 deletions templates/Angular2Spa/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
"sourceMap": true,
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"skipDefaultLibCheck": true
"skipDefaultLibCheck": true,
"lib": [ "es6", "dom" ],
"types": [ "node" ]
},
"exclude": [
"bin",
"node_modules"
]
"exclude": [ "bin", "node_modules" ],
"atom": { "rewriteTsconfig": false }
}
15 changes: 0 additions & 15 deletions templates/Angular2Spa/tsd.json

This file was deleted.

Loading