【问题标题】:Inertia.JS , Laravel closing modal if no validation errorsInertia.JS ,如果没有验证错误,Laravel 关闭模式
【发布时间】:2021-09-25 19:46:26
【问题描述】:

所以我有以下方法:

methods: {
    submit() {    
      this.$inertia.post('/projects', this.form);
      this.openModal = false;
      
    },
},

但即使存在验证错误,它也会关闭我的模式。 我也试过了

this.$inertia.post('/projects', this.form);
      if(!this.$page.props.errors) {
              this.openModal = false;
}

如果有验证错误,它不会关闭模式,但如果没有验证错误,它不会关闭它。

【问题讨论】:

    标签: javascript laravel vue.js inertiajs


    【解决方案1】:

    this.$inertia.post 是异步执行的,因此this.openModal = false 很可能会在您收到服务器响应之前完成。如果您想了解我的意思,请将 sleep(10); 放在控制器中的某个位置,用于 /projects 路由 - 在 Inertia 从服务器返回响应之前,您在 this.$inertia.post 之后的任何代码都将被执行。

    我假设您的控制器重定向回来。如果您的模态在页面加载时默认关闭,那么您可以在出现以下错误时保持模态打开:

    this.$inertia.post(
        this.route("/projects"),
        this.form,
        {
            preserveState: (page) => Object.keys(page.props.errors).length > 0,
        }
    );
    

    请参阅Inertia docs 中的组件状态部分以供参考。

    【讨论】:

      猜你喜欢
      • 2021-01-26
      • 2023-03-23
      • 2020-02-25
      • 1970-01-01
      • 2017-10-21
      • 1970-01-01
      • 2015-10-29
      • 1970-01-01
      • 2018-05-25
      相关资源
      最近更新 更多