├── .editorconfig ├── .gitignore ├── LICENSE ├── README.md ├── angular.json ├── e2e ├── protractor.conf.js ├── src │ ├── app.e2e-spec.ts │ └── app.po.ts └── tsconfig.e2e.json ├── package-lock.json ├── package.json ├── src ├── app │ ├── app-routing.module.ts │ ├── app.component.html │ ├── app.component.scss │ ├── app.component.spec.ts │ ├── app.component.ts │ ├── app.module.ts │ ├── components │ │ ├── host-a │ │ │ ├── host-a.component.html │ │ │ ├── host-a.component.scss │ │ │ ├── host-a.component.spec.ts │ │ │ └── host-a.component.ts │ │ └── host-b │ │ │ ├── host-b.component.html │ │ │ ├── host-b.component.scss │ │ │ ├── host-b.component.spec.ts │ │ │ └── host-b.component.ts │ ├── element-routing-module.ts │ ├── host-location.service.ts │ ├── multi-location-strategy.ts │ ├── services │ │ └── lazy-module.service.ts │ └── web-components │ │ ├── element-a │ │ ├── components │ │ │ ├── home │ │ │ │ ├── home.component.html │ │ │ │ ├── home.component.scss │ │ │ │ ├── home.component.spec.ts │ │ │ │ └── home.component.ts │ │ │ ├── picture │ │ │ │ ├── picture.component.html │ │ │ │ ├── picture.component.scss │ │ │ │ ├── picture.component.spec.ts │ │ │ │ └── picture.component.ts │ │ │ └── profile │ │ │ │ ├── profile.component.html │ │ │ │ ├── profile.component.scss │ │ │ │ ├── profile.component.spec.ts │ │ │ │ └── profile.component.ts │ │ ├── element-a-routing.module.ts │ │ ├── element-a.component.html │ │ ├── element-a.component.scss │ │ ├── element-a.component.spec.ts │ │ ├── element-a.component.ts │ │ └── element-a.module.ts │ │ └── element-b │ │ ├── components │ │ ├── home │ │ │ ├── home.component.html │ │ │ ├── home.component.scss │ │ │ ├── home.component.spec.ts │ │ │ └── home.component.ts │ │ ├── picture │ │ │ ├── picture.component.html │ │ │ ├── picture.component.scss │ │ │ ├── picture.component.spec.ts │ │ │ └── picture.component.ts │ │ └── profile │ │ │ ├── profile.component.html │ │ │ ├── profile.component.scss │ │ │ ├── profile.component.spec.ts │ │ │ └── profile.component.ts │ │ ├── element-b-routing.module.ts │ │ ├── element-b.component.html │ │ ├── element-b.component.scss │ │ ├── element-b.component.spec.ts │ │ ├── element-b.component.ts │ │ └── element-b.module.ts ├── assets │ └── .gitkeep ├── browserslist ├── environments │ ├── environment.prod.ts │ └── environment.ts ├── favicon.ico ├── index.html ├── karma.conf.js ├── main.ts ├── polyfills.ts ├── styles.scss ├── test.ts ├── tsconfig.app.json ├── tsconfig.spec.json └── tslint.json ├── tsconfig.json └── tslint.json /.editorconfig: -------------------------------------------------------------------------------- 1 | # Editor configuration, see https://editorconfig.org 2 | root = true 3 | 4 | [*] 5 | charset = utf-8 6 | indent_style = space 7 | indent_size = 2 8 | insert_final_newline = true 9 | trim_trailing_whitespace = true 10 | 11 | [*.md] 12 | max_line_length = off 13 | trim_trailing_whitespace = false 14 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # See http://help.github.com/ignore-files/ for more about ignoring files. 2 | 3 | # compiled output 4 | /dist 5 | /tmp 6 | /out-tsc 7 | # Only exists if Bazel was run 8 | /bazel-out 9 | 10 | # dependencies 11 | /node_modules 12 | 13 | # profiling files 14 | chrome-profiler-events.json 15 | speed-measure-plugin.json 16 | 17 | # IDEs and editors 18 | /.idea 19 | .project 20 | .classpath 21 | .c9/ 22 | *.launch 23 | .settings/ 24 | *.sublime-workspace 25 | 26 | # IDE - VSCode 27 | .vscode/* 28 | !.vscode/settings.json 29 | !.vscode/tasks.json 30 | !.vscode/launch.json 31 | !.vscode/extensions.json 32 | .history/* 33 | 34 | # misc 35 | /.sass-cache 36 | /connect.lock 37 | /coverage 38 | /libpeerconnection.log 39 | npm-debug.log 40 | yarn-error.log 41 | testem.log 42 | /typings 43 | 44 | # System Files 45 | .DS_Store 46 | Thumbs.db 47 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Timon Graßl 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Angular Elements Routing 2 | This project demonstrates how to use routing inside angular web components 3 | 4 | ![Angular Routing](https://media.giphy.com/media/Md53KZwwrH7HKYmPYa/giphy.gif) 5 | 6 | ## Development server 7 | Run `npm start` for a dev server. Navigate to `http://localhost:4200/`. 8 | -------------------------------------------------------------------------------- /angular.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "./node_modules/@angular/cli/lib/config/schema.json", 3 | "version": 1, 4 | "newProjectRoot": "projects", 5 | "projects": { 6 | "webcomponent-router": { 7 | "root": "", 8 | "sourceRoot": "src", 9 | "projectType": "application", 10 | "prefix": "", 11 | "schematics": { 12 | "@schematics/angular:component": { 13 | "style": "scss" 14 | } 15 | }, 16 | "architect": { 17 | "build": { 18 | "builder": "@angular-devkit/build-angular:browser", 19 | "options": { 20 | "outputPath": "dist/webcomponent-router", 21 | "index": "src/index.html", 22 | "main": "src/main.ts", 23 | "polyfills": "src/polyfills.ts", 24 | "tsConfig": "src/tsconfig.app.json", 25 | "lazyModules": [ 26 | "src/app/web-components/element-a/element-a.module", 27 | "src/app/web-components/element-b/element-b.module" 28 | ], 29 | "assets": [ 30 | "src/favicon.ico", 31 | "src/assets" 32 | ], 33 | "styles": [ 34 | "src/styles.scss" 35 | ], 36 | "scripts": [], 37 | "es5BrowserSupport": true 38 | }, 39 | "configurations": { 40 | "production": { 41 | "fileReplacements": [ 42 | { 43 | "replace": "src/environments/environment.ts", 44 | "with": "src/environments/environment.prod.ts" 45 | } 46 | ], 47 | "optimization": true, 48 | "outputHashing": "all", 49 | "sourceMap": false, 50 | "extractCss": true, 51 | "namedChunks": false, 52 | "aot": true, 53 | "extractLicenses": true, 54 | "vendorChunk": false, 55 | "buildOptimizer": true, 56 | "budgets": [ 57 | { 58 | "type": "initial", 59 | "maximumWarning": "2mb", 60 | "maximumError": "5mb" 61 | } 62 | ] 63 | } 64 | } 65 | }, 66 | "serve": { 67 | "builder": "@angular-devkit/build-angular:dev-server", 68 | "options": { 69 | "browserTarget": "webcomponent-router:build" 70 | }, 71 | "configurations": { 72 | "production": { 73 | "browserTarget": "webcomponent-router:build:production" 74 | } 75 | } 76 | }, 77 | "extract-i18n": { 78 | "builder": "@angular-devkit/build-angular:extract-i18n", 79 | "options": { 80 | "browserTarget": "webcomponent-router:build" 81 | } 82 | }, 83 | "test": { 84 | "builder": "@angular-devkit/build-angular:karma", 85 | "options": { 86 | "main": "src/test.ts", 87 | "polyfills": "src/polyfills.ts", 88 | "tsConfig": "src/tsconfig.spec.json", 89 | "karmaConfig": "src/karma.conf.js", 90 | "styles": [ 91 | "src/styles.scss" 92 | ], 93 | "scripts": [], 94 | "assets": [ 95 | "src/favicon.ico", 96 | "src/assets" 97 | ] 98 | } 99 | }, 100 | "lint": { 101 | "builder": "@angular-devkit/build-angular:tslint", 102 | "options": { 103 | "tsConfig": [ 104 | "src/tsconfig.app.json", 105 | "src/tsconfig.spec.json" 106 | ], 107 | "exclude": [ 108 | "**/node_modules/**" 109 | ] 110 | } 111 | } 112 | } 113 | }, 114 | "webcomponent-router-e2e": { 115 | "root": "e2e/", 116 | "projectType": "application", 117 | "prefix": "", 118 | "architect": { 119 | "e2e": { 120 | "builder": "@angular-devkit/build-angular:protractor", 121 | "options": { 122 | "protractorConfig": "e2e/protractor.conf.js", 123 | "devServerTarget": "webcomponent-router:serve" 124 | }, 125 | "configurations": { 126 | "production": { 127 | "devServerTarget": "webcomponent-router:serve:production" 128 | } 129 | } 130 | }, 131 | "lint": { 132 | "builder": "@angular-devkit/build-angular:tslint", 133 | "options": { 134 | "tsConfig": "e2e/tsconfig.e2e.json", 135 | "exclude": [ 136 | "**/node_modules/**" 137 | ] 138 | } 139 | } 140 | } 141 | } 142 | }, 143 | "defaultProject": "webcomponent-router" 144 | } -------------------------------------------------------------------------------- /e2e/protractor.conf.js: -------------------------------------------------------------------------------- 1 | // Protractor configuration file, see link for more information 2 | // https://github.com/angular/protractor/blob/master/lib/config.ts 3 | 4 | const { SpecReporter } = require('jasmine-spec-reporter'); 5 | 6 | exports.config = { 7 | allScriptsTimeout: 11000, 8 | specs: [ 9 | './src/**/*.e2e-spec.ts' 10 | ], 11 | capabilities: { 12 | 'browserName': 'chrome' 13 | }, 14 | directConnect: true, 15 | baseUrl: 'http://localhost:4200/', 16 | framework: 'jasmine', 17 | jasmineNodeOpts: { 18 | showColors: true, 19 | defaultTimeoutInterval: 30000, 20 | print: function() {} 21 | }, 22 | onPrepare() { 23 | require('ts-node').register({ 24 | project: require('path').join(__dirname, './tsconfig.e2e.json') 25 | }); 26 | jasmine.getEnv().addReporter(new SpecReporter({ spec: { displayStacktrace: true } })); 27 | } 28 | }; -------------------------------------------------------------------------------- /e2e/src/app.e2e-spec.ts: -------------------------------------------------------------------------------- 1 | import { AppPage } from './app.po'; 2 | import { browser, logging } from 'protractor'; 3 | 4 | describe('workspace-project App', () => { 5 | let page: AppPage; 6 | 7 | beforeEach(() => { 8 | page = new AppPage(); 9 | }); 10 | 11 | it('should display welcome message', () => { 12 | page.navigateTo(); 13 | expect(page.getTitleText()).toEqual('Welcome to webcomponent-router!'); 14 | }); 15 | 16 | afterEach(async () => { 17 | // Assert that there are no errors emitted from the browser 18 | const logs = await browser.manage().logs().get(logging.Type.BROWSER); 19 | expect(logs).not.toContain(jasmine.objectContaining({ 20 | level: logging.Level.SEVERE, 21 | } as logging.Entry)); 22 | }); 23 | }); 24 | -------------------------------------------------------------------------------- /e2e/src/app.po.ts: -------------------------------------------------------------------------------- 1 | import { browser, by, element } from 'protractor'; 2 | 3 | export class AppPage { 4 | navigateTo() { 5 | return browser.get(browser.baseUrl) as Promise; 6 | } 7 | 8 | getTitleText() { 9 | return element(by.css('app-root h1')).getText() as Promise; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /e2e/tsconfig.e2e.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "../tsconfig.json", 3 | "compilerOptions": { 4 | "outDir": "../out-tsc/app", 5 | "module": "commonjs", 6 | "target": "es5", 7 | "types": [ 8 | "jasmine", 9 | "jasminewd2", 10 | "node" 11 | ] 12 | } 13 | } -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "webcomponent-router", 3 | "version": "0.0.0", 4 | "scripts": { 5 | "ng": "ng", 6 | "start": "ng serve", 7 | "build": "ng build", 8 | "test": "ng test", 9 | "lint": "ng lint", 10 | "e2e": "ng e2e" 11 | }, 12 | "private": true, 13 | "dependencies": { 14 | "@angular/animations": "~7.2.0", 15 | "@angular/common": "~7.2.0", 16 | "@angular/compiler": "~7.2.0", 17 | "@angular/core": "~7.2.0", 18 | "@angular/forms": "~7.2.0", 19 | "@angular/platform-browser": "~7.2.0", 20 | "@angular/platform-browser-dynamic": "~7.2.0", 21 | "@angular/router": "~7.2.0", 22 | "@webcomponents/webcomponentsjs": "^2.2.10", 23 | "core-js": "^2.5.4", 24 | "document-register-element": "^1.13.2", 25 | "rxjs": "~6.3.3", 26 | "tslib": "^1.9.0", 27 | "zone.js": "~0.8.26" 28 | }, 29 | "devDependencies": { 30 | "@angular-devkit/build-angular": "~0.13.0", 31 | "@angular/cli": "~7.3.8", 32 | "@angular/compiler-cli": "~7.2.0", 33 | "@angular/elements": "^7.2.15", 34 | "@angular/language-service": "~7.2.0", 35 | "@types/jasmine": "~2.8.8", 36 | "@types/jasminewd2": "~2.0.3", 37 | "@types/node": "~8.9.4", 38 | "codelyzer": "~4.5.0", 39 | "jasmine-core": "~2.99.1", 40 | "jasmine-spec-reporter": "~4.2.1", 41 | "karma": "~4.0.0", 42 | "karma-chrome-launcher": "~2.2.0", 43 | "karma-coverage-istanbul-reporter": "~2.0.1", 44 | "karma-jasmine": "~1.1.2", 45 | "karma-jasmine-html-reporter": "^0.2.2", 46 | "protractor": "~5.4.0", 47 | "ts-node": "~7.0.0", 48 | "tslint": "~5.11.0", 49 | "typescript": "~3.2.2" 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /src/app/app-routing.module.ts: -------------------------------------------------------------------------------- 1 | import { HostBComponent } from './components/host-b/host-b.component'; 2 | import { HostAComponent } from './components/host-a/host-a.component'; 3 | import { NgModule } from '@angular/core'; 4 | import { Routes, RouterModule } from '@angular/router'; 5 | import { LocationStrategy } from '@angular/common'; 6 | import { MultiLocationStrategy } from './multi-location-strategy'; 7 | 8 | const routes: Routes = [ 9 | { path: 'host-a', component: HostAComponent }, 10 | { path: 'host-b', component: HostBComponent }, 11 | ]; 12 | 13 | @NgModule({ 14 | imports: [RouterModule.forRoot(routes)], 15 | exports: [RouterModule] 16 | }) 17 | export class AppRoutingModule { } 18 | -------------------------------------------------------------------------------- /src/app/app.component.html: -------------------------------------------------------------------------------- 1 | 2 |
3 |

4 | WebComponents using routing 5 |

6 |
7 | 8 | 9 | -------------------------------------------------------------------------------- /src/app/app.component.scss: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tgrassl/angular-elements-routing/dfb4eadfcd3440936679b64b89ce75f00374542e/src/app/app.component.scss -------------------------------------------------------------------------------- /src/app/app.component.spec.ts: -------------------------------------------------------------------------------- 1 | import { TestBed, async } from '@angular/core/testing'; 2 | import { RouterTestingModule } from '@angular/router/testing'; 3 | import { AppComponent } from './app.component'; 4 | 5 | describe('AppComponent', () => { 6 | beforeEach(async(() => { 7 | TestBed.configureTestingModule({ 8 | imports: [ 9 | RouterTestingModule 10 | ], 11 | declarations: [ 12 | AppComponent 13 | ], 14 | }).compileComponents(); 15 | })); 16 | 17 | it('should create the app', () => { 18 | const fixture = TestBed.createComponent(AppComponent); 19 | const app = fixture.debugElement.componentInstance; 20 | expect(app).toBeTruthy(); 21 | }); 22 | }); 23 | -------------------------------------------------------------------------------- /src/app/app.component.ts: -------------------------------------------------------------------------------- 1 | import { Component, OnInit } from '@angular/core'; 2 | import { LazyModuleService } from './services/lazy-module.service'; 3 | import { Router, NavigationError, NavigationStart } from '@angular/router'; 4 | import {Location} from '@angular/common'; 5 | import { HostLocationService } from './host-location.service'; 6 | 7 | @Component({ 8 | selector: 'app-root', 9 | templateUrl: './app.component.html', 10 | styleUrls: ['./app.component.scss'] 11 | }) 12 | export class AppComponent implements OnInit { 13 | 14 | constructor(private loader: LazyModuleService, private hostLocation: HostLocationService) { } 15 | 16 | ngOnInit() { 17 | this.loader.load(false); 18 | this.hostLocation.handleNavigation(); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/app/app.module.ts: -------------------------------------------------------------------------------- 1 | import { BrowserModule } from '@angular/platform-browser'; 2 | import { NgModule, CUSTOM_ELEMENTS_SCHEMA, NgModuleFactoryLoader, SystemJsNgModuleLoader } from '@angular/core'; 3 | import {Location} from '@angular/common'; 4 | 5 | import { AppRoutingModule } from './app-routing.module'; 6 | import { AppComponent } from './app.component'; 7 | import { HostAComponent } from './components/host-a/host-a.component'; 8 | import { HostBComponent } from './components/host-b/host-b.component'; 9 | import { RouterModule } from '@angular/router'; 10 | 11 | @NgModule({ 12 | declarations: [ 13 | AppComponent, 14 | HostAComponent, 15 | HostBComponent, 16 | ], 17 | imports: [ 18 | BrowserModule, 19 | AppRoutingModule 20 | ], 21 | schemas: [CUSTOM_ELEMENTS_SCHEMA], 22 | bootstrap: [AppComponent], 23 | providers: [ 24 | { provide: NgModuleFactoryLoader, useClass: SystemJsNgModuleLoader } 25 | ], 26 | }) 27 | export class AppModule { } 28 | -------------------------------------------------------------------------------- /src/app/components/host-a/host-a.component.html: -------------------------------------------------------------------------------- 1 |

2 | host-a works! 3 |

4 | 5 | 6 | -------------------------------------------------------------------------------- /src/app/components/host-a/host-a.component.scss: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tgrassl/angular-elements-routing/dfb4eadfcd3440936679b64b89ce75f00374542e/src/app/components/host-a/host-a.component.scss -------------------------------------------------------------------------------- /src/app/components/host-a/host-a.component.spec.ts: -------------------------------------------------------------------------------- 1 | import { async, ComponentFixture, TestBed } from '@angular/core/testing'; 2 | 3 | import { HostAComponent } from './host-a.component'; 4 | 5 | describe('HostAComponent', () => { 6 | let component: HostAComponent; 7 | let fixture: ComponentFixture; 8 | 9 | beforeEach(async(() => { 10 | TestBed.configureTestingModule({ 11 | declarations: [ HostAComponent ] 12 | }) 13 | .compileComponents(); 14 | })); 15 | 16 | beforeEach(() => { 17 | fixture = TestBed.createComponent(HostAComponent); 18 | component = fixture.componentInstance; 19 | fixture.detectChanges(); 20 | }); 21 | 22 | it('should create', () => { 23 | expect(component).toBeTruthy(); 24 | }); 25 | }); 26 | -------------------------------------------------------------------------------- /src/app/components/host-a/host-a.component.ts: -------------------------------------------------------------------------------- 1 | import { Component, OnInit } from '@angular/core'; 2 | 3 | @Component({ 4 | selector: 'host-a', 5 | templateUrl: './host-a.component.html', 6 | styleUrls: ['./host-a.component.scss'] 7 | }) 8 | export class HostAComponent implements OnInit { 9 | 10 | constructor() { } 11 | 12 | ngOnInit() { 13 | } 14 | 15 | } 16 | -------------------------------------------------------------------------------- /src/app/components/host-b/host-b.component.html: -------------------------------------------------------------------------------- 1 |

2 | host-b works! 3 |

4 | 5 | 6 | -------------------------------------------------------------------------------- /src/app/components/host-b/host-b.component.scss: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tgrassl/angular-elements-routing/dfb4eadfcd3440936679b64b89ce75f00374542e/src/app/components/host-b/host-b.component.scss -------------------------------------------------------------------------------- /src/app/components/host-b/host-b.component.spec.ts: -------------------------------------------------------------------------------- 1 | import { async, ComponentFixture, TestBed } from '@angular/core/testing'; 2 | 3 | import { HostBComponent } from './host-b.component'; 4 | 5 | describe('HostBComponent', () => { 6 | let component: HostBComponent; 7 | let fixture: ComponentFixture; 8 | 9 | beforeEach(async(() => { 10 | TestBed.configureTestingModule({ 11 | declarations: [ HostBComponent ] 12 | }) 13 | .compileComponents(); 14 | })); 15 | 16 | beforeEach(() => { 17 | fixture = TestBed.createComponent(HostBComponent); 18 | component = fixture.componentInstance; 19 | fixture.detectChanges(); 20 | }); 21 | 22 | it('should create', () => { 23 | expect(component).toBeTruthy(); 24 | }); 25 | }); 26 | -------------------------------------------------------------------------------- /src/app/components/host-b/host-b.component.ts: -------------------------------------------------------------------------------- 1 | import { Component, OnInit } from '@angular/core'; 2 | 3 | @Component({ 4 | selector: 'host-b', 5 | templateUrl: './host-b.component.html', 6 | styleUrls: ['./host-b.component.scss'] 7 | }) 8 | export class HostBComponent implements OnInit { 9 | 10 | constructor() { } 11 | 12 | ngOnInit() { 13 | } 14 | 15 | } 16 | -------------------------------------------------------------------------------- /src/app/element-routing-module.ts: -------------------------------------------------------------------------------- 1 | import { MultiLocationStrategy } from './multi-location-strategy'; 2 | import { NgModule, NgModuleFactoryLoader, Compiler, Injector, Optional, ModuleWithProviders, SystemJsNgModuleLoader } from '@angular/core'; 3 | import { RouterModule, Router, UrlSerializer, ChildrenOutletContexts, ROUTES, ROUTER_CONFIGURATION, 4 | UrlHandlingStrategy, PreloadingStrategy, NoPreloading, provideRoutes, Routes, ExtraOptions, Route } from '@angular/router'; 5 | import { ɵROUTER_PROVIDERS as ROUTER_PROVIDERS } from '@angular/router'; 6 | import { LocationStrategy, Location} from '@angular/common'; 7 | import { flatten } from '@angular/compiler'; 8 | 9 | export function setupElementsRouter(urlSerializer: UrlSerializer, contexts: ChildrenOutletContexts, location: Location, 10 | loader: NgModuleFactoryLoader, compiler: Compiler, injector: Injector, 11 | routes: Route[][], opts?: ExtraOptions, 12 | urlHandlingStrategy?: UrlHandlingStrategy): Router { 13 | // tslint:disable-next-line:no-non-null-assertion 14 | return new Router(null!, urlSerializer, contexts, location, injector, loader, compiler, flatten(routes)); 15 | } 16 | 17 | @NgModule({ 18 | exports: [RouterModule], 19 | providers: [ 20 | ROUTER_PROVIDERS, {provide: Location, useClass: Location}, 21 | {provide: LocationStrategy, useClass: MultiLocationStrategy}, 22 | { provide: NgModuleFactoryLoader, useClass: SystemJsNgModuleLoader }, { 23 | provide: Router, 24 | useFactory: setupElementsRouter, 25 | deps: [ 26 | UrlSerializer, ChildrenOutletContexts, Location, NgModuleFactoryLoader, Compiler, Injector, 27 | ROUTES, ROUTER_CONFIGURATION, [UrlHandlingStrategy, new Optional()] 28 | ] 29 | }, 30 | {provide: PreloadingStrategy, useExisting: NoPreloading}, provideRoutes([]) 31 | ] 32 | }) 33 | export class ElementRoutingModule { 34 | static withRoutes(routes: Routes, config?: ExtraOptions): 35 | ModuleWithProviders { 36 | return { 37 | ngModule: ElementRoutingModule, 38 | providers: [ 39 | provideRoutes(routes), 40 | {provide: ROUTER_CONFIGURATION, useValue: config ? config : {}}, 41 | ] 42 | }; 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/app/host-location.service.ts: -------------------------------------------------------------------------------- 1 | import { Injectable } from '@angular/core'; 2 | import { Router, NavigationStart } from '@angular/router'; 3 | 4 | @Injectable({ 5 | providedIn: 'root' 6 | }) 7 | export class HostLocationService { 8 | 9 | constructor(private router: Router) { } 10 | 11 | public handleNavigation() { 12 | this.router.events.subscribe((val) => { 13 | if (val instanceof NavigationStart) { 14 | 15 | if (val.id === 1 && val.navigationTrigger === 'imperative' || val.navigationTrigger === 'popstate') { 16 | 17 | // Get URL from current Route 18 | const fullRouteUrl = val.url; 19 | 20 | // Remove outlet from url 21 | const routeWithoutOutlet = fullRouteUrl.replace(/ *\([^)]*\) */g, ''); 22 | 23 | // Navigate to host route and replace location history with fullRouteUrl 24 | this.router.navigateByUrl(routeWithoutOutlet).then(() => { 25 | window.history.pushState({}, null, fullRouteUrl); 26 | }); 27 | } 28 | } 29 | }); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /src/app/multi-location-strategy.ts: -------------------------------------------------------------------------------- 1 | import {Inject, Injectable, Optional} from '@angular/core'; 2 | import { LocationStrategy, Location, PlatformLocation, APP_BASE_HREF, LocationChangeListener, PathLocationStrategy } from '@angular/common'; 3 | 4 | // TODO: Fix location change on navigation arrow click, increased navigationId and routing depth > 1 5 | @Injectable() 6 | export class MultiLocationStrategy extends LocationStrategy { 7 | private _baseHref: string; 8 | 9 | constructor( 10 | private _platformLocation: PlatformLocation, 11 | @Optional() @Inject(APP_BASE_HREF) href?: string) { 12 | super(); 13 | 14 | if (href == null) { 15 | href = this._platformLocation.getBaseHrefFromDOM(); 16 | } 17 | 18 | if (href == null) { 19 | throw new Error( 20 | `No base href set. Please provide a value for the APP_BASE_HREF token or add a base element to the document.`); 21 | } 22 | 23 | this._baseHref = href; 24 | } 25 | 26 | onPopState(fn: LocationChangeListener): void { 27 | this._platformLocation.onPopState(fn); 28 | this._platformLocation.onHashChange(fn); 29 | } 30 | 31 | getBaseHref(): string { return this._baseHref; } 32 | 33 | prepareExternalUrl(internal: string, state?: any): string { 34 | return this.checkPathAndInternal(this.path(), internal, state); 35 | } 36 | 37 | checkPathAndInternal(path: string, internal: string, state: any): string { 38 | let navId = 0; 39 | 40 | if (state) { 41 | navId = state.navigationId; 42 | } 43 | 44 | if (path.startsWith('/') && path.length === 1) { 45 | return internal; 46 | } else if (path.includes('(')) { 47 | return (navId === -1 || navId === 1) ? path : path.replace(/ *\([^)]*\) */g, ''); 48 | } else { 49 | if ( path === internal) { 50 | return path; 51 | } else { 52 | const openParenthesisIndex = internal.indexOf('('); 53 | const closedParenthesisIndex = internal.indexOf(')', openParenthesisIndex); 54 | const result = internal.substring(openParenthesisIndex, closedParenthesisIndex + 1); 55 | 56 | if ( result.length < 1) { 57 | return internal; 58 | } else { 59 | return path + result; 60 | } 61 | } 62 | } 63 | } 64 | 65 | path(includeHash: boolean = false): string { 66 | const pathname = this._platformLocation.pathname + 67 | Location.normalizeQueryParams(this._platformLocation.search); 68 | const hash = this._platformLocation.hash; 69 | return hash && includeHash ? `${pathname}${hash}` : pathname; 70 | } 71 | 72 | pushState(state: any, title: string, url: string, queryParams: string) { 73 | const externalUrl = this.prepareExternalUrl(url + Location.normalizeQueryParams(queryParams), state); 74 | this._platformLocation.pushState(state, title, externalUrl); 75 | } 76 | 77 | replaceState(state: any, title: string, url: string, queryParams: string) { 78 | const externalUrl = this.prepareExternalUrl(url + Location.normalizeQueryParams(queryParams), state); 79 | this._platformLocation.replaceState(state, title, externalUrl); 80 | } 81 | 82 | forward(): void { this._platformLocation.forward(); } 83 | 84 | back(): void { this._platformLocation.back(); } 85 | 86 | } 87 | -------------------------------------------------------------------------------- /src/app/services/lazy-module.service.ts: -------------------------------------------------------------------------------- 1 | import { Injectable, NgModuleRef, NgModuleFactoryLoader, Injector } from '@angular/core'; 2 | 3 | @Injectable({ 4 | providedIn: 'root' 5 | }) 6 | export class LazyModuleService { 7 | 8 | constructor( private loader: NgModuleFactoryLoader, private injector: Injector) { } 9 | 10 | private moduleRef: NgModuleRef; 11 | 12 | public load(loadBoth: boolean = true) { 13 | if (this.moduleRef) { 14 | return Promise.resolve(); 15 | } 16 | 17 | console.log('loading modules...'); 18 | 19 | const paths: string[] = [ 20 | 'src/app/web-components/element-a/element-a.module#ElementAModule', 21 | 'src/app/web-components/element-b/element-b.module#ElementBModule' 22 | ]; 23 | 24 | if (loadBoth) { 25 | return paths.forEach(modulePath => { 26 | return this.loader 27 | .load(modulePath) 28 | .then(moduleFactory => { 29 | this.moduleRef = moduleFactory.create(this.injector).instance; 30 | console.log('Module loaded! =>', this.moduleRef); 31 | }) 32 | .catch(err => { 33 | console.error('error loading module', err); 34 | }); 35 | }); 36 | } else { 37 | return this.loader 38 | .load((paths[0])) 39 | .then(moduleFactory => { 40 | this.moduleRef = moduleFactory.create(this.injector).instance; 41 | console.log('Module loaded! =>', this.moduleRef); 42 | }) 43 | .catch(err => { 44 | console.error('error loading module', err); 45 | }); 46 | } 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /src/app/web-components/element-a/components/home/home.component.html: -------------------------------------------------------------------------------- 1 |

Home

2 | 3 | -------------------------------------------------------------------------------- /src/app/web-components/element-a/components/home/home.component.scss: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tgrassl/angular-elements-routing/dfb4eadfcd3440936679b64b89ce75f00374542e/src/app/web-components/element-a/components/home/home.component.scss -------------------------------------------------------------------------------- /src/app/web-components/element-a/components/home/home.component.spec.ts: -------------------------------------------------------------------------------- 1 | import { async, ComponentFixture, TestBed } from '@angular/core/testing'; 2 | 3 | import { HomeComponent } from './home.component'; 4 | 5 | describe('HomeComponent', () => { 6 | let component: HomeComponent; 7 | let fixture: ComponentFixture; 8 | 9 | beforeEach(async(() => { 10 | TestBed.configureTestingModule({ 11 | declarations: [ HomeComponent ] 12 | }) 13 | .compileComponents(); 14 | })); 15 | 16 | beforeEach(() => { 17 | fixture = TestBed.createComponent(HomeComponent); 18 | component = fixture.componentInstance; 19 | fixture.detectChanges(); 20 | }); 21 | 22 | it('should create', () => { 23 | expect(component).toBeTruthy(); 24 | }); 25 | }); 26 | -------------------------------------------------------------------------------- /src/app/web-components/element-a/components/home/home.component.ts: -------------------------------------------------------------------------------- 1 | import { Component, OnInit } from '@angular/core'; 2 | import { Router } from '@angular/router'; 3 | 4 | @Component({ 5 | selector: 'element-a-home', 6 | templateUrl: './home.component.html', 7 | styleUrls: ['./home.component.scss'] 8 | }) 9 | export class HomeComponent { 10 | 11 | constructor(private router: Router) { } 12 | 13 | public navigateToProfile() { 14 | this.router.navigate([{outlets: {elementA: ['307c5c4e-cd11-4e22-9938-57950f13c3b8']}}]); 15 | } 16 | 17 | public navigateToPictures() { 18 | this.router.navigate([{outlets: {elementA: 'pictures'}}]); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/app/web-components/element-a/components/picture/picture.component.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | -------------------------------------------------------------------------------- /src/app/web-components/element-a/components/picture/picture.component.scss: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tgrassl/angular-elements-routing/dfb4eadfcd3440936679b64b89ce75f00374542e/src/app/web-components/element-a/components/picture/picture.component.scss -------------------------------------------------------------------------------- /src/app/web-components/element-a/components/picture/picture.component.spec.ts: -------------------------------------------------------------------------------- 1 | import { async, ComponentFixture, TestBed } from '@angular/core/testing'; 2 | 3 | import { PictureComponent } from './picture.component'; 4 | 5 | describe('PictureComponent', () => { 6 | let component: PictureComponent; 7 | let fixture: ComponentFixture; 8 | 9 | beforeEach(async(() => { 10 | TestBed.configureTestingModule({ 11 | declarations: [ PictureComponent ] 12 | }) 13 | .compileComponents(); 14 | })); 15 | 16 | beforeEach(() => { 17 | fixture = TestBed.createComponent(PictureComponent); 18 | component = fixture.componentInstance; 19 | fixture.detectChanges(); 20 | }); 21 | 22 | it('should create', () => { 23 | expect(component).toBeTruthy(); 24 | }); 25 | }); 26 | -------------------------------------------------------------------------------- /src/app/web-components/element-a/components/picture/picture.component.ts: -------------------------------------------------------------------------------- 1 | import { Component, OnInit } from '@angular/core'; 2 | import { Router } from '@angular/router'; 3 | 4 | @Component({ 5 | selector: 'element-a-picture', 6 | templateUrl: './picture.component.html', 7 | styleUrls: ['./picture.component.scss'] 8 | }) 9 | export class PictureComponent { 10 | 11 | constructor(private router: Router) { } 12 | 13 | public navigateToHome() { 14 | this.router.navigate([{outlets: {elementA: null}}]); 15 | } 16 | 17 | } 18 | -------------------------------------------------------------------------------- /src/app/web-components/element-a/components/profile/profile.component.html: -------------------------------------------------------------------------------- 1 |

UserID: {{userId}}

2 | 3 | -------------------------------------------------------------------------------- /src/app/web-components/element-a/components/profile/profile.component.scss: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tgrassl/angular-elements-routing/dfb4eadfcd3440936679b64b89ce75f00374542e/src/app/web-components/element-a/components/profile/profile.component.scss -------------------------------------------------------------------------------- /src/app/web-components/element-a/components/profile/profile.component.spec.ts: -------------------------------------------------------------------------------- 1 | import { async, ComponentFixture, TestBed } from '@angular/core/testing'; 2 | 3 | import { ProfileComponent } from './profile.component'; 4 | 5 | describe('ProfileComponent', () => { 6 | let component: ProfileComponent; 7 | let fixture: ComponentFixture; 8 | 9 | beforeEach(async(() => { 10 | TestBed.configureTestingModule({ 11 | declarations: [ ProfileComponent ] 12 | }) 13 | .compileComponents(); 14 | })); 15 | 16 | beforeEach(() => { 17 | fixture = TestBed.createComponent(ProfileComponent); 18 | component = fixture.componentInstance; 19 | fixture.detectChanges(); 20 | }); 21 | 22 | it('should create', () => { 23 | expect(component).toBeTruthy(); 24 | }); 25 | }); 26 | -------------------------------------------------------------------------------- /src/app/web-components/element-a/components/profile/profile.component.ts: -------------------------------------------------------------------------------- 1 | import { Component, OnInit } from '@angular/core'; 2 | import { Router, ActivatedRoute } from '@angular/router'; 3 | 4 | @Component({ 5 | selector: 'element-a-profile', 6 | templateUrl: './profile.component.html', 7 | styleUrls: ['./profile.component.scss'] 8 | }) 9 | export class ProfileComponent implements OnInit{ 10 | 11 | public userId; 12 | 13 | constructor(private router: Router, private route: ActivatedRoute) { } 14 | 15 | ngOnInit() { 16 | this.userId = this.route.snapshot.params.id; 17 | } 18 | 19 | public navigateToHome() { 20 | this.router.navigate([{outlets: {elementA: null}}]); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/app/web-components/element-a/element-a-routing.module.ts: -------------------------------------------------------------------------------- 1 | import { HomeComponent } from './components/home/home.component'; 2 | import { ElementRoutingModule } from 'src/app/element-routing-module'; 3 | import { NgModule } from '@angular/core'; 4 | import { ProfileComponent } from './components/profile/profile.component'; 5 | import { PictureComponent } from './components/picture/picture.component'; 6 | 7 | @NgModule({ 8 | imports: [ 9 | ElementRoutingModule.withRoutes([ 10 | { path: '', component: HomeComponent, outlet: 'elementA' }, 11 | { path: 'pictures', component: PictureComponent, outlet: 'elementA' }, 12 | { path: ':id', component: ProfileComponent, outlet: 'elementA' }, 13 | { path: '**', redirectTo: '', pathMatch: 'full'} 14 | ]), 15 | ], 16 | exports: [ElementRoutingModule], 17 | }) 18 | export class ElementARoutingModule { 19 | 20 | } 21 | -------------------------------------------------------------------------------- /src/app/web-components/element-a/element-a.component.html: -------------------------------------------------------------------------------- 1 |
2 |

Element-A WebComponent

3 | 4 |
-------------------------------------------------------------------------------- /src/app/web-components/element-a/element-a.component.scss: -------------------------------------------------------------------------------- 1 | .element-container { 2 | padding: 1rem; 3 | border-radius: 4px; 4 | width: 500px; 5 | margin: 2rem; 6 | background-color: white; 7 | 8 | &__headline { 9 | border-bottom: 1px solid #333333; 10 | padding-bottom: 0.5rem; 11 | } 12 | } -------------------------------------------------------------------------------- /src/app/web-components/element-a/element-a.component.spec.ts: -------------------------------------------------------------------------------- 1 | import { async, ComponentFixture, TestBed } from '@angular/core/testing'; 2 | 3 | import { ElementAComponent } from './element-a.component'; 4 | 5 | describe('ElementAComponent', () => { 6 | let component: ElementAComponent; 7 | let fixture: ComponentFixture; 8 | 9 | beforeEach(async(() => { 10 | TestBed.configureTestingModule({ 11 | declarations: [ ElementAComponent ] 12 | }) 13 | .compileComponents(); 14 | })); 15 | 16 | beforeEach(() => { 17 | fixture = TestBed.createComponent(ElementAComponent); 18 | component = fixture.componentInstance; 19 | fixture.detectChanges(); 20 | }); 21 | 22 | it('should create', () => { 23 | expect(component).toBeTruthy(); 24 | }); 25 | }); 26 | -------------------------------------------------------------------------------- /src/app/web-components/element-a/element-a.component.ts: -------------------------------------------------------------------------------- 1 | import { Component, OnInit, Input } from '@angular/core'; 2 | import { Router } from '@angular/router'; 3 | 4 | @Component({ 5 | selector: 'element-a', 6 | templateUrl: './element-a.component.html', 7 | styleUrls: ['./element-a.component.scss'] 8 | }) 9 | export class ElementAComponent implements OnInit { 10 | 11 | constructor( 12 | private router: Router) { 13 | } 14 | 15 | ngOnInit() { 16 | this.router.initialNavigation(); // Manually triggering initial navigation for @angular/elements 17 | } 18 | 19 | } 20 | -------------------------------------------------------------------------------- /src/app/web-components/element-a/element-a.module.ts: -------------------------------------------------------------------------------- 1 | import { NgModule, Injector } from '@angular/core'; 2 | import { CommonModule } from '@angular/common'; 3 | import { ElementAComponent } from './element-a.component'; 4 | import { createCustomElement } from '@angular/elements'; 5 | import { HomeComponent } from './components/home/home.component'; 6 | import { ProfileComponent } from './components/profile/profile.component'; 7 | import { ElementARoutingModule } from './element-a-routing.module'; 8 | import { PictureComponent } from './components/picture/picture.component'; 9 | 10 | @NgModule({ 11 | declarations: [ElementAComponent, HomeComponent, ProfileComponent, PictureComponent], 12 | imports: [ 13 | CommonModule, 14 | ElementARoutingModule 15 | ], 16 | entryComponents: [ElementAComponent] 17 | }) 18 | export class ElementAModule { 19 | 20 | constructor(private injector: Injector) { 21 | const elementA = createCustomElement(ElementAComponent, { injector }); 22 | customElements.define('element-a', elementA); 23 | } 24 | 25 | ngDoBootstrap() {} 26 | } 27 | -------------------------------------------------------------------------------- /src/app/web-components/element-b/components/home/home.component.html: -------------------------------------------------------------------------------- 1 |

Home

2 | 3 | -------------------------------------------------------------------------------- /src/app/web-components/element-b/components/home/home.component.scss: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tgrassl/angular-elements-routing/dfb4eadfcd3440936679b64b89ce75f00374542e/src/app/web-components/element-b/components/home/home.component.scss -------------------------------------------------------------------------------- /src/app/web-components/element-b/components/home/home.component.spec.ts: -------------------------------------------------------------------------------- 1 | import { async, ComponentFixture, TestBed } from '@angular/core/testing'; 2 | 3 | import { HomeComponent } from './home.component'; 4 | 5 | describe('HomeComponent', () => { 6 | let component: HomeComponent; 7 | let fixture: ComponentFixture; 8 | 9 | beforeEach(async(() => { 10 | TestBed.configureTestingModule({ 11 | declarations: [ HomeComponent ] 12 | }) 13 | .compileComponents(); 14 | })); 15 | 16 | beforeEach(() => { 17 | fixture = TestBed.createComponent(HomeComponent); 18 | component = fixture.componentInstance; 19 | fixture.detectChanges(); 20 | }); 21 | 22 | it('should create', () => { 23 | expect(component).toBeTruthy(); 24 | }); 25 | }); 26 | -------------------------------------------------------------------------------- /src/app/web-components/element-b/components/home/home.component.ts: -------------------------------------------------------------------------------- 1 | import { Component, OnInit } from '@angular/core'; 2 | import { Router } from '@angular/router'; 3 | 4 | @Component({ 5 | selector: 'element-b-home', 6 | templateUrl: './home.component.html', 7 | styleUrls: ['./home.component.scss'] 8 | }) 9 | export class HomeComponent { 10 | 11 | constructor(private router: Router) { } 12 | 13 | public navigateToProfile() { 14 | this.router.navigate([{outlets: {elementB: ['307c5c4e-cd11-4e22-9938-57950f13c3b8']}}]); 15 | } 16 | 17 | public navigateToPictures() { 18 | this.router.navigate([{outlets: {elementB: 'pictures'}}]); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/app/web-components/element-b/components/picture/picture.component.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | -------------------------------------------------------------------------------- /src/app/web-components/element-b/components/picture/picture.component.scss: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tgrassl/angular-elements-routing/dfb4eadfcd3440936679b64b89ce75f00374542e/src/app/web-components/element-b/components/picture/picture.component.scss -------------------------------------------------------------------------------- /src/app/web-components/element-b/components/picture/picture.component.spec.ts: -------------------------------------------------------------------------------- 1 | import { async, ComponentFixture, TestBed } from '@angular/core/testing'; 2 | 3 | import { PictureComponent } from './picture.component'; 4 | 5 | describe('PictureComponent', () => { 6 | let component: PictureComponent; 7 | let fixture: ComponentFixture; 8 | 9 | beforeEach(async(() => { 10 | TestBed.configureTestingModule({ 11 | declarations: [ PictureComponent ] 12 | }) 13 | .compileComponents(); 14 | })); 15 | 16 | beforeEach(() => { 17 | fixture = TestBed.createComponent(PictureComponent); 18 | component = fixture.componentInstance; 19 | fixture.detectChanges(); 20 | }); 21 | 22 | it('should create', () => { 23 | expect(component).toBeTruthy(); 24 | }); 25 | }); 26 | -------------------------------------------------------------------------------- /src/app/web-components/element-b/components/picture/picture.component.ts: -------------------------------------------------------------------------------- 1 | import { Component, OnInit } from '@angular/core'; 2 | import { Router } from '@angular/router'; 3 | 4 | @Component({ 5 | selector: 'element-b-picture', 6 | templateUrl: './picture.component.html', 7 | styleUrls: ['./picture.component.scss'] 8 | }) 9 | export class PictureComponent { 10 | 11 | constructor(private router: Router) { } 12 | 13 | public navigateToHome() { 14 | this.router.navigate([{outlets: {elementB: null}}]); 15 | } 16 | 17 | } 18 | -------------------------------------------------------------------------------- /src/app/web-components/element-b/components/profile/profile.component.html: -------------------------------------------------------------------------------- 1 |

UserID: {{userId}}

2 | 3 | -------------------------------------------------------------------------------- /src/app/web-components/element-b/components/profile/profile.component.scss: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tgrassl/angular-elements-routing/dfb4eadfcd3440936679b64b89ce75f00374542e/src/app/web-components/element-b/components/profile/profile.component.scss -------------------------------------------------------------------------------- /src/app/web-components/element-b/components/profile/profile.component.spec.ts: -------------------------------------------------------------------------------- 1 | import { async, ComponentFixture, TestBed } from '@angular/core/testing'; 2 | 3 | import { ProfileComponent } from './profile.component'; 4 | 5 | describe('ProfileComponent', () => { 6 | let component: ProfileComponent; 7 | let fixture: ComponentFixture; 8 | 9 | beforeEach(async(() => { 10 | TestBed.configureTestingModule({ 11 | declarations: [ ProfileComponent ] 12 | }) 13 | .compileComponents(); 14 | })); 15 | 16 | beforeEach(() => { 17 | fixture = TestBed.createComponent(ProfileComponent); 18 | component = fixture.componentInstance; 19 | fixture.detectChanges(); 20 | }); 21 | 22 | it('should create', () => { 23 | expect(component).toBeTruthy(); 24 | }); 25 | }); 26 | -------------------------------------------------------------------------------- /src/app/web-components/element-b/components/profile/profile.component.ts: -------------------------------------------------------------------------------- 1 | import { Component, OnInit } from '@angular/core'; 2 | import { Router, ActivatedRoute } from '@angular/router'; 3 | 4 | @Component({ 5 | selector: 'element-b-profile', 6 | templateUrl: './profile.component.html', 7 | styleUrls: ['./profile.component.scss'] 8 | }) 9 | export class ProfileComponent implements OnInit{ 10 | 11 | public userId; 12 | 13 | constructor(private router: Router, private route: ActivatedRoute) { } 14 | 15 | ngOnInit() { 16 | this.userId = this.route.snapshot.params.id; 17 | } 18 | 19 | public navigateToHome() { 20 | this.router.navigate([{outlets: {elementB: null}}]); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/app/web-components/element-b/element-b-routing.module.ts: -------------------------------------------------------------------------------- 1 | import { HomeComponent } from './components/home/home.component'; 2 | import { ElementRoutingModule } from 'src/app/element-routing-module'; 3 | import { NgModule } from '@angular/core'; 4 | import { ProfileComponent } from './components/profile/profile.component'; 5 | import { PictureComponent } from './components/picture/picture.component'; 6 | 7 | @NgModule({ 8 | imports: [ 9 | ElementRoutingModule.withRoutes([ 10 | { path: '', component: HomeComponent, outlet: 'elementB' }, 11 | { path: 'pictures', component: PictureComponent, outlet: 'elementB' }, 12 | { path: ':id', component: ProfileComponent, outlet: 'elementB' }, 13 | { path: '**', redirectTo: '', pathMatch: 'full'} 14 | ]), 15 | ], 16 | exports: [ElementRoutingModule], 17 | }) 18 | export class ElementBRoutingModule { 19 | 20 | } 21 | -------------------------------------------------------------------------------- /src/app/web-components/element-b/element-b.component.html: -------------------------------------------------------------------------------- 1 |
2 |

Element-B WebComponent

3 | 4 |
-------------------------------------------------------------------------------- /src/app/web-components/element-b/element-b.component.scss: -------------------------------------------------------------------------------- 1 | .element-container { 2 | padding: 1rem; 3 | border-radius: 4px; 4 | width: 500px; 5 | margin: 2rem; 6 | background-color: white; 7 | 8 | &__headline { 9 | border-bottom: 1px solid #333333; 10 | padding-bottom: 0.5rem; 11 | } 12 | } -------------------------------------------------------------------------------- /src/app/web-components/element-b/element-b.component.spec.ts: -------------------------------------------------------------------------------- 1 | import { async, ComponentFixture, TestBed } from '@angular/core/testing'; 2 | 3 | import { ElementBComponent } from './element-b.component'; 4 | 5 | describe('ElementAComponent', () => { 6 | let component: ElementBComponent; 7 | let fixture: ComponentFixture; 8 | 9 | beforeEach(async(() => { 10 | TestBed.configureTestingModule({ 11 | declarations: [ ElementBComponent ] 12 | }) 13 | .compileComponents(); 14 | })); 15 | 16 | beforeEach(() => { 17 | fixture = TestBed.createComponent(ElementBComponent); 18 | component = fixture.componentInstance; 19 | fixture.detectChanges(); 20 | }); 21 | 22 | it('should create', () => { 23 | expect(component).toBeTruthy(); 24 | }); 25 | }); 26 | -------------------------------------------------------------------------------- /src/app/web-components/element-b/element-b.component.ts: -------------------------------------------------------------------------------- 1 | import { Component, OnInit } from '@angular/core'; 2 | import { Router } from '@angular/router'; 3 | 4 | @Component({ 5 | selector: 'element-b', 6 | templateUrl: './element-b.component.html', 7 | styleUrls: ['./element-b.component.scss'] 8 | }) 9 | export class ElementBComponent implements OnInit { 10 | 11 | constructor( 12 | private router: Router) { 13 | } 14 | 15 | ngOnInit() { 16 | this.router.initialNavigation(); // Manually triggering initial navigation for @angular/elements 17 | } 18 | 19 | } 20 | -------------------------------------------------------------------------------- /src/app/web-components/element-b/element-b.module.ts: -------------------------------------------------------------------------------- 1 | import { NgModule, Injector } from '@angular/core'; 2 | import { CommonModule } from '@angular/common'; 3 | import { ElementBComponent } from './element-b.component'; 4 | import { createCustomElement } from '@angular/elements'; 5 | import { HomeComponent } from './components/home/home.component'; 6 | import { ProfileComponent } from './components/profile/profile.component'; 7 | import { ElementBRoutingModule } from './element-b-routing.module'; 8 | import { PictureComponent } from './components/picture/picture.component'; 9 | 10 | @NgModule({ 11 | declarations: [ElementBComponent, HomeComponent, ProfileComponent, PictureComponent], 12 | imports: [ 13 | CommonModule, 14 | ElementBRoutingModule 15 | ], 16 | entryComponents: [ElementBComponent] 17 | }) 18 | export class ElementBModule { 19 | 20 | constructor(private injector: Injector) { 21 | const elementB = createCustomElement(ElementBComponent, { injector }); 22 | customElements.define('element-b', elementB); 23 | } 24 | 25 | ngDoBootstrap() {} 26 | } 27 | -------------------------------------------------------------------------------- /src/assets/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tgrassl/angular-elements-routing/dfb4eadfcd3440936679b64b89ce75f00374542e/src/assets/.gitkeep -------------------------------------------------------------------------------- /src/browserslist: -------------------------------------------------------------------------------- 1 | # This file is currently used by autoprefixer to adjust CSS to support the below specified browsers 2 | # For additional information regarding the format and rule options, please see: 3 | # https://github.com/browserslist/browserslist#queries 4 | # 5 | # For IE 9-11 support, please remove 'not' from the last line of the file and adjust as needed 6 | 7 | > 0.5% 8 | last 2 versions 9 | Firefox ESR 10 | not dead 11 | not IE 9-11 -------------------------------------------------------------------------------- /src/environments/environment.prod.ts: -------------------------------------------------------------------------------- 1 | export const environment = { 2 | production: true 3 | }; 4 | -------------------------------------------------------------------------------- /src/environments/environment.ts: -------------------------------------------------------------------------------- 1 | // This file can be replaced during build by using the `fileReplacements` array. 2 | // `ng build --prod` replaces `environment.ts` with `environment.prod.ts`. 3 | // The list of file replacements can be found in `angular.json`. 4 | 5 | export const environment = { 6 | production: false 7 | }; 8 | 9 | /* 10 | * For easier debugging in development mode, you can import the following file 11 | * to ignore zone related error stack frames such as `zone.run`, `zoneDelegate.invokeTask`. 12 | * 13 | * This import should be commented out in production mode because it will have a negative impact 14 | * on performance if an error is thrown. 15 | */ 16 | // import 'zone.js/dist/zone-error'; // Included with Angular CLI. 17 | -------------------------------------------------------------------------------- /src/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tgrassl/angular-elements-routing/dfb4eadfcd3440936679b64b89ce75f00374542e/src/favicon.ico -------------------------------------------------------------------------------- /src/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Angular Elements with Router 6 | 7 | 8 | 9 | 10 | 11 | 12 | Loading... 13 | 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /src/karma.conf.js: -------------------------------------------------------------------------------- 1 | // Karma configuration file, see link for more information 2 | // https://karma-runner.github.io/1.0/config/configuration-file.html 3 | 4 | module.exports = function (config) { 5 | config.set({ 6 | basePath: '', 7 | frameworks: ['jasmine', '@angular-devkit/build-angular'], 8 | plugins: [ 9 | require('karma-jasmine'), 10 | require('karma-chrome-launcher'), 11 | require('karma-jasmine-html-reporter'), 12 | require('karma-coverage-istanbul-reporter'), 13 | require('@angular-devkit/build-angular/plugins/karma') 14 | ], 15 | client: { 16 | clearContext: false // leave Jasmine Spec Runner output visible in browser 17 | }, 18 | coverageIstanbulReporter: { 19 | dir: require('path').join(__dirname, '../coverage/webcomponent-router'), 20 | reports: ['html', 'lcovonly', 'text-summary'], 21 | fixWebpackSourcePaths: true 22 | }, 23 | reporters: ['progress', 'kjhtml'], 24 | port: 9876, 25 | colors: true, 26 | logLevel: config.LOG_INFO, 27 | autoWatch: true, 28 | browsers: ['Chrome'], 29 | singleRun: false, 30 | restartOnFileChange: true 31 | }); 32 | }; 33 | -------------------------------------------------------------------------------- /src/main.ts: -------------------------------------------------------------------------------- 1 | import { enableProdMode } from '@angular/core'; 2 | import { platformBrowserDynamic } from '@angular/platform-browser-dynamic'; 3 | 4 | import { AppModule } from './app/app.module'; 5 | import { environment } from './environments/environment'; 6 | 7 | if (environment.production) { 8 | enableProdMode(); 9 | } 10 | 11 | platformBrowserDynamic().bootstrapModule(AppModule) 12 | .catch(err => console.error(err)); 13 | -------------------------------------------------------------------------------- /src/polyfills.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * This file includes polyfills needed by Angular and is loaded before the app. 3 | * You can add your own extra polyfills to this file. 4 | * 5 | * This file is divided into 2 sections: 6 | * 1. Browser polyfills. These are applied before loading ZoneJS and are sorted by browsers. 7 | * 2. Application imports. Files imported after ZoneJS that should be loaded before your main 8 | * file. 9 | * 10 | * The current setup is for so-called "evergreen" browsers; the last versions of browsers that 11 | * automatically update themselves. This includes Safari >= 10, Chrome >= 55 (including Opera), 12 | * Edge >= 13 on the desktop, and iOS 10 and Chrome on mobile. 13 | * 14 | * Learn more in https://angular.io/guide/browser-support 15 | */ 16 | 17 | /*************************************************************************************************** 18 | * BROWSER POLYFILLS 19 | */ 20 | 21 | /** IE10 and IE11 requires the following for NgClass support on SVG elements */ 22 | // import 'classlist.js'; // Run `npm install --save classlist.js`. 23 | 24 | /** 25 | * Web Animations `@angular/platform-browser/animations` 26 | * Only required if AnimationBuilder is used within the application and using IE/Edge or Safari. 27 | * Standard animation support in Angular DOES NOT require any polyfills (as of Angular 6.0). 28 | */ 29 | // import 'web-animations-js'; // Run `npm install --save web-animations-js`. 30 | 31 | /** 32 | * By default, zone.js will patch all possible macroTask and DomEvents 33 | * user can disable parts of macroTask/DomEvents patch by setting following flags 34 | * because those flags need to be set before `zone.js` being loaded, and webpack 35 | * will put import in the top of bundle, so user need to create a separate file 36 | * in this directory (for example: zone-flags.ts), and put the following flags 37 | * into that file, and then add the following code before importing zone.js. 38 | * import './zone-flags.ts'; 39 | * 40 | * The flags allowed in zone-flags.ts are listed here. 41 | * 42 | * The following flags will work for all browsers. 43 | * 44 | * (window as any).__Zone_disable_requestAnimationFrame = true; // disable patch requestAnimationFrame 45 | * (window as any).__Zone_disable_on_property = true; // disable patch onProperty such as onclick 46 | * (window as any).__zone_symbol__BLACK_LISTED_EVENTS = ['scroll', 'mousemove']; // disable patch specified eventNames 47 | * 48 | * in IE/Edge developer tools, the addEventListener will also be wrapped by zone.js 49 | * with the following flag, it will bypass `zone.js` patch for IE/Edge 50 | * 51 | * (window as any).__Zone_enable_cross_context_check = true; 52 | * 53 | */ 54 | 55 | /*************************************************************************************************** 56 | * Zone JS is required by default for Angular itself. 57 | */ 58 | import 'zone.js/dist/zone'; // Included with Angular CLI. 59 | 60 | 61 | /*************************************************************************************************** 62 | * APPLICATION IMPORTS 63 | */ 64 | -------------------------------------------------------------------------------- /src/styles.scss: -------------------------------------------------------------------------------- 1 | /* You can add global styles to this file, and also import other style files */ 2 | html { 3 | font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif; 4 | background-color: #333333; 5 | } 6 | 7 | h1, p { 8 | color: white; 9 | } 10 | 11 | h2, h3 { 12 | color: #333333; 13 | } 14 | 15 | button { 16 | background-color: #ff7900; 17 | color: #ffffff; 18 | cursor: pointer; 19 | outline: none; 20 | border: 1px solid #ffffff; 21 | font-weight: bold; 22 | min-height: 40px; 23 | padding: 1rem; 24 | transition: all 0.2s ease-in-out; 25 | text-transform: uppercase; 26 | border-radius: 4px; 27 | margin-right: 1rem; 28 | margin-bottom: 1rem; 29 | 30 | &:hover { 31 | background-color: #ffffff; 32 | color: #ff7900; 33 | border: 1px solid #ff7900; 34 | } 35 | } 36 | 37 | img { 38 | height: 330px; 39 | margin-bottom: 1rem; 40 | } -------------------------------------------------------------------------------- /src/test.ts: -------------------------------------------------------------------------------- 1 | // This file is required by karma.conf.js and loads recursively all the .spec and framework files 2 | 3 | import 'zone.js/dist/zone-testing'; 4 | import { getTestBed } from '@angular/core/testing'; 5 | import { 6 | BrowserDynamicTestingModule, 7 | platformBrowserDynamicTesting 8 | } from '@angular/platform-browser-dynamic/testing'; 9 | 10 | declare const require: any; 11 | 12 | // First, initialize the Angular testing environment. 13 | getTestBed().initTestEnvironment( 14 | BrowserDynamicTestingModule, 15 | platformBrowserDynamicTesting() 16 | ); 17 | // Then we find all the tests. 18 | const context = require.context('./', true, /\.spec\.ts$/); 19 | // And load the modules. 20 | context.keys().map(context); 21 | -------------------------------------------------------------------------------- /src/tsconfig.app.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "../tsconfig.json", 3 | "compilerOptions": { 4 | "outDir": "../out-tsc/app", 5 | "types": [] 6 | }, 7 | "exclude": [ 8 | "test.ts", 9 | "**/*.spec.ts" 10 | ] 11 | } 12 | -------------------------------------------------------------------------------- /src/tsconfig.spec.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "../tsconfig.json", 3 | "compilerOptions": { 4 | "outDir": "../out-tsc/spec", 5 | "types": [ 6 | "jasmine", 7 | "node" 8 | ] 9 | }, 10 | "files": [ 11 | "test.ts", 12 | "polyfills.ts" 13 | ], 14 | "include": [ 15 | "**/*.spec.ts", 16 | "**/*.d.ts" 17 | ] 18 | } 19 | -------------------------------------------------------------------------------- /src/tslint.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "../tslint.json", 3 | "rules": { 4 | "directive-selector": [ 5 | true, 6 | "attribute", 7 | "", 8 | "camelCase" 9 | ], 10 | "component-selector": [ 11 | true, 12 | "element", 13 | "", 14 | "kebab-case" 15 | ] 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compileOnSave": false, 3 | "compilerOptions": { 4 | "baseUrl": "./", 5 | "outDir": "./dist/out-tsc", 6 | "sourceMap": true, 7 | "declaration": false, 8 | "module": "es2015", 9 | "moduleResolution": "node", 10 | "emitDecoratorMetadata": true, 11 | "experimentalDecorators": true, 12 | "importHelpers": true, 13 | "target": "es2015", 14 | "typeRoots": [ 15 | "node_modules/@types" 16 | ], 17 | "lib": [ 18 | "es2018", 19 | "dom" 20 | ] 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /tslint.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "tslint:recommended", 3 | "rulesDirectory": [ 4 | "codelyzer" 5 | ], 6 | "rules": { 7 | "array-type": false, 8 | "arrow-parens": false, 9 | "deprecation": { 10 | "severity": "warn" 11 | }, 12 | "variable-name": false, 13 | "import-blacklist": [ 14 | true, 15 | "rxjs/Rx" 16 | ], 17 | "interface-name": false, 18 | "max-classes-per-file": false, 19 | "max-line-length": [ 20 | true, 21 | 140 22 | ], 23 | "member-access": false, 24 | "member-ordering": [ 25 | true, 26 | { 27 | "order": [ 28 | "static-field", 29 | "instance-field", 30 | "static-method", 31 | "instance-method" 32 | ] 33 | } 34 | ], 35 | "no-consecutive-blank-lines": false, 36 | "no-console": [ 37 | false, 38 | "debug", 39 | "info", 40 | "time", 41 | "timeEnd", 42 | "trace" 43 | ], 44 | "no-empty": false, 45 | "no-inferrable-types": [ 46 | true, 47 | "ignore-params" 48 | ], 49 | "no-non-null-assertion": true, 50 | "no-redundant-jsdoc": true, 51 | "no-switch-case-fall-through": true, 52 | "no-use-before-declare": true, 53 | "no-var-requires": false, 54 | "object-literal-key-quotes": [ 55 | true, 56 | "as-needed" 57 | ], 58 | "object-literal-sort-keys": false, 59 | "ordered-imports": false, 60 | "quotemark": [ 61 | true, 62 | "single" 63 | ], 64 | "trailing-comma": false, 65 | "no-output-on-prefix": true, 66 | "use-input-property-decorator": true, 67 | "use-output-property-decorator": true, 68 | "use-host-property-decorator": true, 69 | "no-input-rename": true, 70 | "no-output-rename": true, 71 | "use-life-cycle-interface": true, 72 | "use-pipe-transform-interface": true, 73 | "component-class-suffix": true, 74 | "directive-class-suffix": true 75 | } 76 | } 77 | --------------------------------------------------------------------------------