From f9d71fdf58df30162fe078cd1b63bb8766120ae1 Mon Sep 17 00:00:00 2001 From: Riceball LEE Date: Sun, 25 Feb 2018 10:24:18 +0800 Subject: [PATCH 1/5] + [feat] add location strategy option --- generators/app/prompts.js | 16 +++++++++++++++ .../app/templates/src/app/_app.module.ts | 20 ++++++++++++++++++- 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/generators/app/prompts.js b/generators/app/prompts.js index d53ef611..220ebd1f 100755 --- a/generators/app/prompts.js +++ b/generators/app/prompts.js @@ -104,5 +104,21 @@ module.exports = [ message: 'Do you want lazy loading?', default: false, when: props => props.ui !== 'ionic' + }, + { + type: 'list', + name: 'location', + message: 'Which location strategy do you want?', + choices: [ + { + value: 'hash', + name: 'Hash(This strategy with base-href "./" allow to move the app to any subsite)' + }, + { + value: 'path', + name: 'Path(Only if passed the --base-href argument at build & the server has url rewrite to index.html)' + } + ], + default: 'path' } ]; diff --git a/generators/app/templates/src/app/_app.module.ts b/generators/app/templates/src/app/_app.module.ts index a7f88248..47bb7f31 100644 --- a/generators/app/templates/src/app/_app.module.ts +++ b/generators/app/templates/src/app/_app.module.ts @@ -4,6 +4,14 @@ import { ErrorHandler, NgModule } from '@angular/core'; <% } else { -%> import { NgModule } from '@angular/core'; <% } -%> +import { + LocationStrategy, +<% if (props.location === 'hash') { -%> + HashLocationStrategy, +<% } else { -%> + PathLocationStrategy, +<% } -%> +} from '@angular/common'; import { FormsModule } from '@angular/forms'; import { HttpClientModule } from '@angular/common/http'; <% if (props.pwa) { -%> @@ -54,7 +62,7 @@ import { AppRoutingModule } from './app-routing.module'; <% } else if (props.ui === 'bootstrap') { -%> NgbModule.forRoot(), <% } else if (props.ui === 'ionic') { -%> - IonicModule.forRoot(AppComponent, { locationStrategy: 'path' }), + IonicModule.forRoot(AppComponent, { locationStrategy: <%= props.location === 'hash' ? 'hash': 'path' %> }), <% } -%> CoreModule, SharedModule, @@ -69,6 +77,16 @@ import { AppRoutingModule } from './app-routing.module'; ], declarations: [AppComponent], providers: [ + { + provide: LocationStrategy, +<% if (props.location === 'hash') { -%> + // This strategy with base-href './' allow to move the app to any subsite and works + useClass: HashLocationStrategy +<% } else { -%> + // Only if passed the --base-href argument at build & the server has url rewrite to index.html + useClass: PathLocationStrategy +<% } -%> + }, <% if (props.ui === 'ionic') { -%> <% if (props.target.includes('cordova')) { -%> { provide: ErrorHandler, useClass: IonicErrorHandler }, From 64d96ca7acc945df5c1a520c226146c3b7744771 Mon Sep 17 00:00:00 2001 From: Riceball LEE Date: Sun, 25 Feb 2018 20:08:02 +0800 Subject: [PATCH 2/5] * [bug] forget add quote to location --- generators/app/templates/src/app/_app.module.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/generators/app/templates/src/app/_app.module.ts b/generators/app/templates/src/app/_app.module.ts index 47bb7f31..b98a5911 100644 --- a/generators/app/templates/src/app/_app.module.ts +++ b/generators/app/templates/src/app/_app.module.ts @@ -62,7 +62,7 @@ import { AppRoutingModule } from './app-routing.module'; <% } else if (props.ui === 'bootstrap') { -%> NgbModule.forRoot(), <% } else if (props.ui === 'ionic') { -%> - IonicModule.forRoot(AppComponent, { locationStrategy: <%= props.location === 'hash' ? 'hash': 'path' %> }), + IonicModule.forRoot(AppComponent, { locationStrategy: <%- props.location === 'hash' ? "'hash'": "'path'" %> }), <% } -%> CoreModule, SharedModule, From 8528be19d46c262bd36842b36e11d2a992d6c9b1 Mon Sep 17 00:00:00 2001 From: Riceball LEE Date: Thu, 1 Mar 2018 13:59:58 +0800 Subject: [PATCH 3/5] * update @angular/cli@1.7.2 to avoid the material compiler error --- generators/app/templates/_package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/generators/app/templates/_package.json b/generators/app/templates/_package.json index a599b30c..76e3512b 100644 --- a/generators/app/templates/_package.json +++ b/generators/app/templates/_package.json @@ -79,7 +79,7 @@ <% if (props.target.includes('cordova')) { -%> "cordova": "^8.0.0", <% } -%> - "@angular/cli": "~1.7.0", + "@angular/cli": "~1.7.2", "@angular/compiler-cli": "^5.2.5", "@angular/language-service": "^5.2.5", "@biesbjerg/ngx-translate-extract": "^2.3.1", From 82b9f3b78a9d10ee0ace1de6b58e6e136986d504 Mon Sep 17 00:00:00 2001 From: Riceball LEE Date: Thu, 1 Mar 2018 20:20:17 +0800 Subject: [PATCH 4/5] * do not import LocationStrategy for inonic ui --- generators/app/templates/src/app/_app.module.ts | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/generators/app/templates/src/app/_app.module.ts b/generators/app/templates/src/app/_app.module.ts index b98a5911..3bd9e179 100644 --- a/generators/app/templates/src/app/_app.module.ts +++ b/generators/app/templates/src/app/_app.module.ts @@ -3,7 +3,6 @@ import { BrowserModule } from '@angular/platform-browser'; import { ErrorHandler, NgModule } from '@angular/core'; <% } else { -%> import { NgModule } from '@angular/core'; -<% } -%> import { LocationStrategy, <% if (props.location === 'hash') { -%> @@ -12,6 +11,7 @@ import { PathLocationStrategy, <% } -%> } from '@angular/common'; +<% } -%> import { FormsModule } from '@angular/forms'; import { HttpClientModule } from '@angular/common/http'; <% if (props.pwa) { -%> @@ -77,16 +77,18 @@ import { AppRoutingModule } from './app-routing.module'; ], declarations: [AppComponent], providers: [ +<% if (props.ui !== 'ionic') { -%> { provide: LocationStrategy, -<% if (props.location === 'hash') { -%> +<% if (props.location === 'hash') { -%> // This strategy with base-href './' allow to move the app to any subsite and works useClass: HashLocationStrategy <% } else { -%> // Only if passed the --base-href argument at build & the server has url rewrite to index.html useClass: PathLocationStrategy -<% } -%> +<% } -%> }, +<% } -%> <% if (props.ui === 'ionic') { -%> <% if (props.target.includes('cordova')) { -%> { provide: ErrorHandler, useClass: IonicErrorHandler }, From 4b76a7d59d17df0ecad1f727385984665f8a196d Mon Sep 17 00:00:00 2001 From: Riceball LEE Date: Thu, 1 Mar 2018 20:20:58 +0800 Subject: [PATCH 5/5] * use the location-strategy option and hidden the prompt. --- generators/app/index.js | 5 +++++ generators/app/options.json | 7 +++++++ generators/app/prompts.js | 16 ---------------- 3 files changed, 12 insertions(+), 16 deletions(-) diff --git a/generators/app/index.js b/generators/app/index.js index 83381f5d..0e6d462b 100755 --- a/generators/app/index.js +++ b/generators/app/index.js @@ -26,6 +26,11 @@ class NgxGenerator extends Generator { this.props = {ui: 'raw'}; } + if (this.options['location-strategy']) { + this.props = this.config.get('props') || {}; + this.props.location = this.options['location-strategy']; + } + // Updating let fromVersion = null; diff --git a/generators/app/options.json b/generators/app/options.json index 3b05b23f..83243276 100755 --- a/generators/app/options.json +++ b/generators/app/options.json @@ -33,5 +33,12 @@ "required": false, "description": "Do not use any UI library", "defaults": false + }, + { + "name": "location-strategy", + "type": "String", + "required": false, + "description": "The location strategies in angular router: path/hash", + "defaults": "path" } ] diff --git a/generators/app/prompts.js b/generators/app/prompts.js index 220ebd1f..d53ef611 100755 --- a/generators/app/prompts.js +++ b/generators/app/prompts.js @@ -104,21 +104,5 @@ module.exports = [ message: 'Do you want lazy loading?', default: false, when: props => props.ui !== 'ionic' - }, - { - type: 'list', - name: 'location', - message: 'Which location strategy do you want?', - choices: [ - { - value: 'hash', - name: 'Hash(This strategy with base-href "./" allow to move the app to any subsite)' - }, - { - value: 'path', - name: 'Path(Only if passed the --base-href argument at build & the server has url rewrite to index.html)' - } - ], - default: 'path' } ];