[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 应用程序开发的状态管理模式.它采用集中式存储管理应用的所有组件的状态,并以相应的规则保证状态以一种可预测的方式发生变化. 简单来说就 ...
随机推荐
- 运动识别之HOJ3D和HMM
http://cvrc.ece.utexas.edu/Publications/Xia_HAU3D12.pdf View Invariant Human Action Recognition Us ...
- CMDB学习之一
CMDB - 配置管理数据库 资产管理 自动化相关的平台(基础 CMDB): 1. 发布系统 2. 监控 3. 配管系统.装机 4. 堡垒机 CMDB的目的: 1. 替代EXCEL资产管理 —— 资产 ...
- 利用JAVA反射机制实现调用私有方法
1.fragment是AccessibilityFragment的對象.须要被調用的方法的類. setAccessible(true)并非将方法的訪问权限改成了public.而是取消java的权限控制 ...
- Autodesk 招聘Revit二次开发咨询顾问,与Autodesk全球团队紧密合作,提高职业生涯的好机会
朋友们, 因为我离开Autodesk的全职工作(变为部分时间工作),我的职位空出.急招这个职位.请踊跃把你周围的朋友推荐给Autodesk. 请将简历发给我转交给Autodesk 我的邮箱yexion ...
- LinearLayout-控件不显示
今天Mms遇到了一个问题,布局如下 <RelativeLayout android:layout_width="match_par ...
- jmeter响应数据中文乱码问题
进入jmeter安装文件目录:D:\Program File\apache-jmeter-2.13\apache-jmeter-2.13\bin\ 修改jmeter.properties文件,在最下方 ...
- HDU4630-No Pain No Game(离线,线段树)
Problem Description Life is a game,and you lose it,so you suicide. But you can not kill yourself bef ...
- 1.字符设备驱动------Linux中断处理体系结构
一.中断处理体系结构的初始化 Linux内核将所有的中断统一编号,使用一个irq_desc结构数组来描述这些中断;每个数组项对应一个中断,也可能是一组中断,它们共用相同的中断号,里面记录了中断的名称. ...
- Mysql 简介二
Mysql 数据库引擎: 数据库引擎是用于存储.处理和保护数据的核心服务 Mysql支持的引擎一般有这几种: MyISAM Mysql 5.1版本之前默认的存储引擎,仅仅支持表锁,但查询速度较Inno ...
- CISP/CISA 每日一题 六
CISA 每日一题(答) 采用电子数据交换 EDI 的好处: 1.较少的书面工作: 2.较少的信息交换错误: 3.改善了数据库到数据库.公司到公司的信息流: 4.没有多余的数据重新键入: 5.较少的通 ...