[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 fails to load, your application will not run unless you handle the error properly. This lesson walks you through the options of handling load errors so that your users will always have a good experience.
<template>
<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>
</template> <script>
import { mapState } from 'vuex'
import axios from 'axios' export default { async fetch ({store, redirect}) {
try {
const res = await axios.get('https://todos-cuvsmolowg.now.sh/todoss')
store.commit('init', res.data)
} catch (err) {
redirect('/error')
// store.commit('init', [])
}
},
computed: {
...mapState({
todos: (state) => state.todos
})
}
}
</script>
There are three ways to handle loading data error:
1. try catch the async await:
try {
const res = await axios.get('https://todos-cuvsmolowg.now.sh/todoss')
store.commit('init', res.data)
} catch (err) {
store.commit('init', [])
}
2. Redirect to a error page:
<template>
<p>
There are some errors
</p>
</template> async fetch ({store, redirect}) {
try {
const res = await axios.get('https://todos-cuvsmolowg.now.sh/todos')
store.commit('init', res.data)
} catch (err) {
redirect('/error')
}
},
3. Default error page:
async fetch ({store, redirect, error}) {
try {
const res = await axios.get('https://todos-cuvsmolowg.now.sh/todos')
store.commit('init', res.data)
} catch (err) {
error({
statusCode: 500,
message: 'Something happened on server'
})
}
},
[Nuxt] Load Data from APIs with Nuxt and Vuex的更多相关文章
- [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. ...
- mysql load data 乱码
解决方案: http://stackoverflow.com/questions/26256421/sql-load-data-infile-utf8-issue 即: load data local ...
- Mybatis拦截器 mysql load data local 内存流处理
Mybatis 拦截器不做解释了,用过的基本都知道,这里用load data local主要是应对大批量数据的处理,提高性能,也支持事务回滚,且不影响其他的DML操作,当然这个操作不要涉及到当前所lo ...
- mysql load data 乱码的问题
新学mysql在用load data导入txt文档时发现导入的内容,select 之后是乱码,先后把表,数据库的字符集类型修改为utf8,但还是一样,最后在 http://bbs.chinaunix. ...
- mysql load data infile的使用 和 SELECT into outfile备份数据库数据
LOAD DATA [LOW_PRIORITY | CONCURRENT] [LOCAL] INFILE 'file_name.txt' [REPLACE | IGNORE] INTO TABLE t ...
- 快速的mysql导入导出数据(load data和outfile)
1.load data: ***实际应用:把日志生成的xls文件load到MySQL中: mysql_cmd = "iconv -c -f utf-8 -t gbk ./data/al_ve ...
- 记录load data infile 的用法
load data local infile 'd:/1.txt' into table tcm.wm_dis_category fields terminated by';' lines termi ...
- [MySQL]load data local infile向MySQL数据库中导入数据时,无法导入和字段不分离问题。
利用load data将文件中的数据导入数据库表中的时候,遇到了两个问题. 首先是load data命令无法执行的问题: 命令行下输入load data local infile "path ...
- load data ERROR 1197 (HY000)错误
有一份csv格式的文件,大小在14G左右.max_binlog_cache_size=4G. 登录mysql实例,选择对应的表通过load data往指定表里导数.大概20分钟左右,报以下错误: ER ...
随机推荐
- js对数组进行操作
1.数组的创建 var arrayObj = new Array(); //创建一个数组 var arrayObj = new Array([size]); //创建一个数组并指定长度,注意不是上限, ...
- ubuntu下eclipse java ee首次打开提示找不到jdk的问题
最近想搭建一个本地服务器,方便写一些网络请求相关的demo,遂下载了eclipse ee版 ( IDEA证书好贵,暂时不想买-=-),下载---解压 一切正常,但是当在terminal下打开ecli ...
- POJ 2394 Dijkstra
题意: 思路: 裸的Dijkstra 爆敲一发模板 //By SiriusRen #include <queue> #include <cstdio> #include < ...
- 利用日志文件恢复MYSQL数据库
利用日志文件恢复MYSQL数据库 650) this.width=650;" onclick='window.open("http://blog.51cto.com/viewpic ...
- Codefroces Educational Round 26 837 B. Flag of Berland
B. Flag of Berland time limit per test 1 second memory limit per test 256 megabytes input standard i ...
- coverage python 代码覆盖率工具使用(django 使用)
1. 安装包 pip install coverage 2.启动程序 coverage run -m pytest 3.获取html格式的报告文件 coverage html 4.创建配置文件 .co ...
- 小米开源便签Notes-源码研究(0)-整体功能介绍(图文并茂)
本周对小米开源文件管理器,做了整体的研究,大致弄清了源码的来龙去脉,剩下的就是重点研究几个活动的流程了. 讲解Android应用这种可视化的程序,感觉还是有图比较好,不然功能界面都不清楚,自己不好介绍 ...
- Effective C++ 条款43
学习处理模板化基类里的名称 本节作者编写的意图在我看来能够总结成一句话,就是"怎样定义并使用关于模板类的派生过程,怎样处理派生过程出现的编译不通过问题". 以下我们看一段说明性的代 ...
- php7 兼容 MySQL 相关函数
php7 兼容 MySQL 相关函数 PHP7 废除了 ”mysql.dll” ,推荐使用 mysqli 或者 pdo_mysql http://PHP.net/manual/zh/mysqlinfo ...
- js38---门面模式
(function(){ //门面 function addEvebtFacade(el,type,fn){ if(window.addEventListener){ //使用与火狐浏览器 alert ...