首页 > 解决方案 > 使用整页组件时如何更改页面标题-Livewire

问题描述

<head>
   <title>dynamic title here - {{ config('app.name') }}</title>
</head>

如果你试试这个,请告诉我。

标签: laravel-livewirelivewires

解决方案


我更喜欢将 JavaScript 与 Blade 组件一起使用document.title = 'My Title'

Alpine JS实现

刀片组件 (.../components/page-title.blade.php)

@props(['title'])

<div x-data x-init="document.title = @js($title.' - '.config('app.name'))"></div>

page-title所以你可以像这样在你的刀片苍蝇中的任何地方调用组件

<x-page-title :title="'Homepage'" />

可能有更简单的方法可以通过 JavaScript 方式进行,但这是我能想到的。希望能帮助到你

PHP的方式可能是将$title作为道具传递给布局,例如

<x-guest-layout>
    <x-slot name="title">
       Custom Title
    </x-slot>

    ...Page Content
</x-guest-layout>

guest-layout.blade.php现在看起来像

<html>
<head>
    <title>{{ $title ?? 'Todo Manager' }}</title>
</head>
<body>
    <h1>Todos</h1>
    <hr/>
    {{ $slot }}
</body>

推荐阅读