1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Laravelのディレクティブや変数をVueコンポーネントで使いたいときはslotが便利

Last updated at Posted at 2022-01-25

環境:vue2

Laravel側のディレクティブや変数の内容をVueのコンポーネントの中で使いたい場合、
propsで頑張るのではなく、slotを使ったほうがシンプルに実装できるかも。
↓こんな感じ(必要な個所以外は省略しています)

blade側
<side-menu>
      @auth
      <ul class="side-menu-inner">
        <li><a href='{{ route("users.show", ["uid" => Auth::user()->uid]) }}'>マイページ</a></li>
        <li><a href='{{ route("users.info") }}'>通知</a></li>
      @endauth
      @guest
      <ul class="side-menu-inner">
        <li><a href='{{ route("users.register") }}'>新規登録</a></li>
        <li><a href='{{ route("users.login") }}'>ログイン</a></li>
      </ul>
      @endguest
</side-menu>
SideMenu.vue
<template>
    <div>
       <slot></slot>
    </div>
</template>

特に@guest@authの出し分けなんかは、slot無しでやろうと思ったらbladeからAuth::check()の結果をvueにpropsで渡して、v-ifで出し分けて~とかになってしまうので。

なお、Laravelからjsへの値の受け渡し方法については、下記記事様がわかりやすくまとめてくださってます。

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?