Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/30.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 角度布线主元件仅在单击时起作用_Angular_Authentication_Routes_Auth Guard - Fatal编程技术网

Angular 角度布线主元件仅在单击时起作用

Angular 角度布线主元件仅在单击时起作用,angular,authentication,routes,auth-guard,Angular,Authentication,Routes,Auth Guard,我现在正为一个奇怪的情况烦恼。我正在尝试创建一个简单的身份验证应用程序。到现在为止,一切都很好 在我的应用程序中,我将登录验证发送到使用Express创建的后端。然后它确认并向前端发送ok响应。问题就在这里。当我登录后尝试单击主页时,它会正确显示主页: 但当我试图将浏览器定位到'http://localhost:4200/,它不显示已将my homecomponent路径设置为“”的主组件: 它应该显示出来,因为。下面是代码: 应用程序路由模块 const routes: Routes =

我现在正为一个奇怪的情况烦恼。我正在尝试创建一个简单的身份验证应用程序。到现在为止,一切都很好

在我的应用程序中,我将登录验证发送到使用Express创建的后端。然后它确认并向前端发送ok响应。问题就在这里。当我登录后尝试单击主页时,它会正确显示主页:

但当我试图将浏览器定位到'http://localhost:4200/,它不显示已将my homecomponent路径设置为“”的主组件:

它应该显示出来,因为。下面是代码:

应用程序路由模块

const routes: Routes = [
  {
    path: '',
    component: HomeComponent,
    canActivate: [AuthGuard]
  },
  {
    path: 'login',
    component: LoginComponent
  },
  {
    path: 'register',
    component: RegisterComponent
  }
];
授权守卫

canActivate(): boolean {
    if(this._cookieService.get('access_token')) {
      return this._authService.loggedIn();
    } else {
      this._router.navigate(['login']);
      return false;
    }
    
  }
这是身份验证服务

 _registerURI = 'http://localhost:3000/auth/register';
  _loginURI = 'http://localhost:3000/auth/login';
  _token = 'http://localhost:3000/auth/VpW02cG0W2vGeGXs8DdLIq3dQ62qMd0';
  isIt: boolean = false;

  token = {};
  user = {
    name: ''
  };

  constructor(
    private http: HttpClient,
    private _cookieService: CookieService) { }

  registerUser(user) {
    return this.http.post<any>(this._registerURI, user, {withCredentials: true});
  };

  loginUser(user) {
    return this.http.post<any>(this._loginURI, user, {withCredentials: true});
  };

  getToken() { // this is for the validation of token
    return this.http.post<any>(this._token, this.token, {responseType: 'json', withCredentials: true});
  }

  loggedIn(): boolean {
    this.getToken().subscribe(res => {
      if(res.success === true){
        this.isIt = true;
      }
    }, err => {
      if(err.status === 401){
      this.isIt = false;
      alert('You need to sign in.');
      }
      console.log(err);
    })

    return this.isIt;
  }
\u registerURI=http://localhost:3000/auth/register';
_罗吉努里酒店http://localhost:3000/auth/login';
_代币http://localhost:3000/auth/VpW02cG0W2vGeGXs8DdLIq3dQ62qMd0';
isIt:boolean=false;
令牌={};
用户={
名称:“”
};
建造师(
私有http:HttpClient,
私人{u cookieService:cookieService}
注册服务器(用户){
返回this.http.post(this.\u registerURI,用户,{withCredentials:true});
};
登录用户(用户){
返回this.http.post(this.\u loginURI,用户,{withCredentials:true});
};
getToken(){//这是用于验证令牌的
返回this.http.post(this.u令牌,this.token,{responseType:'json',带凭证:true});
}
loggedIn():布尔值{
this.getToken().subscribe(res=>{
如果(res.success==true){
this.isIt=true;
}
},err=>{
如果(错误状态===401){
this.isIt=false;
警报('您需要登录');
}
控制台日志(err);
})
返回此.isIt;
}

提前谢谢

手动输入
http://localhost:4200/
。不,没有任何错误消息