[Nuxt] Use Vuex Actions to Delete Data from APIs in Nuxt and Vue.js
You'll begin to notice as you build out your actions in Vuex, many of them will look quite similar. Creating a remove action looks almost the same as the add action except for using the axios.delete
method then filtering out the deleted todo once the response comes back.
Add a remove button to the todo list:
<template>
<div>
<form @submit.prevent="add(task)">
<input v-model="task" type="text" />
<input type="submit" value="ADD">
</form>
<article class="pa3 pa5-ns">
<ul class="list pl0 ml0 center mw6 ba b--light-silver br2">
<li v-for="todo of todos" class="flex items-center ph3 pv3 bb b--light-silver">
<span class="flex-auto">{{todo.id}} {{todo.task}}</span>
<button @click="remove(todo)"><img src="https://icon.now.sh/trash" alt="delete"></button>
</li>
</ul>
</article>
</div>
</template> <script>
import { mapState, mapActions } from 'vuex'
import {init} from './shared' export default {
fetch: init,
computed: {
...mapState({
todos: (state) => state.todos
})
},
data () {
return {
task: 'Some data'
}
},
methods: {
...mapActions([
'add',
'remove'
])
}
}
</script>
store/index.js:
import Vuex from 'vuex'
import axios from 'axios' const store = () => new Vuex.Store({
state: {
todos: [
]
},
mutations: {
init (state, todos) {
state.todos = todos
},
add (state, todo) {
state.todos = [
...state.todos,
todo
]
},
remove (state, todo) {
state.todos = state.todos.filter((t) => {
return t.id !== todo.id
})
}
},
actions: {
async add (context, task) {
const commit = context.commit
const res = await axios.post('https://todos-cuvsmolowg.now.sh/todos', {
task,
complete: false
})
commit('add', res.data)
},
async remove ({commit}, todo) {
await axios.delete(`https://todos-cuvsmolowg.now.sh/todos/${todo.id}`)
commit('remove', todo)
}
}
}) export default store
[Nuxt] Use Vuex Actions to Delete Data from APIs in Nuxt and Vue.js的更多相关文章
- [Nuxt] Build a Vue.js Form then use Vuex Actions to Post to an API in Nuxt
The default behavior of submitting an HTML form is to reload the page. You can use the Vue.js @submi ...
- [Nuxt] Load Data from APIs with Nuxt and Vuex
In a server-rendered application, if you attempt to load data before the page renders and the data f ...
- [Vue-rx] Disable Buttons While Data is Loading with RxJS and Vue.js
Streams give you the power to handle a "pending" state where you've made a request for dat ...
- [Nuxt] Update State with Vuex Actions in Nuxt.js
You can conditionally add classes to Vue.js templates using v-bind:class. This will help display the ...
- [Nuxt] Add Arrays of Data to the Vuex Store and Display Them in Vue.js Templates
You add array of todos to the store simply by adding them to the state defined in your store/index.j ...
- Nuxt使用Vuex
Vuex 是一个专为 Vue.js 应用程序开发的状态管理模式.它采用集中式存储管理应用的所有组件的状态,并以相应的规则保证状态以一种可预测的方式发生变化. 基础知识这里不再重述,学习的话请自行到官网 ...
- [Vuex] Perform Async Updates using Vuex Actions with TypeScript
Mutations perform synchronous modifications to the state, but when it comes to make an asynchronous ...
- [Nuxt] Update Vuex State with Mutations and MapMutations in Vue.js
You commit changes to state in Vuex using defined mutations. You can easily access these state mutat ...
- vue.js移动端app实战3:从一个购物车入门vuex
什么是vuex? 官方的解释是:Vuex 是一个专为 Vue.js 应用程序开发的状态管理模式.它采用集中式存储管理应用的所有组件的状态,并以相应的规则保证状态以一种可预测的方式发生变化. 简单来说就 ...
随机推荐
- 网页中插入javascript的几种方法
网页中插入javascript的方法常见的有两种: 一.直接使用html标记 JavaScript 可以出现在 html的任意地方.使用标记<script>…</script> ...
- Mvc异步
<h3>MVC 自带的yibu请求</h3> <%-- 1.要执行的方法,2.控制器,3.Ajax操作--%> <%using (Ajax.BeginForm ...
- 分享一下事件监听addEventListener----attachEvent的用法
来自:http://www.cnblogs.com/wkylin/archive/2011/10/09/2203161.html 事件监听addEventListener----attachEvent ...
- OpenCV —— 跟踪与运动
理解物体运动主要包含两个部分:识别和建模 识别在视频流后续的帧中找出之前某帧镇南关的感兴趣物体 寻找角点 可跟踪的特征点都称为角点,从直观上讲,角点(而非边缘)是一类含有足够信息且能从当前帧和下一帧中 ...
- 洛谷 P2242 公路维修问题
P2242 公路维修问题 题目描述 由于长期没有得到维修,A国的高速公路上出现了N个坑.为了尽快填补好这N个坑,A国决定对M处地段采取交通管制.为了求解方便,假设A国的高速公路只有一条,而且是笔直的. ...
- 迅雷云监工crysadm搭建
之前在淘宝众筹买了个迅雷赚钱宝Pro,2016年01月到手,玩了半个月. 之后在看百度贴吧时,看到好多大神都在用PC上用网页版软件来管理赚钱宝.非常是好奇.搜索了非常多资料,基本上都已经过时了.都无法 ...
- LeetCode_ZigZag Conversion
一.题目 ZigZag Conversion Total Accepted: 31399 Total Submissions: 140315My Submissions The string &quo ...
- 用for和while循环求e的值[e=1+1/1!+1/2!+1/3!+1/4!+1/5!+...+1/n!]
/*编敲代码,依据下面公式求e的值. 要求用两种方法计算: 1)for循环.计算前50项 2)while循环,直至最后一项的值小于10-4 e=1+1/1!+1/2!+1/3!+1/4!+1/5!+. ...
- [NOI.AC#30]candy 贪心
链接 一个直观的想法是,枚举最小的是谁,然后二分找到另外一个序列对应位置更新答案,复杂度 \(O(NlogN)\) 实际上不需要二分,因为每次当最大的变大之后,原来不行的最小值现在也一定不行,指针移动 ...
- php课程 12-39 继承中parent的作用是什么
php课程 12-39 继承中parent的作用是什么 一.总结 一句话总结:PHP5中使用parent::来引用父类的方法.parent:: 可用于调用父类中定义的成员方法. parent::的追溯 ...