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的更多相关文章

  1. [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. ...

  2. mysql load data 乱码

    解决方案: http://stackoverflow.com/questions/26256421/sql-load-data-infile-utf8-issue 即: load data local ...

  3. Mybatis拦截器 mysql load data local 内存流处理

    Mybatis 拦截器不做解释了,用过的基本都知道,这里用load data local主要是应对大批量数据的处理,提高性能,也支持事务回滚,且不影响其他的DML操作,当然这个操作不要涉及到当前所lo ...

  4. mysql load data 乱码的问题

    新学mysql在用load data导入txt文档时发现导入的内容,select 之后是乱码,先后把表,数据库的字符集类型修改为utf8,但还是一样,最后在 http://bbs.chinaunix. ...

  5. mysql load data infile的使用 和 SELECT into outfile备份数据库数据

    LOAD DATA [LOW_PRIORITY | CONCURRENT] [LOCAL] INFILE 'file_name.txt' [REPLACE | IGNORE] INTO TABLE t ...

  6. 快速的mysql导入导出数据(load data和outfile)

    1.load data: ***实际应用:把日志生成的xls文件load到MySQL中: mysql_cmd = "iconv -c -f utf-8 -t gbk ./data/al_ve ...

  7. 记录load data infile 的用法

    load data local infile 'd:/1.txt' into table tcm.wm_dis_category fields terminated by';' lines termi ...

  8. [MySQL]load data local infile向MySQL数据库中导入数据时,无法导入和字段不分离问题。

    利用load data将文件中的数据导入数据库表中的时候,遇到了两个问题. 首先是load data命令无法执行的问题: 命令行下输入load data local infile "path ...

  9. load data ERROR 1197 (HY000)错误

    有一份csv格式的文件,大小在14G左右.max_binlog_cache_size=4G. 登录mysql实例,选择对应的表通过load data往指定表里导数.大概20分钟左右,报以下错误: ER ...

随机推荐

  1. 4.cocos场景和层的调用

    调用关系: AppDeligate.cpp bool AppDelegate::applicationDidFinishLaunching() { // initialize director aut ...

  2. vue+mui+html5+ plus开发的混合应用底部导航的显示与隐藏

    1. 模板相关内容(template) <template> <div> <transition :name="transitionName"> ...

  3. find---查找文件或目录

    ind命令用来在指定目录下查找文件.任何位于参数之前的字符串都将被视为欲查找的目录名.如果使用该命令时,不设置任何参数,则find命令将在当前目录下查找子目录与文件.并且将查找到的子目录和文件全部进行 ...

  4. init进程

    2.Linux下的三个特殊进程 Linux下有三个特殊的进程idle进程(PID=0),init进程(PID=1),和kthreadd(PID=2)idle进程由系统自动创建,运行在内核态idle进程 ...

  5. 洛谷——P3384 【模板】树链剖分

    https://www.luogu.org/problem/show?pid=3384#sub 题目描述 如题,已知一棵包含N个结点的树(连通且无环),每个节点上包含一个数值,需要支持以下操作: 操作 ...

  6. 徒弟们对话,遇到sb领导,离职吧

     由于涉及私密,所以qq号做了干扰.见谅. 非常多人以为在公司,你优化了几十个sql老板就给你涨工资了.或者你bbed搞点特殊恢复就涨工资了. 或者解决某些棘手问题就涨工资了. 那是不正确的. 遇 ...

  7. 用Fiddle跟踪调试移动表单

    简介:Fiddle,调试,移动表单 http://jingyan.baidu.com/article/925f8cb81e3a52c0dde05616.html

  8. 用硬件卡克隆Linux集群

    650) this.width=650;" onclick="window.open("http://blog.51cto.com/viewpic.php?refimg= ...

  9. CISP/CISA 每日一题 18

    CISSP 每日一题(答)What is the purpose of an access review and audit? Checkto ensure that users do not hav ...

  10. 阻止事件冒泡js jquery

    jQuery之防止冒泡事件 冒泡事件就是点击子节点,会向上触发父节点.祖先节点的点击事件. 以下是html代码部分: <body> <div id="content&quo ...