在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問答/HTML/ vue data的數(shù)據(jù)發(fā)生改變,列表渲染出的數(shù)據(jù)沒有隨之改變,手動(dòng)刷新才有變化,

vue data的數(shù)據(jù)發(fā)生改變,列表渲染出的數(shù)據(jù)沒有隨之改變,手動(dòng)刷新才有變化,什么原因呢?

實(shí)現(xiàn)一個(gè)個(gè)人博客的增刪改查,博客內(nèi)容是頁面創(chuàng)建時(shí)axios請求后端接口的,刪除時(shí)axios.delete,后臺的數(shù)據(jù)也發(fā)生了變化,可是列表渲染出的數(shù)據(jù)并沒有相應(yīng)發(fā)生改變,需要手動(dòng)刷新頁面才有變化。
部分代碼如下:

    <ul>
      <router-link
        v-for="blog in blogs" 
        :key="blog.id" 
        class="singleArticle"
        tag="li"
        :to="'/blog/'+blog.id"
        @click.native="handleBlogClick(blog)"
      >
        <h2>{{blog.blogTitle}}</h2>
        <div>
          <span>分類:{{blog.classify.toString()}}</span>
          <span>類型:{{blog.type}}</span>
        </div>
        <p>{{blog.blogContent}}</p>
        <div class="handleBlog">
          <router-link :to="'/editBlog/'+blog.id" class="handleOption" @click="handleEdit">編輯</router-link>
          <router-link to="/articleList" 
          @click.native="handleDelete(blog.id)" class="handleOption">刪除</router-link>
        </div>
      </router-link>
    </ul>
 methods: {
    handleBlogClick(blog) {
      this.$store.dispatch("changePresentBlog",blog);
    },
    getData() {
      axios.get("http://localhost:3000/article")
        .then((data) => {
          this.blogs = data.data.reverse();
        })
    },
    handleDelete(id) {
      axios.delete("http://localhost:3000/article/"+id)
        .then(() => {
          console.log("刪除成功");
        })
      this.getData();
    }
  },
  created() {
    this.getData();
  }

刪除第一條數(shù)據(jù)時(shí),控制臺顯示刪除成功,后臺該條數(shù)據(jù)也成功刪除

clipboard.png
但視圖并沒有更新,請問是什么原因呢?
實(shí)現(xiàn)這種效果有什么比較好的建議嗎?小白求助

回答
編輯回答
孤島

你把拉取數(shù)據(jù)放在del的回調(diào)里面。你現(xiàn)在存在一個(gè)問題,如果刪除慢于查詢。那么查到的數(shù)據(jù)不對。

axios.delete("http://localhost:3000/article/"+id)
    .then(() => {
      console.log("刪除成功");
      this.getData();
    })
    
2017年9月9日 10:41
編輯回答
忠妾

理解啦,感謝!

2018年6月3日 09:48