2017-11-02 16 views
-1

解決しよう:それは新しい状態に更新するような機能にthis.tasksの=のresp.dataを追加することで...更新DOMは、API要求後の結果をレンダリングする

私は現在、単純なtodo-に取り組んでいますvuejsのリストアプリと私はapiのリクエストを行った後、円滑な方法でdomを更新する方法を探しています。私が変更の直後に変更を表示できる唯一の方法は、応答にlocation.reload()を置くことです。私はいくつかの例やガイドを見てきました。人々は.bind()でこれを行うことができるようですが、私のためには機能しません。

//deletePost works for displaying changes but i don't want the page to flash on every update 
deletePost(id) { 
    axios.delete(`http://localhost:3000/tasks/${id}`) 
    .then((resp) => { 
    console.log(resp.data) 
    location.reload(); 
    }) 
    .catch((err) => { 
    console.log(err) 
    }) 
}, 
    //this is how i've seen people doing it, but it's not working for me. 
    updatePost(selected, id) { 
    axios.put(`http://localhost:3000/tasks/${id}`,{ status: selected }) 
    .then(function(response){ 
     console.log('saved successfully') 
     }.bind(this)); 
    } 
}, 

+1

ページを再読み込みする代わりに、状態のローカルビューをどこで更新していますか?私は、あなたの削除/更新メソッドの 'then'部分に' this.posts'を修正することを期待しています。 – Botje

答えて

0

あなたはあなたのVueコンポーネントにタスクの配列を持っています。 http要求を送信すると、サーバーからリソースが削除されますが、ローカルには削除されません。コンポーネント内部のリソースを削除するには、次のように例えば.then()の部分でそれを行う必要があります:

data() return { 
    tasks: [] 
}, 

deletePost(id) { 
    axios.delete(`http://localhost:3000/tasks/${id}`) 
    .then((resp) => { 
    console.log(resp.data) 
    // Here we delete the task from the component locally 
    // Note that we only want to delete it if the request is successful 
    let index= this.tasks.find(task => task.id === id) 
    this.tasks.splice(index,1); 
    }) 
    .catch((err) => { 
    console.log(err) 
    }) 
}, 
    //this is how i've seen people doing it, but it's not working for me. 
    updatePost(selected, id) { 
    axios.put(`http://localhost:3000/tasks/${id}`,{ status: selected }) 
    .then(function(response){ 
     console.log('saved successfully') 
     }.bind(this)); 
    } 
}, 
+0

コンポーネントをローカルで削除するだけで、例を追加しました。それがうまくいくか試しましたか? –

+0

要求は機能しますが、依然として要求後に更新されません。 –

0

おかげでこんなに早くここで私の最初のポストに返信するために、もちろん私はべきでした

data() { 
    return { 
     tasks: [], 
     formValue: '', 
     selected: '' 
    } 
    }, 
    created() { 
    this.fetchData() 
    }, 
    watch: { 
    '$route': 'fetchData', 
    }, 
    methods: { 
    fetchData() { 
     axios.get('http://localhost:3000/tasks') 
     .then((resp) => { 
     this.tasks = resp.data 
     console.log(resp.data) 
     }) 
     .catch((err) => { 
     console.log(err) 
     }) 
    }, 
    deletePost(id) { 
     axios.delete(`http://localhost:3000/tasks/${id}`) 
     .then((resp) => { 
     console.log(resp.data) 
     // Here we delete the task from the component locally 
     // Note that we only want to delete it if the request is successful 
     let index= this.tasks.find(task => task.id === id) 
     this.tasks.splice(index,1); 
     }) 
     .catch((err) => { 
     console.log(err) 
     }) 
    }, 
     //updates post status 
     updatePost(selected, id) { 
     axios.put(`http://localhost:3000/tasks/${id}`,{ status: selected }) 
     .then(function(response){ 
      console.log('saved successfully') 
      }.bind(this)); 
     } 
    }, 
    } 
+0

私はあなたの仕事がどのようなものか分かりません。それは単なる例です。コンポーネント状態でそれを削除するコードは、特定のIDを持ち、そのインデックスを持つタスクのためにタスク配列を検索するということです。タスクのインデックスがわかると、アレイからタスクが削除されます。これは単なる例です。たとえば、タスクにidがなくてもIdまたはtaskIdがない場合は、この例は機能しません。 –

+0

すべてのタスクには、それらを削除して更新するために使用するIDがあります。 jsonobjectは次のようになります。 { "_id": "59fc296ce2e82702e744dd1c"、 "名前": "新しいタスク"、 "__v":0、 "ステータス": "保留"、 "Created_date":「2017 -11-03T08:31:40.805Z " } –

関連する問題