jdw5 / surge-toast
一个Vue 3 + Inertia插件,用于渲染无头Toast组件。
v0.1.0
2024-02-21 11:26 UTC
Requires
- php: ^8.1
- illuminate/session: ^10.45
- illuminate/support: ^10.45
README
Vue/Inertia的surge-toast-client的服务端对应插件。它提供了一个全局宏来向会话中发送数据,客户端库会自动连接到InertiaJs接收路由事件。
安装
通过composer安装此包
composer require jdw5/surge-toast
要提供到前端链接,请确保您有通过Inertia在所有请求中共享props的中介。入门套件将安装为HandleInertiaRequests中介。将以下内容复制到该中介中的share函数,或等效函数
public function share (Requesr $request): array { return [ ...parent::share($request), 'toast' => $request->session()->get('toast') ] }
这会将所有Toast消息共享到您的前端,属性名为toast。要使用客户端库,安装后请在app.js文件中使用该插件。有关JavaScript库的文档,请参阅此处
用法
该包提供了一个全局方法来通过ToastData结构向会话发送数据。ToastData接受以下参数
public function __construct( public string $message, public ?string $type = ToastType::DEFAULT->value, public ?int $duration = self::DEFAULT_DURATION, public ?string $title, public ?mixed $custom, ) {}
您可以通过以下方式从控制器中调用该方法
toast(new ToastData('A message', 'info')); toast(ToastData::from([ 'message' => 'A message', 'duration' => 5000, 'title' => 'A title', 'custom' => ['key' => 'value'] 'type' => 'info', ])); toast([ 'message' => 'Error message', 'type' => 'error', 'duration' => 5000, ]);
或这些的组合。这将向会话发送一个具有提供的type的消息,如果没有指定,将使用default类型
您还可以使用以下方法来简化类型
toast()->info('A message'); toast()->success('A message'); toast()->error('A message'); toast()->warning('A message');
这将为您设置类型。您还可以与duration和title方法一起使用
toast()->info('A message')->duration(5000)->title('A title');
许可
MIT许可(MIT)。有关更多信息,请参阅许可文件。