13
12

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 3 years have passed since last update.

Vue.jsとLaravelで/public以下の画像ファイルなどを呼び出したい

Last updated at Posted at 2019-11-19

結論

<template>
  <div>
    <img :src="'/img/image.jpg'">
  </div>
</template>

解説

1. Vue.jsから見た/ルートは/public

appName/public/img/image.jpgにあるファイルは
/img/image.jpgで呼び出せます。

補足: /publicはlaravelで作製したディレクトリで/app/routesと同じ階層のことだよ!

これはコンパイルするディレクトリが指定されているからだね!
Laravelなら多分laravel-mix使ってるかな?
webpack.mix.jsってファイルに個別設定とrequire()で呼び出されてるものがあるから追ってみるといい!

2. :src=""の形式でpathを文字列で指定する

src='/img/image.jpg'で書くとコンパイルすらさせて貰えなかったりするよ!

だから:src=""のVue.jsの書き方で書いてあげないとうまく認識できないんだ!

さらに:src="'文字列'"としてダブルクオートの中にシングルクオートを書いて文字列として
渡して上げないと「そんな変数ねぇーじゃんwww」と怒られちゃう。

13
12
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
13
12

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?