Vue Async Computed Decorator

0.0.5 · abandoned · verified Sun Apr 19

The `vue-async-computed-decorator` package provides a decorator (`@AsyncComputed`) that integrates `vue-async-computed` functionality into Vue 2 components written with `vue-class-component`. This allows class-style Vue 2 components to define properties that resolve asynchronously, typically fetching data, and automatically react to their dependencies, similar to standard computed properties. The package is at a very early pre-release version (0.0.5) and has seen no updates in over six years. It is designed exclusively for Vue 2 environments, relying on Vue 2-specific plugins and the `vue-class-component` library, which is itself deprecated for Vue 3. Consequently, this package is not compatible with Vue 3 or the Composition API, which offers native solutions like `Suspense` and `computedAsync` from libraries like VueUse for handling asynchronous data. Its primary differentiator was providing a concise decorator syntax for async computed properties within the `vue-class-component` paradigm.

Common errors

Warnings

Install

Imports

Quickstart

This example demonstrates how to define an asynchronous computed property in a Vue 2 class component using the `@AsyncComputed` decorator. It shows the necessary imports, plugin installation, and a basic setup with a simulated data fetch and a loading state.

import Vue from 'vue'
import AsyncComputedPlugin from 'vue-async-computed'
import AsyncComputed from 'vue-async-computed-decorator'
import Component from 'vue-class-component'

// Install the base vue-async-computed plugin
Vue.use(AsyncComputedPlugin)

// Define a Vue 2 class component using decorators
@Component
class MyComponent extends Vue {
  // Simulate an asynchronous operation
  async fetchData() {
    return new Promise(resolve => {
      setTimeout(() => resolve('Data fetched!'), 1000)
    })
  }

  // Decorate a method to make it an async computed property
  @AsyncComputed
  async someComputedProp() {
    const data = await this.fetchData()
    return `Async result: ${data}`
  }

  // A regular computed property to show loading state
  get loadingMessage() {
    // The async computed property will be null or its default value while loading
    return this.someComputedProp === null ? 'Loading async data...' : ''
  }
}

// Create a root Vue instance and mount the component
new Vue({
  el: '#app',
  template: `
    <div id="app">
      <h1>Vue Async Computed Decorator Example (Vue 2)</h1>
      <p v-if="loadingMessage">{{ loadingMessage }}</p>
      <p>{{ someComputedProp }}</p>
    </div>
  `,
  components: { MyComponent }
}).$mount('#app')

// To run in a browser, you'd need a basic HTML file:
// <div id="app"></div>
// <script src="https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.js"></script>
// <script src="https://cdn.jsdelivr.net/npm/vue-async-computed@3.9.0/dist/vue-async-computed.js"></script>
// <script src="https://cdn.jsdelivr.net/npm/vue-class-component@7.2.6/dist/vue-class-component.min.js"></script>
// <script src="https://cdn.jsdelivr.net/npm/reflect-metadata@0.1.13/Reflect.js"></script>
// <script type="module">/* compiled code here */</script>

view raw JSON →