正确地惯性重定向到另一页? [英] Correctly redirect to another page with inertia?

查看:481
本文介绍了正确地惯性重定向到另一页?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于某种原因,我试图将单击重定向到另一个页面,这不起作用.这是我的Vue上的重定向按钮所在的代码.我尝试了两种不同的方法,但均不起作用.

I"m trying to redirect on click to another page, for some reason it's not working. This is the code on my vue where the redirect buttons are. I've tried two different ways and neither are working.

<el-row class="import-btn">
    <a :href="'/imports/teachers'">
        <el-button type="warning">                                     Importar
        </el-button>
    </a>
</el-row>
<el-row class="import-btn">
    <el-button type="warning"                               
        @click="redirectStudents()">                                    Importar
    </el-button>
</el-row>

redirectStudents() {      
  this.$inertia.visit('/imports/students');
},

我有这样的web.php路由

I have the web.php routes like this

Route::resource('imports/students', 'ImportStudentController');
Route::resource('imports/teachers', 'ImportTeacherController');

在两个控制器中,我目前都只填充了index()

In both the controllers I currently just have the index() filled

public function index()
{
   return Inertia::render('Import/Students');
}

public function index()
{
    return Inertia::render('Import/Teachers');
}

在教师和学生"的vue文件中,我具有这些页面的基本布局和标题,因此它们并不为空.

In the vue files for Teachers and Students I have basic layout and titles for those pages, so they're not empty.

当我单击<a :href="">按钮时,我将重定向到链接,但页面完全空白,而当我单击另一个按钮时,它就像在内部的一个窗口一样打开了.

When I click on the <a :href=""> button I get redirected to the link but the page is totally blank and when I click on the other button it opens up like a window inside also blank.

解决此问题的正确方法是什么?

What is the correct way to fix this?

推荐答案

链接

在InertiaJS中创建链接非常简单.它具有一个自定义标签,以表示它属于框架的范围.

Links

Creating a link in InertiaJS is pretty straight-forward. It has a custom tag as to denote that it's something that falls into the domain of the framework.

<inertia-link href="/">Home</inertia-link>

内部有一个<a>标签,这也意味着所有传递的属性都将发送到该基础<a>标签.

Under the hood there is a <a> tag, which also means that all attributes passed will be sent to that underlying <a> tag.

如果您真正要寻找的只是一个简单的链接单击-而不是重定向本身-那么使用上述代码就可以了.

If all you're really looking for is a simple link-click - and not a redirect per se - then you're fine with the above code.

如果您对重定向感兴趣,例如在更新用户或类似操作之后,则只需像在其他任何Laravel应用程序中一样使用Redirect门面就足够了.

If you're instead interesting in a redirect - for example after updating a user or the something similar - then simply using the Redirect facade like you would do in any other Laravel application is sufficient.

class UsersController extends Controller
{
    public function store()
    {
        User::create(
            Request::validate([
                'name' => ['required', 'max:50'],
                'email' => ['required', 'max:50', 'email'],
            ])
        );

        return Redirect::route('users');
    }
}

如果您已安装InertiaJS laravel适配器包,则此重定向将返回303状态码,该状态码与302状态码相同,不同之处在于该请求已更改为GET请求.

If you've installed the InertiaJS laravel adapter package, then this redirect will return a 303 status code, which is the same as a 302 status code, except that the request is changed into a GET request.

希望这会有所帮助!

这篇关于正确地惯性重定向到另一页?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆