Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/462.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/8.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/blackberry/2.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
Javascript 如何重定向页面取决于第8节中的条件_Javascript_Typescript_Angular7_Angular8 - Fatal编程技术网

Javascript 如何重定向页面取决于第8节中的条件

Javascript 如何重定向页面取决于第8节中的条件,javascript,typescript,angular7,angular8,Javascript,Typescript,Angular7,Angular8,我正在尝试从登录页面重定向页面。我有一个标志,用于检查购物车是否为空。我可以设置该标志为真或假。我想在登录后重定向页面,这取决于标志和是否已登录。我已尝试,但无法工作。请帮助任何人找到解决方案 login.commponent.ts: onSubmit() { this.submitted = true; if (this.loginForm.invalid) { return; } this.loading = true; this.

我正在尝试从登录页面重定向页面。我有一个标志,用于检查购物车是否为空。我可以设置该标志为真或假。我想在登录后重定向页面,这取决于标志和是否已登录。我已尝试,但无法工作。请帮助任何人找到解决方案

login.commponent.ts:

onSubmit() {
    this.submitted = true; 
    if (this.loginForm.invalid) {
        return;
    } 
    this.loading = true;
    this.authenticationService.login(this.f.username.value, this.f.password.value)
        .pipe(first())
        .subscribe(
            data => {
              //if cart is not empty
              // if(this.flag==true){
              //   this.router.navigate(['./billing']);
              // }
               //if cart is empty
              //else{
              //   this.router.navigate(['./cartempty']);
              // }
              //this.router.navigate([this.returnUrl]);

            },
            error => {
                this.alertService.error(error);
                this.loading = false;
            });
}
goTOloginbilling(){

                  //if Not logged
                  // if(????){
                  //   this.router.navigate(['./login']);
                  // }
                  //if cart is not empty
                  // else if(this.flag==true){
                  //   this.router.navigate(['./billing']);
                  // }
                   //if cart is empty
                  //else if(this.flag==false){
                  //   this.router.navigate(['./cartempty']);
                  // }

  }
order.component.ts:

onSubmit() {
    this.submitted = true; 
    if (this.loginForm.invalid) {
        return;
    } 
    this.loading = true;
    this.authenticationService.login(this.f.username.value, this.f.password.value)
        .pipe(first())
        .subscribe(
            data => {
              //if cart is not empty
              // if(this.flag==true){
              //   this.router.navigate(['./billing']);
              // }
               //if cart is empty
              //else{
              //   this.router.navigate(['./cartempty']);
              // }
              //this.router.navigate([this.returnUrl]);

            },
            error => {
                this.alertService.error(error);
                this.loading = false;
            });
}
goTOloginbilling(){

                  //if Not logged
                  // if(????){
                  //   this.router.navigate(['./login']);
                  // }
                  //if cart is not empty
                  // else if(this.flag==true){
                  //   this.router.navigate(['./billing']);
                  // }
                   //if cart is empty
                  //else if(this.flag==false){
                  //   this.router.navigate(['./cartempty']);
                  // }

  }

演示:

查看您的代码时,我发现了一些问题

在login.component.ts中,变量标志声明如下:

flag: true
我想你想写
flag:boolean=true

在同一文件中,在调用登录函数时,在 你的回复是:

this.router.navigate(['./billing']);
但在app.routing.module.ts中,路由是:

{ path: '', component: BillingComponent, canActivate: [AuthGuard] },
所以你不得不写:

this.router.navigate(['']);
因此,总而言之:

onSubmit() {
    this.submitted = true;
    if (this.loginForm.invalid) {
      return;
    }
    this.loading = true;
    this.authenticationService
      .login(this.f.username.value, this.f.password.value)
      .subscribe(
        data => {
          //if cart is not empty
          if (this.flag == true) this.router.navigate([""]);
          else {
            this.router.navigate(["cartempty"]);
          }
        },
        error => {
          this.alertService.error(error);
          this.loading = false;
        }
      );
  }
显然,您可以在app.routing.ts中为cartEmpty定义路由:

{ path: 'cartempty', component: CartemptyComponent },

用户名密码?@brk:您可以创建它。这里有注册选项。谢谢。如何在标题右侧显示登录的用户名?