2

I'm new to angular 2 and trying to create a login app which I managed okay however after checking user/pass then redirect to dashboard it reloads the app. Is there a way to not refresh the page using router.navigate?

Edit: it redirects to dashboard first then reloads the page then redirects again back to dashboard.

import { Component } from '@angular/core';
import { Router, ROUTER_DIRECTIVES } from '@angular/router';

@Component({
    selector: 'login',
    templateUrl: './app/login/views/login.html',
})

export class LoginComponent {
    constructor(public router: Router) {}
    data = {
        username: "",
        password: "",
    };
    loginAction (){
        if(this.data.username=="user1" && this.data.password=="pass1"){
            console.log('do redirect to dashboard');
            this.router.navigate(['dashboard']);
        } else {
            console.log('Something is wrong with your user/password.');
        }
    }
}

2 Answers 2

1

this.router.navigate(['/dashboard']);

Sign up to request clarification or add additional context in comments.

2 Comments

yeah did that too and still refresh the page, also this should be a comment than an answer.
hm perhaps that's the answer. Another question about routing, is there a way to restrict user from doing /dashboard? if they force to go to this route without checking localStorage it should redirect them back. Normally it'd be something like if(localStorage.getItem)..
0

Did you try relativeTo

router.navigate(['dashboard'], {relativeTo: route});

1 Comment

not yet however that's giving me an error compilation, tired injecting route in the const and import but in {relativeTo: route} route is can't find name. See my edit in my original post.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.