【问题标题】:Angular - Sweetalrt2 redirects before I click OKAngular - Sweetalert2 在我单击确定之前重定向
【发布时间】:2019-09-20 03:12:11
【问题描述】:

我将 Angular-7 用于我的 Web 应用程序。在项目中,我应用了sweetalert2。

client.component.ts

import { Component, OnInit, ElementRef, NgZone, ViewChild } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { ApiService } from '../../shared/services/api.service';
import { TokenService } from '../../shared/services/token.service';
import { Router } from '@angular/router';
import { SnotifyService } from 'ng-snotify';
import swal from 'sweetalert2';
import { FormControl } from "@angular/forms";
import { MapsAPILoader } from '@agm/core';

onSubmit(){
 this.notify.clear();
 var header = {
  'Content-Type': 'application/json'
 }
 return this.api.post('clientquotelanding', this.form, header).subscribe(
  response => {
  swal.fire(
    'Congratulations!',
    'Your Quote have been successfully received. You will hear from us shortly through the provided email. Thank you!',
    'success'
  )
  },
  error => this.errorHandle(error),
  this.router.navigateByUrl('/landing')
);
}

我遇到的问题是,当我单击提交按钮时,页面会在 sweetalert2 消息弹出之前重定向到登录页面。我该如何解决这个问题?

【问题讨论】:

  • 订阅完成方法中this.router.navigateByUrl('/landing') 的预期行为是什么

标签: angular


【解决方案1】:

问题在于this.router.navigateByUrl('/landing') 语句。你应该把它放在成功里面。 subscribe 采用第三个参数进行 oncomplete 操作。

试试这样的。

response => {
  swal.fire(
    'Congratulations!',
    'Your Quote have been successfully received. You will hear from us shortly through the provided email. Thank you!',
    'success'
  );
this.router.navigateByUrl('/landing')
  },

您可以阅读更多关于它的信息here

【讨论】:

    【解决方案2】:
    swal({
            title: 'Are you sure?',
            text: "You won't be able to revert this!",
            type: 'warning',
            showCancelButton: true,
            confirmButtonClass: 'btn btn-success',
            cancelButtonClass: 'btn btn-danger',
            confirmButtonText: 'Yes, delete it!',
             buttonsStyling: false
          }).then((result) => {
            if (result.value) {
              this.router.navigateByUrl('/landing');
            }
          })
    

    【讨论】:

    • 你的解释给了我一个线索。但唯一的问题是我没有删除。提交后(保存到数据库)。 swal 带来了成功的信息。单击确定按钮时,它会重定向到 /landing。注意:它应该只在单击确定按钮时重定向到数据库
    猜你喜欢
    • 2015-08-16
    • 1970-01-01
    • 2014-10-14
    • 2021-03-15
    • 2020-08-18
    • 2018-06-06
    • 2016-09-12
    • 2023-02-26
    • 1970-01-01
    相关资源
    最近更新 更多