簡體   English   中英

在Vue JS Laravel組件中傳遞數據屬性

[英]Passing data properties in vue js laravel component

Vue.js的新手,我想將數據屬性值傳遞給我的模板。

我有:

export default {
  name: "profile",
  data: () => ({
    user:[]
  }),
  mounted() {
    axios
      .get('/api/user')
      .then(function (response) {
        this.user = response
      })
      .catch(function (response) {
        console.error(response);
      });
  }
}

在HTML模板中,我有:

<template>
  <div>{{user.name }}</div>
</template>

現在我得到一個錯誤

無法將屬性user設置為undefined

console.log(response)產生:

名稱:“用戶1”,//名稱存在
電子郵件...

嘗試將arrow function用於axios回調

.then((response) => {
    this.user = response
}) 

arrow function按預期方式綁定了組件上下文。

已安裝:

mounted() {
    var self = this;
    axios.get('/api/user')
        .then(function (response) {
           self.user = response
        })
        .catch(function (response) {
           console.error(response);
        });
     }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM