简体   繁体   English

Laravel 8.15.0/Jetstream - 如何注册新刀片 x-jet-newblade?

[英]Laravel 8.15.0/Jetstream - How to register new blades x-jet-newblade?

I am just doing my very first steps with Laravel 8 and found a problem that I can not solve.我只是在用 Laravel 8 做我的第一步,发现了一个我无法解决的问题。

/var/www/html/laravel/resources/views/dashboard.blade.php : /var/www/html/laravel/resources/views/dashboard.blade.php

    <div class="py-12">
    <div class="max-w-7xl mx-auto sm:px-6 lg:px-8">
        <div class="bg-white overflow-hidden shadow-xl sm:rounded-lg">
            <x-jet-welcome />
        </div>

If i create a new blade in the same directory (fe the form.blade.php) with the same code as above but with <x-jet-subform/> instead of <x-jet-welcome> it should normally redirect to the subform.blade.php which is located under var/www/html/laravel/resources/views/vendor/jetstream/components/subform.blade.php如果我在同一个目录(例如 form.blade.php)中创建一个新的刀片,其代码与上面相同,但使用<x-jet-subform/>而不是<x-jet-welcome>它通常应该重定向到subform.blade.php 位于var/www/html/laravel/resources/views/vendor/jetstream/components/subform.blade.php

But if I try to get to that page (after setting a Route at web.php) it says但是,如果我尝试访问该页面(在 web.php 上设置路由后),它会说

InvalidArgumentException无效参数异常
Unable to locate a class or view for component [jet-subform].无法找到组件 [jet-subform] 的类或视图。

So I think it's necessary to "register" new blades but I found no way to do that...所以我认为有必要“注册”新刀片,但我发现没有办法做到这一点......

The view is already published with该视图已发布

php artisan vendor:publish --tag=jetstream-views

I was dealing with the same problem here and found your question unanswered.我在这里处理同样的问题,发现您的问题没有得到解答。 The solution I found was to create my own new Blade component.我找到的解决方案是创建我自己的新 Blade 组件。 You can do that using:你可以使用:

$ php artisan make:component MyComponent

This will create two new files /resources/views/components/my-component.blade.php and /app/View/Components/MyComponent.php.这将创建两个新文件 /resources/views/components/my-component.blade.php 和 /app/View/Components/MyComponent.php。 Now you just need to build your component on that blade file and reference it using the x-tag like this: <x-my-component></x-my-component>现在您只需要在该刀片文件上构建您的组件并使用 x-tag 引用它,如下所示: <x-my-component></x-my-component>

This is how the blade component code should look like这就是刀片组件代码的样子

<button {{ $attributes->merge(['type' => 'button', 'class' => 'some-classes']) }}> {{ $slot }} </button>

Hope it helps.希望能帮助到你。 Greetings from Brazil :)来自巴西的问候 :)

You can register your jetstream blade components in App\\Providers\\JetstreamServiceProvider.php located in app\\Providers folder.您可以在位于app\\Providers文件夹中的App\\Providers\\JetstreamServiceProvider.php注册您的 jetstream 刀片组件。

Add the following helper function to the file:将以下辅助函数添加到文件中:

protected function registerComponent(string $component) {
    \Illuminate\Support\Facades\Blade::component('jetstream::components.'.$component, 'jet-'.$component);
}

Then use the following snippet in register function to register your jetstream blade components:然后在 register 函数中使用以下代码段来注册您的 jetstream 叶片组件:

public function register() {
    $this->registerComponent('subform');
}

Now you can use your custom jetstream component:现在您可以使用您的自定义 jetstream 组件:

<x-jet-subform>

I am not sure whether it's the correct or intended way to add new custom x-jet components here as this method may not survive an update, but you can register new components in this file:我不确定在此处添加新的自定义 x-jet 组件是否正确或有意,因为此方法可能无法在更新后继续使用,但您可以在此文件中注册新组件:

vendor/laravel/jetstream/src/JetstreamServiceProvider.php . vendor/laravel/jetstream/src/JetstreamServiceProvider.php

Add, $this->registerComponent('subform');添加, $this->registerComponent('subform'); to the configureComponents method, and then call it with an <x-jet-subform> tag到 configureComponents 方法,然后使用<x-jet-subform>标签调用它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM