[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‘
        })
      }
    },
时间: 2024-12-29 09:48:39

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

mysql导入数据load data infile用法整理

有时候我们需要将大量数据批量写入数据库,直接使用程序语言和Sql写入往往很耗时间,其中有一种方案就是使用MySql Load data infile导入文件的形式导入数据,这样可大大缩短数据导入时间. 假如是从MySql客户端调用,将客户端的文件导入,则需要使用 load local data infile. LOAD DATA INFILE 语句以很高的速度从一个文本文件中读取行到一个表中.文件名必须是一个文字字符串. 1,开启load local data infile. 假如是Linux下

mysql文件导入到数据库load data infile into table 的使用例子

load data infile "C:/Users/Administrator/Desktop/1.txt"into table 要一个已经存的表名 字段默认用制表符隔开 文件 我爱你 20 相貌平常李奎 21 相貌1平常王二米 210 相貌3平常老三 24 很强老四 34 XXXXX 常用如下: Load Data InFile 'C:/Data.txt' Into Table `TableTest` Lines Terminated By '\r\n';这个语句,字段默认用制表符

关于MySQL中使用LOAD DATA INFILE导入csv文件时的日期格式问题

在使用MySQL时,常常会用到Load Data Infile来导入数据,在遇到Date类型的列时,有时会遇到格式转换的问题: 首先创建一张简单的people表,包含名字,生日,年龄三个字段: mysql> create table people( -> name varchar(10) NOT NULL, -> birthday date NOT NULL, -> age int NOT NULL); Query OK, 0 rows affected (0.18 sec) 构造

Mysql load data 命令解析、处理 error 29 (ErrCode: 13) 错误(在ubuntu环境下)

在 mysql 服务器上,可以通过 load data infile 'file_name' into table table_name; 命令将一个文本文件中的所有数据存到指定表中.最粗略形式的例子: load data infile 'test.txt' into table test_table; 默认情况下,load data infile 对于文本中行为是: 一行对应数据库表中的一条记录 各个字段之间以tab键分开 每个字段的值没有被任何字符括起来 行没有前缀可以忽略 比如某一行文本:

mysql 的load data infile的用法

LOAD DATA INFILE语句从一个文本文件中以很高的速度读入一个表中. 1.基本语法 LOAD DATA [LOW_PRIORITY | CONCURRENT] [LOCAL] INFILE 'file_name.txt' [REPLACE | IGNORE] INTO TABLE tbl_name [FIELDS [TERMINATED BY 'string'] [[OPTIONALLY] ENCLOSED BY 'char'] [ESCAPED BY 'char' ] ] [LINE

Dynamic load data by scrollview

The  demo generate from 北京尚学堂 package com.example.scrollview; import android.support.v7.app.ActionBarActivity; import android.support.v7.app.ActionBar; import android.support.v4.app.Fragment; import android.os.Bundle; import android.view.LayoutInflat

load data infile

LOAD DATA [LOW_PRIORITY | CONCURRENT] [LOCAL] INFILE 'file_name' [REPLACE | IGNORE] INTO TABLE tbl_name [PARTITION (partition_name,...)] [CHARACTER SET charset_name] [{FIELDS | COLUMNS} [TERMINATED BY 'string'] [[OPTIONALLY] ENCLOSED BY 'char'] [ESCA

My SQL load data infile 遇到的问题总结

假如: create table test_table( id int, name varchar (128); 假如命令如下: load data infile 'a.txt' into table `test_table` fields enclosed by '"' terminated by ',' 假如列值如下: "1","abc"123"abc" 这样的值还是能正确处理的.导入之后,列值为:1 abc"123&qu