Angular JS - Native Routing Example
Enabling Native Routing with @beacon.li/bar in an Angular Component
@beacon.li/bar in an Angular ComponentIf you're using Angular and the Angular Router and you want to enable native routing for the Beacon widget, you can achieve this by integrating the @beacon.li/bar package with your Angular component. This allows you to control the behavior of the Beacon widget based on the current route using the OnInit lifecycle hook.
Integrating @beacon.li/bar with Angular Router
@beacon.li/bar with Angular RouterHere's how you can enable native routing with Beacon in your Angular component:
Import the necessary modules and components at the beginning of your component:
import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { Beacon } from '@beacon.li/bar';Create your Angular component as usual and implement the
OnInitinterface:
@Component({
selector: 'app-my-component',
templateUrl: './my-component.component.html',
styleUrls: ['./my-component.component.css']
})
export class MyComponent implements OnInit {Inject the Angular Router service into your component's constructor:
constructor(private router: Router) {
}Use the
ngOnInitlifecycle hook to set up Beacon's router based on the current route. This allows you to navigate to different routes when the user interacts with the Beacon widget:
ngOnInit() {
Beacon.setRouter((path: string) => {
this.router.navigateByUrl(path);
});
}How It Works
The
Routerservice from the Angular Router provides you with methods for navigating to different routes in your Angular application.In the
ngOnInitmethod, you callBeacon.setRouter((path: string) => this.router.navigateByUrl(path)). This configures Beacon to use the Angular Router'snavigateByUrlmethod when navigating to different routes.Whenever the user interacts with the Beacon widget, it will trigger this router configuration, allowing you to seamlessly integrate beacon into your Angular application's navigation flow.
By following these steps, you can enable native routing for the Beacon widget in your Angular component, ensuring a consistent and user-friendly experience when users interact with the beacon bar.
Last updated