Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
703 views
in Technique[技术] by (71.8m points)

php - Livewire x-slot tag in component

I search to use x-slot in Livewire component to populate my sidebar with wire:model form inputs.

My edit.blade.php file:

<x-app-layout>
    <x-slot name="header">
        <h2 class="font-semibold text-xl text-gray-800 leading-tight">
            {{ __('Header') }}
        </h2>
    </x-slot>

    <div>{{ $sidebar }}</div>

    <div class="bg-white">
        <livewire:editor-component :post="$post" />
    </div>
</x-app-layout>

My Livewire component:

<div>
    <x-slot name="sidebar">
        <div class="bg-gray-100">
            Some text... Some inputs...
        </div>
    </x-slot>

    <div class="p-5">
        <livewire:other-component />
    </div>
</div>

And my component:

<?php

namespace AppHttpLivewire;

use AppModelsPost;
use LivewireComponent;

class EditorComponent extends Component
{
    /** @var Post */
    public $post;
}

I want access to $post in sidebar x-slot and I should be able to write in input and manage values of component from sidebar.

question from:https://stackoverflow.com/questions/65920764/livewire-x-slot-tag-in-component

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can access $post inside your Livewire view directly as it is a public property.

As example you can output the id of your $post, given that there is an id property on your $post, like this:

<div>
    <x-slot name="sidebar">
        <div class="bg-gray-100">
            Some text... Some inputs...
        </div>
    </x-slot>

    Post id: {{ $post->id }}

    <div class="p-5">
        <livewire:other-component />
    </div>
</div>

How to access properties is written down in the Livewire docs

Access properties across components, can be done by utilizing Livewire Events


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...