2.0.7 ā€¢ Published 3 years ago

vue-awaited v2.0.7

Weekly downloads
107
License
MIT
Repository
-
Last release
3 years ago

VueAwaited

Build Status Coverage Status

Small Vue.js component for convenient data fetching and lazy loading.

Inspired by vue-promised, recommend to look at it if you need loading/errors handling without lazy loading.

Features

  • āš”ļø Works for both Vue 3 and 2
  • šŸ“ Fetch, show and manage data directly in the template
  • šŸ‘ļø Loads data when the element becomes visible
  • šŸ“ Slots for loading and error states
  • No dependencies.

Why does this library exist?

Usually, on big pages, rendering all content at once can cause performance problems with big "Time to Interactive" or "Largest Contentful Paint" time, or even bigger problem if SSR used - server should render all the content before user could see the page.

And in most cases, the user will not even scroll to that content, but must wait to it to be rendered.

VueAwaited solves these problems by loading data only when it becomes visible and handle loading/error state itself using slots šŸ™‚.

Installation

yarn add vue-awaited

or

npm i vue-awaited

Usage

Global

// Vue 2

import Vue from 'vue'
import VueAwaited from 'vue-awaited'

Vue.use(VueAwaited, { /* options */ })


// Vue 3

import { createApp } from 'vue'
import VueAwaited from 'vue-awaited'

const app = createApp(App)
app.use(VueAwaited, { /* options */ }))

Local

<script>
import { awaited } from 'vue-awaited'

export default {
  components: {
    awaited
  }
}
</script>

Basic Example

Using url string:

<template>
  <awaited
    action="https://rickandmortyapi.com/api/character/1"
    #default="{ data: { image, name, species } }"
  >
    <img :src="image" :alt="name" />
    <h2>{{ name }}</h2>
    <span>{{ species }}</span>
  </awaited>
</template>

Using component's method:

<template>
  <awaited :action="getCharacter" #default="{ data: { image, name, species } }">
    <img :src="image" :alt="name" />
    <h2>{{ name }}</h2>
    <span>{{ species }}</span>
  </awaited>
</template>

<script>
export default {
  methods: {
    getCharacter() {
      return fetch('https://rickandmortyapi.com/api/character/1')
    }
  }
}
</script>

Using slots:

<template>
  <awaited action="https://rickandmortyapi.com/api/character/1">
    <template #pending>Loading...</template>
    <template #error="{ error }">{{ error.message }}</template>
    <template #default="{ data: { image, name, species } }">
      <img :src="image" :alt="name" />
      <h2>{{ name }}</h2>
      <span>{{ species }}</span>
    </template>
  </awaited>
</template>

No need to call .json() method on response object, it will be done automatically under the hood.

API Reference

options

NameDescriptionType
nameComponent name. Defaults to awaitedString
propsProps which will be passed to componentObject

props

All of these props could be passed to global config as well as directly to component.

NameDescriptionType
actionUrl string, method or promise objectString Function Promise
lazyEnables lazy loading which uses Intersection Observer API under the hoodBoolean
delayDelay in ms to wait before displaying the pending slot. Defaults to 200Number
marginrootMargin option for IntersectionObserver class. Defaults to 0px. See docsString
thresholdthreshold option for IntersectionObserver class. Defaults to 1.0. See docsString
heightHeight of an element that is shown before pending slot. Defaults to 0pxString
watchReactive value or watch function to watch changes and rerun actionNumber String Array Object Function
fetchOptionsOptions for fetch functionNumber
actionHandlerCustom action handler. F.e to use axios libraryNumber

slots

All slots can be used as scoped or regular slots.

NameDescriptionScope
pendingContent to display while the action is pending and before delay is overpreviously resolved data
defaultContent to display once the action has been successfully resolvedresolved data
errorContent to display if the action is rejectedthrowed error

Author

šŸ‘¤ Enkot

šŸ¤ Contributing

Contributions, issues and feature requests are welcome!Feel free to check issues page.

License

MIT

2.0.7

3 years ago

2.0.6

3 years ago

2.0.3

3 years ago

2.0.2

3 years ago

2.0.5

3 years ago

2.0.1

3 years ago

2.0.0

3 years ago

1.2.5

4 years ago

1.2.4

4 years ago

1.2.3

4 years ago

1.2.2

5 years ago

1.2.1

5 years ago

1.2.0

5 years ago

1.1.10

5 years ago

1.1.9

5 years ago

1.1.8

5 years ago

1.1.7

5 years ago

1.1.6

5 years ago

1.1.5

5 years ago

1.1.4

5 years ago

1.1.3

5 years ago

1.1.2

5 years ago

1.1.1

5 years ago

1.1.0

5 years ago

1.0.11

5 years ago

1.0.10

5 years ago

1.0.9

5 years ago

1.0.8

5 years ago

1.0.7

5 years ago

1.0.6

5 years ago

1.0.5

5 years ago

1.0.4

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago