[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 @submit.prevent
syntax to avoid that behavior. Then wire together the @submit
event with an add
Vuex action to handle an async post to an api. This lesson walks you through posting to an API using Vue.js, Vuex. and Nuxt.
<template>
<form @submit="onSubmit(task)">
<input v-model="task" type="text" />
</form>
</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: {
onSubmit(task) {
alert(task)
}
}
}
</script>
Notice that after reload the page, when the form submit, the page reloads.
To prevent page reloads every time we submit the form we can use:
<form @submit.prevent="onSubmit(task)">
To deal with the store, we can use 'actions' in Vuex.Store and 'mapActions' helpers:
<template>
<div>
<form @submit.prevent="add(task)">
<input v-model="task" type="text" />
</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="ph3 pv3 bb b--light-silver">{{todo.task}}</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'
])
}
}
</script>
We change form submit to:
<form @submit.prevent="add(task)">
The 'add' method map to action in store:
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
]
}
},
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)
}
}
}) export default store
Inside, add function, we post the data to the backend, then we can use 'commit' method to mutate the data in the store.
[Nuxt] Build a Vue.js Form then use Vuex Actions to Post to an API in Nuxt的更多相关文章
- vue.js和vue-router和vuex快速上手知识
vue.js和vue-router和vuex快速上手知识 一直以来,认为vue相比react而言,学习成本会更低,会更简单,但最近真正接触后,发现vue的各方面都有做一些客户化的优化,有一些亮点,但也 ...
- Vue.js——十分钟入门Vuex
一. 什么是Vuex? Vuex Vuex是一个专门为Vue.js应用程序开发的状态管理模式, 它采用集中式存储管理所有组件的公共状态, 并以相应的规则保证状态以一种可预测的方式发生变化. Vue ...
- Vue.js状态管理模式 Vuex
vuex 是一个专为 Vue.js 应用程序开发的状态管理模式.它采用集中式存储管理应用的所有组件的状态,并以相应的规则保证状态以一种可预测的方式发生变化. 安装.使用 vuex 首先我们在 vue. ...
- [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. ...
- 关于Vue.js 2.0 的 Vuex 2.0,你需要更新的知识库
应用结构 实际上,Vuex 在怎么组织你的代码结构上面没有任何限制,相反,它强制规定了一系列高级的原则: 应用级的状态集中放在 store 中. 改变状态的唯一方式是提交mutations,这是个同步 ...
- Vue.js中学习使用Vuex详解
在SPA单页面组件的开发中 Vue的vuex和React的Redux 都统称为同一状态管理,个人的理解是全局状态管理更合适:简单的理解就是你在state中定义了一个数据之后,你可以在所在项目中的任何一 ...
- Build a Basic CRUD App with Vue.js and nodejs
https://developer.okta.com/blog/2018/02/15/build-crud-app-vuejs-node#add-authentication-with-okta I’ ...
- [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 学习笔记 一
本文的Demo和源代码已放到GitHub,如果您觉得本篇内容不错,请点个赞,或在GitHub上加个星星! https://github.com/zwl-jasmine95/Vue_test 以下所有知 ...
随机推荐
- 41.内存函数实现(memcpy,memset,memmove,memicmp,memchr.memccpy)
memcpy #include <stdio.h> #include <stdlib.h> #include <memory.h> void * mymemcpy( ...
- 分享一个vue常用的ui控件
vue学习文档 http://www.jianshu.com/p/8a272fc4e8e8 vux github ui demo:https://github.com/airyland/vux M ...
- nodejs学习(二)--express热更新nodemon,自启动项目
一.说一下 每次修改文件,我们都需要重启服务器npm start,很麻烦,所以使用引入nodemon插件,解决这个问题,实现保存文件,即自启动刷新项目 二.直接开码 npm install nodem ...
- LuoguP4012 深海机器人问题(费用流)
题目描述 深海资源考察探险队的潜艇将到达深海的海底进行科学考察. 潜艇内有多个深海机器人.潜艇到达深海海底后,深海机器人将离开潜艇向预定目标移动. 深海机器人在移动中还必须沿途采集海底生物标本.沿途生 ...
- shell项目-分发系统-expect讲解
shell项目-分发系统-expect讲解 yum install -y expect 1. 自动远程登录 #! /usr/bin/expect set host "192.168.133. ...
- 今日SGU 5.10
SGU 168 题意:从a矩阵求出b矩阵,规则直接看题目就行了,不好打字说明 收获:dp #include<bits/stdc++.h> #define de(x) cout<< ...
- UITouch 的使用
直接上代码: touch 的四大状态.: // // TouchView.m // UI_practice_04 // // Created by lanouhn on 15/4/22. // Cop ...
- 3.SOAP和WSDL的一些必要知识
转自:https://www.cnblogs.com/JeffreySun/archive/2009/12/14/1623766.html SOAP和WSDL对Web Service.WCF进行深入了 ...
- Codefroces 760 B. Frodo and pillows
B. Frodo and pillows time limit per test 1 second memory limit per test 256 megabytes input standard ...
- 对ng-repeat的表格内容添加不同样式:ng-style
对ng-repeat的表格内容添加不同样式,html代码: <tr ng-repeat="x in tableData"> <td>{{x.networkN ...