簡體   English   中英

Vue.js更新Data屬性變量中的Ajax數據

[英]Vue.js update ajax data in data property variable

我是Vue.js的新手。 我想從ajax數據更新數據。

<template>
  <span class="label_cont">This is to certify that</span> {{ name }}
</template>

我想從ajax數據中{{ name }}變量。
這是我的js代碼

export default{
  created(){
    const url = 'app/result.php';
    this.$http.get(url).then(response => {
      this.resp = response.body;
    });
  },
  data(){  
    return {
     resp:[],
     name: resp.name,
   }
  }
}

在我的ajax name屬性位於this.resp.name

更新:

這是我的ajax響應數據格式

{
  "name": "X",
  "class": "Y",
  "roll": "Z"
}

在下面的回調中添加額外的行。

this.$http.get(url).then(response => {
  this.resp = response.body;
  this.name = response.name
});

在您的數據中,只需將其初始化為null。

data(){  
  return {
    resp:[],
    name: null,
  }
}

或者,您可以只使用

{{resp.name}}

在您的模板中。

暫無
暫無

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

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