0

我正在使用带有 vuejs 和惯性js 的 laravel 项目。我想让用户登录,登录后我希望用户重定向到 test.vue 文件,我创建该文件只是为了学习。我在提交注册表后做了一个注册表我调用了一个函数登录,即我希望同一个用户登录,谁注册了。我设置的登录功能工作正常,但无法使用惯性渲染重定向到 vue 页面。

注册表格.vue


     SubmitRegistration(){
                       this.$v.$touch();
                       if (this.$v.$invalid) return;
                        axios.post('submitRegistrationForm',this.form)
                        .catch(({ response }) => {
                            this.$toast.error(response.data.message, 'error', {timeout:3000});
                        }).then(({ data }) => {
                            this.setLogin();
                        });
                    },
                    setLogin(){
                      const data = {
                            email: this.form.email,
                            password: this.form.password
                        }
                        axios.post('loggedInUser',data)
                        .catch(({ response }) => {
                            this.$toast.error(response.data.message,'error', {timeout: 3000});
                        });
                },

网页.php


    Auth::routes();
    Route::post('loggedInUser',[FrontEndController::class,'authenticate'])->name('login.attempt');

前端控制器.php

 

    public function authenticate(Request $request)
        {
            $credentials = $request->only('email', 'password');
    
            if (Auth::attempt($credentials)) {
                return Inertia::render('Layouts/Test');
            }
        }

测试.vue


    <template>
        <h1>
        Hello
        </h1>
    </template>

4

1 回答 1

1

尝试访问loggedInUserusingthis.$inertia.post(url, data)而不是 axios 调用。也就是说,执行:

setLogin() {
    const data = {
        email: this.form.email,
        password: this.form.password
    }
    this.$inertia.post(url, data);
};

代替:

setLogin() {
    const data = {
        email: this.form.email,
        password: this.form.password
    }
    axios.post('loggedInUser',data)
        .catch(({ response }) => {
            this.$toast.error(response.data.message,'error', {timeout: 3000});
        });
}
于 2020-12-02T15:46:40.367 回答