Socket
Socket
Sign inDemoInstall

@vue/composition-api

Package Overview
Dependencies
Maintainers
16
Versions
106
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@vue/composition-api

Provide logic composition capabilities for Vue.


Version published
Weekly downloads
107K
decreased by-8.6%
Maintainers
16
Weekly downloads
 
Created

Package description

What is @vue/composition-api?

@vue/composition-api is a plugin that provides Vue 2.x users with the Composition API, which is a set of APIs that allows you to use Vue 3's reactivity system and component logic composition in Vue 2.x applications.

What are @vue/composition-api's main functionalities?

Reactive State

The `reactive` function is used to create a reactive state object. This allows you to manage state in a reactive manner, similar to Vue 3.

```javascript
import { reactive } from '@vue/composition-api';

export default {
  setup() {
    const state = reactive({ count: 0 });
    return { state };
  }
};
```

Computed Properties

The `computed` function is used to create computed properties that automatically update when their dependencies change.

```javascript
import { computed } from '@vue/composition-api';

export default {
  setup() {
    const state = reactive({ count: 0 });
    const doubleCount = computed(() => state.count * 2);
    return { state, doubleCount };
  }
};
```

Watchers

The `watch` function is used to perform side effects in response to reactive state changes.

```javascript
import { watch } from '@vue/composition-api';

export default {
  setup() {
    const state = reactive({ count: 0 });
    watch(() => state.count, (newValue, oldValue) => {
      console.log(`Count changed from ${oldValue} to ${newValue}`);
    });
    return { state };
  }
};
```

Lifecycle Hooks

The `onMounted` and `onUnmounted` functions are used to register lifecycle hooks within the setup function.

```javascript
import { onMounted, onUnmounted } from '@vue/composition-api';

export default {
  setup() {
    onMounted(() => {
      console.log('Component is mounted');
    });
    onUnmounted(() => {
      console.log('Component is unmounted');
    });
  }
};
```

Other packages similar to @vue/composition-api

Changelog

Source

0.6.7 (2020-06-24)

Bug Fixes

  • toRefs: do not warn when toRefs is called in a prop value (#405) (048b6d3)
  • type: improve defineComponent type for option apis (#406) (1c64108)

Features

<a name="0.6.6"></a>

Readme

Source

Vue Composition API

Vue 2 plugin for Composition API in Vue 3.

npm GitHub Workflow Status

English | 中文文档 / Composition API RFC

Note: the primary goal of this package is to allow the community to experiment with the API and provide feedback before it's finalized. The implementation may contain minor inconsistencies with the RFC as the latter gets updated. We do not recommend using this package for production yet at this stage.


Installation

NPM

npm install @vue/composition-api
# or
yarn add @vue/composition-api

You must install @vue/composition-api as a plugin via Vue.use() before you can use the Composition API to compose your component.

import Vue from 'vue'
import VueCompositionAPI from '@vue/composition-api'

Vue.use(VueCompositionAPI)
// use the APIs
import { ref, reactive } from '@vue/composition-api'

:bulb: When you migrate to Vue 3, just replacing @vue/composition-api to vue and your code should just work.

CDN

Include @vue/composition-api after Vue and it will install itself automatically.

<script src="https://cdn.jsdelivr.net/npm/vue@2.6"></script>
<script src="https://cdn.jsdelivr.net/npm/@vue/composition-api@0.6.7"></script>

@vue/composition-api will be exposed to global variable window.vueCompositionApi.

const { ref, reactive } = vueCompositionApi

TypeScript Support

TypeScript version >3.5.1 is required

To let TypeScript properly infer types inside Vue component options, you need to define components with defineComponent

import { defineComponent } from '@vue/composition-api'

export default defineComponent({
  // type inference enabled
})

JSX/TSX

To make JSX/TSX work with @vue/composition-api, check out babel-preset-vca-jsx by @luwanquan.

Limitations

Ref Unwrap

Unwrap is not working with Array index.

Should not store ref as a direct child of Array:

const state = reactive({
  list: [ref(0)],
});
// no unwrap, `.value` is required
state.list[0].value === 0; // true

state.list.push(ref(1));
// no unwrap, `.value` is required
state.list[1].value === 1; // true

Should not use ref in a plain object when working with Array:

const a = {
  count: ref(0),
};
const b = reactive({
  list: [a], // `a.count` will not unwrap!!
});

// no unwrap for `count`, `.value` is required
b.list[0].count.value === 0; // true
const b = reactive({
  list: [
    {
      count: ref(0), // no unwrap!!
    },
  ],
});

// no unwrap for `count`, `.value` is required
b.list[0].count.value === 0; // true

Should always use ref in a reactive when working with Array:

const a = reactive({
  count: ref(0),
});
const b = reactive({
  list: [a],
});
// unwrapped
b.list[0].count === 0; // true

b.list.push(
  reactive({
    count: ref(1),
  })
);
// unwrapped
b.list[1].count === 1; // true

Using reactive will mutate the origin object

This is an limitation of using Vue.observable in Vue 2.

Vue 3 will return an new proxy object.


watch() API

onTrack and onTrigger are not available in WatchOptions.


Template Refs

:white_check_mark: Support     :x: Not Supported

:white_check_mark: String ref && return it from setup():

<template>
  <div ref="root"></div>
</template>

<script>
  export default {
    setup() {
      const root = ref(null);

      onMounted(() => {
        // the DOM element will be assigned to the ref after initial render
        console.log(root.value); // <div/>
      });

      return {
        root,
      };
    },
  };
</script>

:white_check_mark: String ref && return it from setup() && Render Function / JSX:

export default {
  setup() {
    const root = ref(null);

    onMounted(() => {
      // the DOM element will be assigned to the ref after initial render
      console.log(root.value); // <div/>
    });

    return {
      root,
    };
  },
  render() {
    // with JSX
    return () => <div ref="root" />;
  },
};

:x: Function ref:

<template>
  <div :ref="el => root = el"></div>
</template>

<script>
  export default {
    setup() {
      const root = ref(null);

      return {
        root,
      };
    },
  };
</script>

:x: Render Function / JSX in setup():

export default {
  setup() {
    const root = ref(null);

    return () =>
      h('div', {
        ref: root,
      });

    // with JSX
    return () => <div ref={root} />;
  },
};

If you really want to use template refs in this case, you can access vm.$refs via SetupContext.refs.

:warning: Warning: The SetupContext.refs won't exist in Vue 3.0. @vue/composition-api provide it as a workaround here.

export default {
  setup(initProps, setupContext) {
    const refs = setupContext.refs;
    onMounted(() => {
      // the DOM element will be assigned to the ref after initial render
      console.log(refs.root); // <div/>
    });

    return () =>
      h('div', {
        ref: 'root',
      });

    // with JSX
    return () => <div ref="root" />;
  },
};

You may also need to augment the SetupContext when working with TypeScript:

import Vue from 'vue';

declare module '@vue/composition-api' {
  interface SetupContext {
    readonly refs: { [key: string]: Vue | Element | Vue[] | Element[] };
  }
}

:x: Reactive APIs in data()

Passing ref, reactive or other reactive apis to data() would not work.

export default {
  data() {
    return {
      // will result { a: { value: 1 } } in template
      a: ref(1) 
    }
  },
};

SSR

Even if there is no definitive Vue 3 API for SSR yet, this plugin implements the onServerPrefetch lifecycle hook that allows you to use the serverPrefetch hook found in the classic API.

import { onServerPrefetch } from '@vue/composition-api';

export default {
  setup (props, { ssrContext }) {
    const result = ref();

    onServerPrefetch(async () => {
      result.value = await callApi(ssrContext.someId);
    });

    return {
      result,
    };
  },
};

Keywords

FAQs

Package last updated on 24 Jun 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc