The canActivate() function encounters issues when working with Observable responses in Angular 2

I am facing an issue with canActivate in Angular 2.0.0-rc.3.

canActivate(next: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean>{
    console.log('canActivate with AclService'); 
   // return true;
    return Observable.create((observer:Subject<boolean>) => { observer.next(true);  });
   }

It seems that it's not working with Observable response, but it does work with a simple boolean response.

How can I resolve this problem?

Answer №1

If you include the .first() method or choose to complete the Observable using alternative methods, it should function as intended:

canActivate(next: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean>{
    console.log('canActivate with AclService'); 
   // return true;
    return Observable.create((observer:Subject<boolean>) => { observer.next(true);  }).first();
}

or

canActivate(next: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean>{
    console.log('canActivate with AclService'); 
   // return true;
    return Observable.of(true);
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Using Angular 2 to position a md-fab button with 'position: fixed' inside an inner component

Utilizing the md-fab-button from the initial angular2 material-framework has presented a challenge for me. I am attempting to set the md-fab-button(for adding a new entity) with position: fixed, but my goal is to position the button within an inner compone ...

When using the ionic 3 storage.get function, it may return a null value when accessed outside

In regards to storage, the function is returning a null value outside of the function. Below is the code snippet: username:any; this.storage.get('user').then((value) => { this.username = value; }); console.log(this.username); Ou ...

Managing a MySQL database in NodeJS using Typescript through a DatabaseController

I'm currently working on a restAPI using nodejs, express, and mysql. My starting point is the app.js file. Within the app.js, I set up the UserController: const router: express.Router = express.Router(); new UserController(router); The UserControll ...

Encountering a roadblock while trying to install a package using NPM, as the installation process becomes halted at version [email 

Having trouble installing @angular/cli via npm. It seems to get stuck every time while trying to download the package chokidar. https://example.com/image.png Here is some diagnostic information: npm version 5.0.0 node version 8.0.0 OS: Windows 7 ...

Tips on validating interconnected strings with the help of yup within a react native environment

In my scenario, I am dealing with two date strings called start_date and end_date. Initially, both of these start off as empty strings: export interface FilterSchema { start_date?: any; end_date?: any; } const initialValues: FilterSchema = { s ...

The justify-content: space-between property does not work in a nested flex box scenario

I'm currently working on styling a cart in Angular and facing an issue with aligning the price all the way to the right of the cart. I attempted using `space-between` within the outer div, which worked fine, but when applied to the inner div, it doesn ...

The process of upgrading all dependencies to a particular version

I need guidance on upgrading a project from Angular 6 to version 7. The challenge lies in updating numerous dependencies as well. Most tutorials available only cover upgrading to the latest version (v8), rather than a specific one. Can anyone provide ins ...

How can I update a property within an object in a sequential manner, similar to taking turns in a game, using React.js?

I am currently working on a ReactJs project where I am creating a game, but I have encountered an issue. I need to alternate turns between players and generate a random number between 1 and 10 for each player, storing this random number inside their respec ...

Breaking up and Substituting text within Angular 8's HTML structure

When I retrieve data from a REST api, I need to split the name parameter at '2330' and insert a line break. For example, if the name is: ABCD 2330 This is My Name, I want the output on my screen to appear as: ABCD 2330 This is My Name // this par ...

What type should React Router props be?

For a specific scenario, I developed a custom router component that redirects if certain parameters are missing: const StateParamRoute = (props: any) => { ... return stateParam ? <Route {...props} /> : <Redirect to="/error" />; ...

Why does tsc produce a compiled file that throws an exception when executed, while ts-node successfully runs the TypeScript file without any issues?

I have written two ts files to test a decorator. Here is the content of index.ts: import { lockMethod } from './dec'; class Person { walk() { console.info(`I am walking`); } @lockMethod run() { console.info(`I am running`); } ...

Can you explain the process for accessing a parent function in Angular?

I have a form component that inserts data into a database upon submission, and I need to update the displayed data in another component once it changes in the database. I attempted using ViewChild to invoke the necessary functions, but encountered issues w ...

The perplexing actions of Map<string, string[]> = new Map() have left many scratching their heads

I encountered an issue while trying to add a value to a map in my Angular project. The map is initially set up using the following code: filters: Map<string, string[]> = new Map(); However, when I attempt to add a value to this map, it starts displa ...

Sending data from app.config file to component

I have a function in my app.config file that retrieves the current environment using window.location.hostname. I am looking for a way to pass this value to another component within my application. How can I accomplish this? Here is an excerpt from my app. ...

Create a custom navigation system for ng-bootstrap tabset using manual controls

Currently, when I click on the "Client" tab, the content opens. Similarly, when I click on the "Rate card" tab, the rate card content opens. However, the navigation does not work when I use the arrows at the bottom. How can I enable navigation for the ar ...

The router fails to navigate upon clicking after transitioning from beta to rc5 as a module

My routing configuration is as follows: import { Router, RouterModule } from '@angular/router'; import { HomeComponent } from './modules/home/home.component'; import { Step1Component } from './modules/step1/step1.component' ...

Guidance on incorporating a function as a prop in React using TypeScript

I'm currently learning TypeScript with React and ran into an issue. I attempted to pass a function as a property from my App component to a child component named DataForm. However, I encountered the following error: Type '(f: any) => any&ap ...

The art of Angular localization: harvesting language elements from ts documents

Is it true that $localize is only available for translation in ts files, but the extraction of strings is currently only implemented for templates? I came across information stating that the extraction of strings from ts files should be possible in Angular ...

"Angular allows for the creation of nested div elements using its powerful functionalities

In my file, I have the following code: <kendo-chat [messages]="feed | async" [user]="user" (sendMessage)="sendMessage($event)"> </kendo-chat> After it is loaded in the browser, the code looks like this: <kendo-chat> < ...

Angular 15 brings an exciting new feature: the Swiper 9 Element

I went through the swiperjs official documentation found at: swiperjs doc To display images, I created a method to configure and initialize the swiper only when necessary. Below is the HTML code snippet: <swiper-container #swiperRef init="false& ...