英文:
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
<x-guest-layout :meta_description="$obj->DescriptionToString( 150 )">
While the attributes bag does work.
Guest view
<x-meta name="description" :content="$attributes->get('meta_description')"/>
When using the @props I can only access the variable if it has a default value otherwise, I get “undefined variable.
Guest view
@props(["meta_description" => "A description"])
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论