Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/33.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 角度8导航后不更改收割台_Angular - Fatal编程技术网

Angular 角度8导航后不更改收割台

Angular 角度8导航后不更改收割台,angular,Angular,我想在用户登录时更改标题,但无法更改。重定向工作正常,页面中心发生变化,但页眉不起作用 这是在auth组件中单击“登录”按钮时的功能 LoginFormSubmit(LoginForm){ return this.http.post(environment.laravel_api+'/login',LoginForm.value).subscribe( data => this.afterLogin(data) , error => console.log(error) )

我想在用户登录时更改标题,但无法更改。重定向工作正常,页面中心发生变化,但页眉不起作用

这是在auth组件中单击“登录”按钮时的功能

 LoginFormSubmit(LoginForm){
return this.http.post(environment.laravel_api+'/login',LoginForm.value).subscribe(
  data => this.afterLogin(data) ,
  error => console.log(error)
);
}

后宫{

this.user.setSession(data['user']);
this.route.navigateByUrl('');
}

以下是我的标题组件:

 session;
  constructor(private  user: UserService, private http: HttpClient, private route: Router) {

  }

  ngOnInit(): void {
    console.log("Dentro");
    this.session = this.user.getSession();
  }


  Logout(){
    localStorage.clear();
    this.afterLogout();
  }

  afterLogout(){
    this.route.initialNavigation();
  }
这是用户服务:

 constructor() { }



  setSession(session_data){
    localStorage.setItem('session', JSON.stringify(session_data));

  }

  getSession(){
    return JSON.parse(localStorage.getItem('session'));

  }

您必须在用户服务中实现类似的功能:

@Injectable()
class UserService() {

    private _session= new BehaviorSubject<any>({});

    setSession(session_data: string) {
        this._session.next(session_data);
    }

    get session() {
        return this._session.asObservable();
    }
}
说明:最好不要使用localStorage来保存会话,因为它可能会导致Angular应用程序在会话过期时与后端不同步。更喜欢从后端发送cookie,让Angular应用程序在每次启动或刷新应用程序时检索用户信息


Angular服务是整个应用程序的单一实例,因此它可以保存状态并与任何组件共享。当登录状态发生更改时,BehaviorSubject会帮助通知订阅者。

我想查看带有路由器出口的组件。选中this.session=this.user.getSession时,标题组件已加载;你们看到登特罗再次进入控制台了吗?好的,我明白了。您必须在路由到的组件中具有标头。@DrilonHametaj抱歉,我没有理解您的意思谢谢,但是我如何才能注销?注销可以通过通知后端来完成,后端将清除cookie。如果没有网络,也可以从前端删除cookie。
<button>{{ (userService.session | async).firstName }}</button>