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

Laravel+Vue.jsでコンポーネントを使わずにバインドするには

Last updated at Posted at 2021-08-20

Laravelのbladeでvueコンポーネントを使わずに直接vueをバインドしたいなぁ。
けど、bladeで{{ xxxx }}を書くと、phpの変数参照でエラーになるので、その解決方法です。

Laravel+Vue.jsでコンポーネントを使わずにバインドするには

その1

頭に@ つける

@{{ xxxx }}

参考サイト
https://blog.capilano-fw.com/?p=8367#BladeVue

その2

”Mustache” 構文(二重中括弧) {{ xxxx }} を変更する

Vue.jsでマスタッシュ記法で記述するデリミタを変更する方法

Vue.js 2系の場合

new Vue({
    el: '#app',
    data: data,
    delimiters: ["<%","%>"]
});

その3

v-text属性でバインドする。
<span v-text="xxxx"></span>

参考サイト
https://blog.capilano-fw.com/?p=8367#Vue

おまけ:非同期の値が再描画されない

JavaScript - axiosでポストして帰ってきたデータがVueのv-modelに再描画されない|teratail

原因

原因としてはthen内でthisが指しているのは Vue instance ではないからです。console.logしてみると確かめられます。
解決策としては、3つぐらいあります。

対応

arrow function

.then(response => {
  // "this" points to the vue instance
})

bind

.then(function (response) {
  // "this" points to the vue instance
}.bind(this))

this の退避

const self = this

// ...
.then(function (response) {
  // "self" points to the vue instance
})

おまけ2:Laravelではaxiosで非同期通信するとCSRFトークンは自動でセットしてくれる

LaravelのBladeにVueコンポーネントを組み込む - やんばるテック

なお、axiosによる非同期通信のリクエストヘッダのX-XSRF-TOKENには、Laravelのクッキーから取得したCSRFトークンの値が自動でセットされます。

参考URL

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?