Skip to content

Commit 5556877

Browse files
authored
Merge pull request #2619 from angular-fullstack/feat/angular-router
Angular Router
2 parents 01b9a11 + 99f4b5c commit 5556877

File tree

14 files changed

+108
-51
lines changed

14 files changed

+108
-51
lines changed

Diff for: src/generators/app/index.js

+10-9
Original file line numberDiff line numberDiff line change
@@ -174,13 +174,13 @@ export class Generator extends Base {
174174
choices: ['CSS', 'Sass', 'Stylus', 'Less'],
175175
filter: val => val.toLowerCase()
176176
}, {
177-
type: 'list',
178-
name: 'router',
179-
default: 1,
180-
message: 'What Angular router would you like to use?',
181-
choices: ['ngRoute', 'uiRouter'],
182-
filter: val => val.toLowerCase()
183-
}, {
177+
// type: 'list',
178+
// name: 'router',
179+
// default: 1,
180+
// message: 'What Angular router would you like to use?',
181+
// choices: ['ngRoute', 'uiRouter'],
182+
// filter: val => val.toLowerCase()
183+
//}, {
184184
type: 'confirm',
185185
name: 'bootstrap',
186186
message: 'Would you like to include Bootstrap?'
@@ -203,8 +203,9 @@ export class Generator extends Base {
203203
this.filters[answers.stylesheet] = true;
204204
insight.track('stylesheet', answers.stylesheet);
205205

206-
this.filters[answers.router] = true;
207-
insight.track('router', answers.router);
206+
//this.filters[answers.router] = true;
207+
//insight.track('router', answers.router);
208+
this.filters['ngroute'] = true;
208209

209210
this.filters.bootstrap = !!answers.bootstrap;
210211
insight.track('bootstrap', !!answers.bootstrap);

Diff for: src/test/fixtures/.yo-rc.json

+2-3
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
"flow": false,
1919
"html": true,
2020
"sass": true,
21-
"uirouter": true,
21+
"ngroute": true,
2222
"bootstrap": true,
2323
"uibootstrap": true,
2424
"ws": true,
@@ -41,11 +41,10 @@
4141
"moduleName": "",
4242
"modulePrompt": true,
4343
"filters": [
44-
"uirouter",
44+
"ngroute",
4545
"mocha",
4646
"expect",
4747
"should",
48-
"uirouter",
4948
"es6",
5049
"webpack"
5150
],

Diff for: src/test/main.test.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ const defaultOptions = {
1919
transpiler: 'babel',
2020
markup: 'html',
2121
stylesheet: 'sass',
22-
router: 'uirouter',
22+
router: 'ngroute',
2323
testing: 'mocha',
2424
chai: 'expect',
2525
bootstrap: true,
@@ -193,7 +193,7 @@ describe('angular-fullstack:app', function() {
193193
transpiler: 'ts',
194194
markup: 'pug',
195195
stylesheet: 'less',
196-
router: 'uirouter',
196+
router: 'ngroute',
197197
testing: 'jasmine',
198198
odms: ['mongoose'],
199199
auth: true,
@@ -265,7 +265,7 @@ describe('angular-fullstack:app', function() {
265265
transpiler: 'babel',
266266
markup: 'pug',
267267
stylesheet: 'css',
268-
router: 'uirouter',
268+
router: 'ngroute',
269269
testing: 'jasmine',
270270
odms: ['sequelize'],
271271
auth: true,
@@ -337,7 +337,7 @@ describe('angular-fullstack:app', function() {
337337
transpiler: 'ts',
338338
markup: 'pug',
339339
stylesheet: 'stylus',
340-
router: 'uirouter',
340+
router: 'ngroute',
341341
testing: 'mocha',
342342
chai: 'should',
343343
odms: [],

Diff for: templates/app/client/__index.html

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
<!doctype html>
22
<html lang="en">
33
<head>
4+
<base href="/">
45
<meta charset="utf-8">
56
<meta http-equiv="x-ua-compatible" content="ie=edge">
6-
<base href="/">
77
<title>Angular Full-Stack Generator</title>
88
<meta name="description" content="">
99
<meta name="viewport" content="width=device-width">
@@ -27,7 +27,7 @@
2727

2828
<app>LOADING</app>
2929
<navbar></navbar>
30-
<% if (filters.ngroute) { %><div ng-view=""></div><% } %><% if (filters.uirouter) { %><div ui-view=""></div><% } %>
30+
<% if (filters.ngroute) { %><router-outlet></router-outlet><% } %><% if (filters.uirouter) { %><div ui-view=""></div><% } %>
3131
<footer></footer>
3232
</body>
3333
</html>

Diff for: templates/app/client/app/account(auth)/account.module.js

+18-3
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,29 @@ import { FormsModule } from '@angular/forms';
33
import { BrowserModule } from '@angular/platform-browser';
44
<%_ if (filters.uirouter) { -%>
55
import { UIRouterModule } from 'ui-router-ng2';<% } %>
6-
<%_ if (filters.ngroute) { -%><% } %>
6+
<%_ if (filters.ngroute) { -%>
7+
import { RouterModule, Routes } from '@angular/router';<% } %>
78
<%_ if(filters.oauth) { -%>
89
import { DirectivesModule } from '../../components/directives.module';<% } %>
910

1011
import { STATES } from './account.routes';
1112

1213
import { LoginComponent } from './login/login.component';
13-
import { SignupComponent } from './signup/signup.component';
1414
import { SettingsComponent } from './settings/settings.component';
15+
import { SignupComponent } from './signup/signup.component';
16+
17+
<%_ if (filters.ngroute) { -%>
18+
const accountRoutes: Routes = [{
19+
path: 'login',
20+
component: LoginComponent,
21+
//data: { title: 'Home' }
22+
}, {
23+
path: 'settings',
24+
component: SettingsComponent,
25+
}, {
26+
path: 'signup',
27+
component: SignupComponent,
28+
}];<% } %>
1529

1630
@NgModule({
1731
imports: [
@@ -21,7 +35,8 @@ import { SettingsComponent } from './settings/settings.component';
2135
UIRouterModule.forChild({
2236
states: STATES,
2337
}),<% } %>
24-
<%_ if (filters.ngroute) { -%><% } %>
38+
<%_ if (filters.ngroute) { -%>
39+
RouterModule.forChild(accountRoutes),<% } %>
2540
<%_ if(filters.oauth) { -%>
2641
DirectivesModule,<% } %>
2742
],

Diff for: templates/app/client/app/account(auth)/login/login.component.js

+10-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { Component } from '@angular/core';
22
<%_ if(filters.uirouter) { -%>
33
import { StateService } from 'ui-router-ng2';<% } %>
4-
<%_ if(filters.ngroute) { -%><% } %>
4+
<%_ if(filters.ngroute) { -%>
5+
import { Router } from '@angular/router';<% } %>
56
import { AuthService } from '../../../components/auth/auth.service';
67

78
// @flow
@@ -37,10 +38,11 @@ export class LoginComponent {
3738
<%_ if(filters.uirouter) { -%>
3839
StateService;<% } %>
3940

40-
static parameters = [AuthService, <% if(filters.ngroute) { %><% } else { %>StateService<% } %>];
41-
constructor(_AuthService_: AuthService, <% if(filters.ngroute) { %><% } else { %>_StateService_: StateService<% } %>) {
41+
static parameters = [AuthService, <% if(filters.ngroute) { %>Router<% } else { %>StateService<% } %>];
42+
constructor(_AuthService_: AuthService, <% if(filters.ngroute) { %>router: Router<% } else { %>_StateService_: StateService<% } %>) {
4243
this.AuthService = _AuthService_;
43-
<%_ if(filters.ngroute) { -%><% } %>
44+
<%_ if(filters.ngroute) { -%>
45+
this.Router = router;<% } %>
4446
<%_ if(filters.uirouter) { -%>
4547
this.StateService = _StateService_;<% } %>
4648
}
@@ -54,7 +56,10 @@ export class LoginComponent {
5456
})
5557
.then(() => {
5658
// Logged in, redirect to home
57-
this.StateService.go('main');
59+
<%_ if(filters.ngroute) { -%>
60+
this.Router.navigateByUrl('/home');<% } %>
61+
<%_ if(filters.uirouter) { -%>
62+
this.StateService.go('main');<% } %>
5863
})
5964
.catch(err => {
6065
this.errors.login = err.message;

Diff for: templates/app/client/app/admin(auth)/admin.module.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,19 @@ import { RouterModule, Routes } from '@angular/router';<% } %>
77

88
import { AdminComponent } from './admin.component';
99

10-
<%_ if(filters.ngroute) { -%>
11-
import { ROUTES } from './admin.routes';<% } %>
1210
<%_ if(filters.uirouter) { -%>
1311
import { STATES } from './admin.routes';<% } %>
12+
<%_ if (filters.ngroute) { -%>
13+
const adminRoutes: Routes = [{
14+
path: 'admin',
15+
component: AdminComponent,
16+
}];<% } %>
1417

1518
@NgModule({
1619
imports: [
1720
BrowserModule,
1821
<%_ if(filters.ngroute) { _%>
19-
RouterModule.forChild(ROUTES),<% } %>
22+
RouterModule.forChild(adminRoutes),<% } %>
2023
<%_ if(filters.uirouter) { _%>
2124
UIRouterModule.forChild({
2225
states: STATES,

Diff for: templates/app/client/app/app.component.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { Component } from '@angular/core';
33
@Component({
44
selector: 'app',
55
template: `<navbar></navbar>
6-
<ui-view></ui-view>
6+
<% if (filters.ngroute) { %><router-outlet></router-outlet><% } %><% if (filters.uirouter) { %><ui-view></ui-view><% } %>
77
<footer></footer>`
88
})
99
export class AppComponent {}

Diff for: templates/app/client/app/app.module.js

+22-1
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,13 @@ import {
6666
} from '@angularclass/hmr';
6767
<%_ if (filters.uirouter) { -%>
6868
import { UIRouterModule } from 'ui-router-ng2';<% } %>
69+
<%_ if (filters.ngroute) { -%>
70+
import { RouterModule, Routes } from '@angular/router';<% } %>
6971
import { provideAuth } from 'angular2-jwt';
7072

7173
import { AppComponent } from './app.component';
7274
import { MainModule } from './main/main.module';
75+
// import { MainComponent } from './main/main.component';
7376
import { DirectivesModule } from '../components/directives.module';
7477
import { AccountModule } from './account/account.module';
7578
import { AdminModule } from './admin/admin.module';
@@ -95,12 +98,30 @@ if(constants.env === 'development') {
9598
providers.push({ provide: RequestOptions, useClass: HttpOptions });
9699
}
97100

101+
const appRoutes: Routes = [
102+
//{ path: 'crisis-center', component: CrisisListComponent },
103+
//{ path: 'hero/:id', component: HeroDetailComponent },
104+
// {
105+
// path: 'home',
106+
// component: MainComponent,
107+
// data: { title: 'Home' }
108+
// },
109+
{ path: '',
110+
redirectTo: '/home',
111+
pathMatch: 'full'
112+
},
113+
//{ path: '**', component: PageNotFoundComponent }
114+
];
115+
98116
@NgModule({
99117
providers,
100118
imports: [
101119
BrowserModule,
102120
HttpModule,
103-
UIRouterModule.forRoot(),
121+
<%_ if (filters.uirouter) { -%>
122+
UIRouterModule.forRoot(),<% } %>
123+
<%_ if (filters.ngroute) { -%>
124+
RouterModule.forRoot(appRoutes, { enableTracing: process.env.NODE_ENV === 'development' }),<% } %>
104125
MainModule,
105126
DirectivesModule,
106127
AccountModule,

Diff for: templates/app/client/app/main/main.module.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import { SocketService } from '../../components/socket/socket.service';<% } %>
1414

1515
<%_ if(filters.ngroute) { _%>
1616
export const ROUTES: Routes = [
17-
{ path: '', component: MainComponent },
17+
{ path: 'home', component: MainComponent },
1818
];<% } %>
1919
<%_ if(filters.uirouter) { _%>
2020
export const STATES = [

Diff for: templates/app/client/components/auth(auth)/auth.service.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ export class AuthService {
164164
* @returns {Promise}
165165
*/
166166
isLoggedIn(callback) {
167-
let is = this.currentUser.hasOwnProperty('role');
167+
let is = !!this.currentUser._id;
168168
safeCb(callback)(is);
169169
return Promise.resolve(is);
170170
}
@@ -174,7 +174,7 @@ export class AuthService {
174174
* @returns {Boolean}
175175
*/
176176
isLoggedInSync() {
177-
return this.currentUser.hasOwnProperty('role');
177+
return !!this.currentUser._id;
178178
}
179179

180180
/**

Diff for: templates/app/client/components/directives.module.js

+9-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
import { NgModule } from '@angular/core';
22
import { CommonModule } from '@angular/common';
3-
import { UIRouterModule } from 'ui-router-ng2';
43
import { CollapseModule } from 'ng2-bootstrap';
54

5+
<%_ if (filters.uirouter) { -%>
6+
import { UIRouterModule } from 'ui-router-ng2';<% } %>
7+
<%_ if (filters.ngroute) { -%>
8+
import { RouterModule } from '@angular/router';<% } %>
9+
610
import { AuthModule } from './auth/auth.module';
711

812
import { NavbarComponent } from './navbar/navbar.component';
@@ -13,9 +17,12 @@ import { OauthButtonsComponent } from './oauth-buttons/oauth-buttons.component';
1317
@NgModule({
1418
imports: [
1519
CommonModule,
16-
UIRouterModule.forChild(),
1720
CollapseModule,
1821
AuthModule,
22+
<%_ if (filters.uirouter) { -%>
23+
UIRouterModule,<% } %>
24+
<%_ if (filters.ngroute) { -%>
25+
RouterModule,<% } %>
1926
],
2027
declarations: [
2128
NavbarComponent,

Diff for: templates/app/client/components/navbar/navbar.component.js

+14-8
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import { Component } from '@angular/core';
22
<%_ if (filters.auth) { -%>
3-
<%_ if (filters.uirouter) { -%>
3+
<%_ if (filters.uirouter) { -%>
44
import { StateService } from 'ui-router-ng2';<% } %>
5+
<%_ if (filters.ngroute) { -%>
6+
import { Router } from '@angular/router';<% } %>
57
import { AuthService } from '../auth/auth.service';<% } %>
68

79
@Component({
@@ -15,21 +17,24 @@ export class NavbarComponent {
1517
currentUser = {};
1618
menu = [{
1719
title: 'Home',
18-
<% if(filters.uirouter) { %>'state': 'main'<% } else { %>'link': '/'<% } %>,
20+
<% if(filters.uirouter) { %>'state': 'main'<% } else { %>'link': '/home'<% } %>,
1921
}];
2022
<%_ if(filters.auth) { -%>
2123

22-
static parameters = [AuthService<% if(filters.uirouter) { %>, StateService<% } %>];
23-
constructor(authService: AuthService<% if(filters.uirouter) { %>, stateService: StateService<% } %>) {
24+
static parameters = [AuthService<% if(filters.uirouter) { %>, StateService<% } else { %>, Router<% } %>];
25+
constructor(authService: AuthService<% if(filters.uirouter) { %>, stateService: StateService<% } else { %>, router: Router<% } %>) {
2426
this.AuthService = authService;
25-
this.StateService = stateService;
27+
<%_ if(filters.uirouter) { -%>
28+
this.StateService = stateService;<% } %>
29+
<%_ if(filters.ngroute) { -%>
30+
this.Router = router;<% } %>
2631

2732
this.reset();
2833

2934
this.AuthService.currentUserChanged.subscribe(user => {
3035
this.currentuser = user;
3136
this.reset();
32-
})
37+
});
3338
}
3439

3540
reset() {
@@ -46,9 +51,10 @@ export class NavbarComponent {
4651

4752
logout() {
4853
let promise = this.AuthService.logout();
49-
<%_ if (filters.uirouter) { -%>
54+
<%_ if(filters.uirouter) { -%>
5055
this.StateService.go('login');<% } -%>
51-
<%_ if (filters.ngroute) { -%><% } -%>
56+
<%_ if(filters.ngroute) { -%>
57+
this.Router.navigateByUrl('/home');<% } -%>
5258
return promise;
5359
}<% } -%>
5460
}

0 commit comments

Comments
 (0)