following starting point:
We have a few angular frontend micro-services, and now we want to bring them together in one app. We are trying the Angular-Elements approach and we have exported one of the microservices which yielded a huge JS-file.
The problem
When we included the angular element in the "bring it together"-project, simply the "router-outlet" tag is in the dom, but no html is rendered.
Code
The Custom Element:
app.module.ts:
import {NgModule, Injector} from '@angular/core';
import {AppRoutingModule} from './app-routing.module';
import {AppComponent} from './app.component';
import {PhoneModule} from "./features/phone/phone.module";
import {createCustomElement} from '@angular/elements';
import {Router} from '@angular/router';
@NgModule({
declarations: [
AppComponent,
],
imports: [
AppRoutingModule,
PhoneModule,
],
entryComponents: [AppComponent],
})
export class AppModule {
constructor(private injector: Injector, private router: Router) {
this.router = router;
}
ngOnInit() {
this.router.initialNavigation();
}
ngDoBootstrap() {
const customElement = createCustomElement(AppComponent, {injector: this.injector});
customElements.define('phone-contracts', customElement);
}
}
app-routing.module.ts:
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { PhoneListComponent } from './features/phone/phone-list/phone-list.component';
import {PhoneContractDetailsComponent} from "./features/phone/phone-contract-details/phone-contract-details.component";
const routes: Routes = [
{ path: '', redirectTo: 'phone-list', pathMatch: 'full' },
{ path: '', component: PhoneListComponent,},
{ path: 'phone-list/:id', component: PhoneContractDetailsComponent },
{ path: '**', redirectTo: '/phone-list', pathMatch: 'full'}
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule { }
The bring-it-together-project: These are from the component which is supposed to handle this specific micro-service
phone-contracts.component.html:
<p>
phone-contracts works!
</p>
<phone-contracts>
<app-phone-list>
</app-phone-list>
</phone-contracts>
app.module.ts:
import {CUSTOM_ELEMENTS_SCHEMA, NgModule} from '@angular/core';
import { FormsModule } from '@angular/forms';
import { BrowserModule } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import { HomeComponent } from './home/home.component';
import { LoginComponent } from './login/login.component';
import { FooComponent } from './foo/foo.component';
import { HttpClientModule } from '@angular/common/http';
import { CookieService } from 'ngx-cookie-service';
import { AppRoutingModule } from './app-routing.module';
import { AppService } from './app.service';
import { PhoneContractsComponent } from './phone-contracts/phone-contracts.component';
import { DeviceListComponent } from './device-list/device-list.component';
@NgModule({
declarations: [
AppComponent,
HomeComponent,
LoginComponent,
FooComponent,
PhoneContractsComponent,
DeviceListComponent
],
imports: [
BrowserModule,
FormsModule,
AppRoutingModule,
HttpClientModule],
schemas: [CUSTOM_ELEMENTS_SCHEMA],
providers: [CookieService,AppService],
bootstrap: [AppComponent]
})
export class AppModule { }
stuff that might matter?
We are using this as our "builder": "@angular-devkit/build-angular:browser",
We think that the issue is that the custom element cant resolve the routing and therefore just shows nothing, could this be true?
First, add links to the two components. Assign the anchor tag that you want to add the route to the routerLink attribute. Set the value of the attribute to the component to show when a user clicks on each link. Next, update your component template to include <router-outlet> .
The Router-Outlet # The Router-Outlet is a directive that's available from the router library where the Router inserts the component that gets matched based on the current browser's URL. You can add multiple outlets in your Angular application which enables you to implement advanced routing scenarios.
You can have multiple router-outlet in same template by configuring your router and providing name to your router-outlet, you can achieve this as follows. Advantage of below approach is thats you can avoid dirty looking URL with it. eg: /home(aux:login) etc.
Add a default routelink To make the application navigate to the dashboard automatically, add the following route to the routes array. content_copy { path: '', redirectTo: '/dashboard', pathMatch: 'full' }, This route redirects a URL that fully matches the empty path to the route whose path is '/dashboard' .
This is what your Routes list should look like:
const routes: Routes = [
{
path: 'phone-list',
component: PhoneListComponent
},
{
path: 'phone-list/:id',
component: PhoneContractDetailsComponent
},
{
path: '**',
redirectTo: 'phone-list',
pathMatch: 'full'
}
];
In addition to that, you haven't actually specified where your router-outlet element is present.. if it's displaying it as html then chances are you haven't closed it's parent tag and/or it's not being used on an Angular component.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With