在Livewire中是否可以像普通控制器一样进行双重渲染?

huangapple go评论49阅读模式
英文:

is it posible to double render in livewire like a normal controller?

问题

我是新手,我有一个简单的问题,让我彻夜难眠,所以在Livewire中是否可以像正常的控制器一样进行双重渲染?

像这样

//Home Controller

public function index(){
    return view('home.index');
}

public function dragonCat(){
    return view('blah.index');
}
英文:

im newbie here, I have a simple question that keeps me up all night,so is it posible to double render in livewire like a normal controller?

like this

//Home Controller

public function index(){
return view(home.index);
}

public function dragonCat(){
return view(blah.index);
}

答案1

得分: 2

不是以您展示的方式(使用单独的方法),不是这样的。这也不是 Livewire 的预期工作方式。然而,如果您有一个想要这样做的原因,也许我可以给您提供一个更好的方法建议。

只是提供信息,控制器是您的 HTTP 路由和视图之间的桥梁。尽管您在技术上不需要控制器(因为您定义的路由只需要一个回调),但最初您会使用控制器来保持事物有序(例如,所有与产品模型相关的路由),以及定义更复杂的逻辑,否则会使您的 web.php 混乱不堪。

Livewire 组件是前端的“实时”部分,一个响应式组件,基本上是一个非常复杂和高质量的 AJAX 封装器,用于在客户端和服务器之间进行通信。它使用 DOM 差异比对以确保所需的最小载荷。如果您在渲染方法中更改视图,也许会起作用,但这很可能会非常慢,因为这意味着替换整个视图。

因此,虽然控制器“技术上”可能有(或可以有)多个 return view(),但您始终只会同时调用一个,因为它与您当前所在的路由相关。Livewire 组件也只能有一个 return view(),但在单个页面上拥有多个 Livewire 组件并没有什么不好的。

英文:

Not in the way that you're showing (using separate methods), no. And that's also not how Livewire is meant to work. However, if there's a reason why you would want that, perhaps I can give you a suggestion on a better approach.

Just for info, a controller is a bridge between your HTTP routes and your views. While you technically don't need a controller (as the routes you define just need a callback), you use a controller at first to keep things organized (e.g. all Product model related routes) as well as to define more complex logic that would otherwise clutter your web.php.

A Livewire component is a "live" piece of frontend, a reactive component, basically a very complex and high quality AJAX wrapper to communicate between the client and the server. It uses DOM diffing to ensure minimal payload needed. If you were to change the view in the render method, maybe that would work, but it would most likely be very slow as it would mean replacing the whole view.

So where a controller "technically" has (or can have) more than one return view(), you would always only ever call one at the same time, since it would be related to the current route you're on. A Livewire component can also only have one return view(), however there is nothing bad about having more than one Livewire component on a single page.

huangapple
  • 本文由 发表于 2023年1月9日 01:33:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/75049960.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定