请求的路径包含索引0处的未定义段

时间:2017-05-17 12:08:07

标签: authentication routing angular2-routing

authentication.service.ts,我的登录服务是这样定义的

import { Injectable } from '@angular/core';
import { Http, Headers, Response, RequestOptions } from '@angular/http';
import { Observable } from 'rxjs';
import 'rxjs/Rx';
import 'rxjs/add/operator/map'
import { AuthHttp } from 'angular2-jwt';
import { Router } from '@angular/router';
   @Injectable()
   export class AuthenticationService {
token: string;
redirectUrl: any;
constructor(public router: Router, private http: Http, private authHttp: AuthHttp) {
    //set token if saved in local storage
    if (localStorage.getItem('authToken') != null) {
        try {
            // var currentUser = 
          JSON.parse(localStorage.getItem('currentUser'));
             this.token = localStorage.getItem('authToken');
             // this.token = currentUser && currentUser.token;
         } catch (e) {
             localStorage.removeItem("authToken");
         }
    }
 }

login(username: string, password: string) {
    return this.http.post('http://localhost:3002/api/authenticate',({ username: username, password: password }))
        .map(res => {
            console.log(res);
            var authToken = res.json().token;
            localStorage.setItem('authToken', authToken);
            let redirectUrlTemp: any;
            redirectUrlTemp = this.redirectUrl;
            this.redirectUrl = null;
            if (!redirectUrlTemp) {
                    console.log(redirectUrlTemp);
                redirectUrlTemp = ['/login'];
            }


            this.router.navigate(redirectUrlTemp);


        },
        err => {
            //alert(error.text());
            console.log(err.text());
        });
}
logout(){
    // clear token remove user from local storage to log user out
    this.token = null;
    localStorage.removeItem('authToken');
 }
}

当我尝试使用邮递员发布数据时,它可以正常工作但是从角度2开始它不允许从下一页重定向

Login.component.ts

import { Component, OnInit } from '@angular/core';
import { Router, ActivatedRoute } from '@angular/router';
import { AuthenticationService } from "app/services";
import { LoginRequest } from "app/models/models";
import { AlertService } from 'app/services';
import { Observable } from 'rxjs/Observable';
import 'rxjs/Rx';
import { Http, Response } from '@angular/http';
   @Component({
     moduleId: module.id,
     selector: 'login',
     templateUrl: 'login.component.html'
    })
    export class LoginComponent implements OnInit {

      model: any = {};
      getLogin: LoginRequest[] = [];
      loading = false;
      error = '';
      returnUrl: string;
   constructor(
    private route: ActivatedRoute,
    private router: Router,
    private http: Http,
    private alertService: AlertService,
    private authenticationService: AuthenticationService) { }

login() {
    this.loading = true;
    this.authenticationService.login(this.model.username, this.model.password).subscribe(data => {
                this.router.navigate([this.returnUrl]);
            },
            error => {
                this.alertService.error(error);
                this.loading = false;
            });

      //let redirect = this.authenticationService.redirectUrl ? this.authenticationService.redirectUrl : '/allTask';
 }
}

auth.guadr.ts

import { Injectable } from '@angular/core';
import { Router, CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router';
import { AuthenticationService } from "app/services";


 @Injectable()
  export class AuthGuard implements CanActivate {
  constructor(private router: Router, private authService: AuthenticationService) { }

 canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean {
    if (localStorage.getItem('authToken')) {
        // logged in so return true

        return true;
    }
    this.authService.redirectUrl =[state.url];
    this.router.navigate(['/login']);
    return false;
   }
}

1 个答案:

答案 0 :(得分:3)

此错误表示未定义索引为0的导航数组参数。 在您的情况下,您没有在Login.component.ts中设置select a.*,b.cnt from demo a join ( select name,count(*) cnt from demo group by name ) b using(name) this.returnUrl