Laravel 8从局部视图传递变量到父视图

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

Laravel 8 passing variables from partial view to parent view

问题

我正在使用laravel 8.74,并尝试从子视图传递数据到父视图的guest布局。

子视图使用<x-guest-layout>标记来继承guest视图。

我以为我可以使用属性包和@props([])来从子视图获取和设置变量到父视图。

子视图或部分视图

<x-guest-layout :meta_description="$obj->DescriptionToString( 150 )">

虽然属性包确实起作用。

Guest视图

<x-meta name="description" :content="$attributes->get('meta_description')"/>

当使用@props时,只有在变量具有默认值时才能访问变量,否则会出现“未定义变量”的错误。

Guest视图

@props(["meta_description" => "A description"])

这到目前为止与组件的行为相同,这正是我所期望的。

如果@props函数存在,那么“meta_description”变量将不再存在于属性包中,这是正常的。但如果我不定义默认值并且从子视图传递的值会被覆盖,那么变量将不存在。

我是否忘记或误解了什么?

英文:

I am using laravel 8.74 and I am trying to pass data from a child's view to the parent view guest layout.

The child view is using <x-guest-layout> tag to inherit the guest view.

This way I was assuming I can use the attributes bag and the @props([]) to get and set the variables to the parent view.

Child or partial view

&lt;x-guest-layout :meta_description=&quot;$obj-&gt;DescriptionToString( 150 )&quot;&gt;

While the attributes bag does work.

Guest view

&lt;x-meta name=&quot;description&quot; :content=&quot;$attributes-&gt;get(&#39;meta_description&#39;)&quot;/&gt;

When using the @props I can only access the variable if it has a default value otherwise, I get “undefined variable.

Guest view

@props([&quot;meta_description&quot; =&gt; &quot;A description&quot;])

It’s so far, the same behavior as a component and that is what I was expecting.

If the @props function exists, then the “meta_description” variable does not exist anymore in the attributes bag, which is normal. But again does not exist if I do not define a default value and the value passed from the child view is overwritten.

Is there something I forgot or misunderstood?

答案1

得分: 0

"Dough!!" 意思是 "哎呀!" 或者表示惊讶的声音。

"One of my Beaker moments..." 意为 "我生活中的其中一个 'Beaker' 时刻..."

"Just realized that the guest and app layouts have class component." 意为 "刚刚意识到访客和应用程序布局都使用了类组件。"

英文:

Dough!!
One of my Beaker moments...

Just realized that the guest and app layouts have class component.

App/View/Components

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

发表评论

匿名网友

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

确定