Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

@vue/composition-api

vuejs593.3kMIT1.7.2TypeScript support: included

Provide logic composition capabilities for Vue.

vue, composition-api

readme

@vue/composition-api

用于提供 组合式 API 的 Vue 2 插件.

npm GitHub Workflow Status

English | 中文 ・ 组合式 API 文档

⚠️ 随着 Vue 2.7的发布,它内置了Composition API,你不再需要这个插件了。因此,这个插件已经进入维护模式,将只支持Vue 2.6 或更早的版本。本项目将在 2022 年底达到生命终点(EOL)。

安装

NPM

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

在使用 @vue/composition-api 前,必须先通过 Vue.use() 进行安装。之后才可使用新的 组合式 API 进行组件开发。

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

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

:bulb: 当迁移到 Vue 3 时,只需简单的将 @vue/composition-api 替换成 vue 即可。你现有的代码几乎无需进行额外的改动。

CDN

在 Vue 之后引入 @vue/composition-api ,插件将会自动完成安装。

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

@vue/composition-api 将会暴露在全局变量 window.VueCompositionAPI 中。

const { ref, reactive } = VueCompositionAPI

TypeScript 支持

本插件要求使用 TypeScript 4.2 或以上版本

为了让 TypeScript 在 Vue 组件选项中正确地进行类型推导,我们必须使用 defineComponent 来定义组件:

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

export default defineComponent({
  // 类型推断启用
})

JSX/TSX

JSX 现已在 vuejs/jsx 中官方支持。你可以根据这篇文档开启支持。你也可以使用由 @luwanquan 维护的社区版本 babel-preset-vca-jsx

对于 TSX 支持,请在你的项目中创建如下声明文件:

// file: shim-tsx.d.ts
import Vue, { VNode } from 'vue';
import { ComponentRenderProxy } from '@vue/composition-api';

declare global {
  namespace JSX {
    interface Element extends VNode {}
    interface ElementClass extends ComponentRenderProxy {}
    interface ElementAttributesProperty {
      $props: any; // specify the property name to use
    }
    interface IntrinsicElements {
      [elem: string]: any;
    }
  }
}

SSR

尽管 Vue 3 暂时没有给出确定的 SSR 的 API,这个插件实现了 onServerPrefetch 生命周期钩子函数。这个钩子允许你使用传统 API 中的 serverPrefetch 函数。

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

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

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

    return {
      result,
    }
  },
}

浏览器兼容性

@vue/composition-api 支持所有现代浏览器以及IE11+。对于更低版本的IE浏览器你需要安装WeakMap polyfill (例如使用 core-js库)。

限制

:white_check_mark: 支持     :x: 不支持

Ref 自动展开 (unwrap)

<summary> ❌ 不要 在数组中使用含有 ref 的普通对象 </summary>
const a = {
  count: ref(0),
}
const b = reactive({
  list: [a], // `a.count` 不会自动展开!!
})

// `count` 不会自动展开, 须使用 `.value`
b.list[0].count.value === 0 // true
const b = reactive({
  list: [
    {
      count: ref(0), // 不会自动展开!!
    },
  ],
})

// `count` 不会自动展开, 须使用 `.value`
b.list[0].count.value === 0 // true
<summary> ✅ 在数组中,应该 总是将 ref 存放到 reactive 对象中 </summary>
const a = reactive({
  count: ref(0),
})
const b = reactive({
  list: [a],
})
// 自动展开
b.list[0].count === 0 // true

b.list.push(
  reactive({
    count: ref(1),
  })
)
// 自动展开
b.list[1].count === 1 // true

模板 Refs

<summary> ✅ 在`模板`中使用字符串 ref && 从 setup() 返回 ref </summary>
<template>
  <div ref="root"></div>
</template>

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

      onMounted(() => {
        // 在初次渲染后 DOM 元素会被赋值给 ref
        console.log(root.value) // <div/>
      })

      return {
        root,
      }
    },
  }
</script>
<summary> ✅ 在render()中使用字符串 ref && 从 setup() 返回 ref </summary>
export default {
  setup() {
    const root = ref(null)

    onMounted(() => {
      // 在初次渲染后 DOM 元素会被赋值给 ref
      console.log(root.value) // <div/>
    })

    return {
      root,
    }
  },
  render() {
    // 使用 JSX
    return () => <div ref="root" />
  },
}
<summary> ❌ 函数式 ref </summary>
<template>
  <div :ref="el => root = el"></div>
</template>

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

      return {
        root,
      }
    },
  }
</script>
<summary> ❌ 在 setup() 中返回的`渲染函数 / JSX` </summary>
export default {
  setup() {
    const root = ref(null)

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

    // 使用 JSX
    return () => <div ref={root} />
  },
}
<summary> ⚠️ $refs 访问的变通方案 </summary>

:warning: 警告: SetupContext.refs 并非 Vue 3.0 的一部分, @vue/composition-api 将其暴露在 SetupContext 中只是临时提供一种变通方案。

如果你依然选择在 setup() 中写 render 函数,那么你可以使用 SetupContext.refs 来访问模板引用,它等价于 Vue 2.x 中的 this.$refs:

export default {
  setup(initProps, setupContext) {
    const refs = setupContext.refs
    onMounted(() => {
      // 在初次渲染后 DOM 元素会被赋值给 ref
      console.log(refs.root) // <div/>
    })

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

    // 使用 JSX
    return () => <div ref="root" />
  },
}

如果项目使用了 TypeScript,你还需要扩展 SetupContext 类型:

import Vue from 'vue'

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

Reactive

<summary> ⚠️ reactive() 会返回一个修改过的原始的对象 </summary>

此行为与 Vue 2 中的 Vue.observable 一致

:bulb: 在 Vue 3 中,reactive() 会返回一个新的的代理对象

<summary> ⚠️ setdel 添加与刪除响应式属性变通方案 </summary>

⚠️ 警告: setdel 并非 Vue 3 的一部分。由于 Vue 2.x 响应式系统的限制, 我们在这里提供它们作为一种变通方案。 在 Vue 2中,你将需要调用set 去追踪object上新的属性 (与Vue.set类似,但用于由 Composition API 创建的reactive objects)。在 Vue 3 中,你只需要像对待普通对象一样直接为属性赋值即可。

同样地, 在 Vue 2 中你将需要调用del确保响应式对象中属性的删除将触发视图更新 (与Vue.delete类似,但用于由 Composition API 创建的reactive objects)。在Vue3中,你只需要通过调用 delete foo.bar 来删除它们。

import { reactive, set, del } from '@vue/composition-api'

const a = reactive({
  foo: 1
})

// 添加新的响应式属性
set(a, 'bar', 1)

// 刪除属性并触发响应式更新
del(a, 'bar')

Watch

<summary> ❌ 不支持 onTrackonTrigger 选项 </summary>
watch(
  () => {
    /* ... */
  },
  {
    immediate: true,
    onTrack() {}, // 不可用
    onTrigger() {}, // 不可用
  }
)

createApp

<summary> ⚠️ createApp() 是全局的 </summary>

在 Vue3 中,引入了 createApp() 来隔离不同应用实例的上下文(plugin, components 等)。 由于 Vue2 的设计,在这个插件中,我们提供 createApp() 作为一个向前兼容的 API ,它只是全局的一个别名。

const app1 = createApp(RootComponent1)
app1.component('Foo', Foo) // 相当于 Vue.component('Foo', Foo)
app1.use(VueRouter) // 相当于 Vue.use(VueRouter)

const app2 = createApp(RootComponent2)
app2.component('Bar', Bar) // 相当于 Vue.component('Bar', Bar)

createElement / h

<summary> ⚠️ createElement / h 变通方案 </summary>

在 Vue2中 createElement / h 只能通过 render() 函数访问。要在 render()之外使用它, 你可以显式地给它绑定一个组件实例。

:warning: 警告: 此功能是作为 Vue 2 的变通方法提供的,它不是 Vue 3 API 的一部分。

import { h as _h } from '@vue/composition-api'

export default {
  setup() {
    const vm = getCurrentInstance()
    const h = _h.bind(vm)

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

shallowReadonly

<summary> ⚠️ shallowReadonly() 会返回一个新的浅拷贝对象,在此之后新加的字段将不会获得只读或响应式状态。 </summary>

:bulb: 在 Vue 3 中,shallowReadonly() 会返回一个新的的代理对象

readonly

<summary> ⚠️ readonly() 只提供类型层面的只读。 </summary>

readonly() 只在类型层面提供和 Vue 3 的对齐。在其返回值或其属性上使用 isReadonly() 检查的结果将无法保证。

props

<summary> ⚠️ 当使用 toRefs 访问深层属性对象 (如 toRefs(props.foo) 时将会得到不正确的警告。
     ⚠️ isReactive(props.foo) 将会返回 false。 </summary>
defineComponent({
  setup(props) {
    const { bar } = toRefs(props.foo) // it will `warn`

    // use this instead
    const { foo } = toRefs(props)
    const a = foo.value.bar
  }
})

computed().effect

<summary> ⚠️ computed() 拥有一个被设置为 trueeffect 属性,用来代替 ReactiveEffect<T>。 </summary>

由于实现上的不同, 在 @vue/composition-api 中没有 ReactiveEffect 这种概念。 因此, effecttrue 只是为了能够区分 computed 和 refs:

function isComputed<T>(o: ComputedRef<T> | unknown): o is ComputedRef<T>
function isComputed(o: any): o is ComputedRef {
  return !!(isRef(o) && o.effect)
}

缺失的 API

以下在 Vue 3 新引入的 API ,在本插件中暂不适用:

  • onRenderTracked
  • onRenderTriggered
  • isProxy

data() 中使用组合式 API

<summary> ❌ 在 data() 中使用 ref, reactive 或其他组合式 API 将不会生效 </summary>
export default {
  data() {
    return {
      // 在模版中会成为 { a: { value: 1 } }
      a: ref(1),
    }
  },
}

emit 选项

<summary> ❌ emit 仅因在类型定义中对齐 Vue3 的选项而提供,不会有任何效果。 </summary>
defineComponent({
  emit: {
    // 无效
    submit: (eventOption) => {
      if (...) {
        return true
      } else {
        console.warn('Invalid submit event payload!')
        return false
      }
    }
  }
})

性能影响

由于 Vue 2 的公共 API 的限制,@vue/composition-api 不可避免地引入了额外的性能开销。除非在极端情况下,否则这并不会对你造成影响。

你可以查看这个 跑分结果 了解更多信息。

changelog

1.7.2 (2023-08-15)

Bug Fixes

  • types: function constructor should be a Function (#972) (6247ba3)

Features

1.7.1 (2022-09-23)

Bug Fixes

1.7.0 (2022-07-01)

Bug Fixes

1.6.3 (2022-06-24)

Features

  • support Vue3 style app-level provide (#945) (d167b9b)

1.6.2 (2022-05-23)

Features

1.6.1 (2022-05-05)

Bug Fixes

  • inject: allow default value to be undefined (#930) (17d3fc1)

1.6.0 (2022-04-27)

Bug Fixes

1.5.0 (2022-04-25)

Features

  • createElement: allow createElement to bind vm (#920) (564a5a4)
  • Unified as the key of raw in vue3 (#922) (5874eb5)

1.4.10 (2022-03-16)

1.4.9 (2022-02-26)

1.4.8 (2022-02-26)

Bug Fixes

  • types: optional Boolean props as default props (#909) (8f88ae6)
  • use registered Vue instance for warning (b01f1e4)

1.4.7 (2022-02-24)

Bug Fixes

1.4.6 (2022-02-07)

Bug Fixes

  • dynamically update deep scopedSlot refs (#899) (ef312a3)

1.4.5 (2022-01-27)

Bug Fixes

  • reactive: remove useless proxy logic of shallowReactive (#890) (7243ffa)
  • shallowReactive: don't trigger watchers for oldVal === newVal (#894) (2a88e71)

1.4.4 (2022-01-16)

Bug Fixes

1.4.3 (2021-12-27)

Bug Fixes

1.4.2 (2021-12-17)

Bug Fixes

1.4.1 (2021-11-30)

Bug Fixes

Features

1.4.0 (2021-11-14)

1.4.0-beta.0 (2021-11-07)

Features

1.3.3 (2021-11-03)

Features

1.3.2 (2021-11-03)

Bug Fixes

1.3.1 (2021-11-01)

Bug Fixes

  • types: defineComponent object format with no props type (#839) (8a31c78)

1.3.0 (2021-10-28)

Bug Fixes

1.2.4 (2021-10-07)

Bug Fixes

1.2.3 (2021-10-05)

Bug Fixes

  • proxyRefs: When using proxyRefs, the internal variable composition-api.refKey is exposed on the object itself #817 (#818) (92b7eb1)
  • ssr: set() twice lose reactivity (#821) (416845a)
  • correct prop type inference when using PropType<unknown> (#825) (9c9f8e8)

Features

  • computed: allow differentiating refs from computed (#820) (68b5d97)

1.2.2 (2021-09-24)

Reverts

1.2.1 (2021-09-21)

Features

  • types: align ComponentPublicInstance type (2f9cfbf)

1.2.0 (2021-09-21)

Bug Fixes

1.1.5 (2021-09-09)

Bug Fixes

  • improve isReadonly behaviour, close #811, close #812 (d3c456a)
  • api-watch: watching nested ref array w/ deep doesn't work (#808) (b625420)

1.1.4 (2021-08-31)

Bug Fixes

  • types: align emits type with vue-next (565cbd1)

1.1.3 (2021-08-22)

1.1.2 (2021-08-21)

Bug Fixes

Features

1.1.1 (2021-08-14)

Bug Fixes

1.1.0 (2021-08-09)

Features

1.0.6 (2021-08-09)

Features

  • support second target argument for lifecycle functions (3f3b9c6)

1.1.0-beta.7 (2021-08-09)

Bug Fixes

  • effectScope: should have a vaild scope with component (da21873)

1.1.0-beta.6 (2021-08-09)

  • new watchPostEffect api (92fe90c)
  • support second target argument for lifecycle functions (0133c1e)

1.1.0-beta.4 (2021-07-22)

Bug Fixes

  • revert module field to esm.js version, close #769 (92afa6f)

1.1.0-beta.3 (2021-07-18)

Bug Fixes

  • build for mjs and exports all submodules (c116714)

1.1.0-beta.2 (2021-07-16)

Bug Fixes

  • effectScope: should stop along with parent component (784d96c)

1.1.0-beta.1 (2021-07-16)

Features

Features

  • implement effectScope api (#762) (fcadec2)
  • support second target argument for lifecycle functions (3f3b9c6)

1.0.6 (2021-08-09)

Features

1.0.5 (2021-08-01)

Bug Fixes

  • function: properties of function should not disappear. (#778) (68c1a35)

1.0.4 (2021-07-22)

Bug Fixes

  • revert module field to esm.js version, close #769 (4ac545c)

1.0.3 (2021-07-18)

Bug Fixes

  • build for mjs and exports all submodules (69538ee)

1.0.2 (2021-07-16)

Bug Fixes

  • readonly: align behavior with vue-next. (#765) (42104aa)
  • type: remove unnecessary type assertion (#766) (ebb7975)
  • should dynamically update refs in context (#764) (d7de23e)

1.0.1 (2021-07-16)

1.0.0 (2021-07-15)

Bug Fixes

  • mockReactivityDeep: add parameter seen for mockReactivityDeep. (#759) (40cb14a)
  • runtime-core: trigger warning when the injectionKey is undefined (#760) (2ccad9b)

1.0.0-rc.14 (2021-07-12)

Bug Fixes

  • customReactive: avoid circular reference. (#758) (2bd6ea5)
  • watch: traverse refs in deep watch (#753) (55a0a20)
  • only trigger warning in the dev environment (#755) (bc7c2af)
  • watch: errors thrown in the asynchronous callback function in watch will not be caught. (#751) (f0e423f)
  • watch: only trigger warning in the dev environment (#754) (0fe0088)

1.0.0-rc.13 (2021-07-02)

Bug Fixes

  • observe: solve the Ref not unwrapping on the ssr side issue with recursive way. (#723) (debd37d)
  • the hasOwn should be used to determine whether an attribute exists. (#737) (65abcb4)
  • shallowReadonly: align behavior with vue-next (#741) (14d1c7b)
  • types: use AnyObject insteads of any (#742) (efb4195)

1.0.0-rc.12 (2021-06-17)

Bug Fixes

  • proxyRefs: infinite loop when using proxyRefs. (#730) (0b6ab25)
  • reactivity: check type of ob in isRaw and isReactive (#732) (97dd671)
  • watch: watched previous values can't be destructure on first fire. (#727) (b3ab6f9)

1.0.0-rc.11 (2021-06-04)

Bug Fixes

  • reactivity: should trigger watchEffect when using set to change value of array length (#720) (9c03a45)
  • reactivity: unexpected behaviors for array index out of valid array length when set and del (#719) (f08a1d6)
  • shallowReactive: should keep array as array (#717) (620d09b)
  • shallowReadonly: watch should work for ref/reactive with shallowReadonly (#714) (b6fc1f7)

Features

  • reactivity: unwrap value when using set (#722) (bd198e7)

1.0.0-rc.10 (2021-05-27)

Bug Fixes

  • dev: setup data in nextTick is proxied to vm._data. (#697) (e231837)
  • watch: align behavior with vue-next(doWatch). (#710) (fcf8bc3)

1.0.0-rc.9 (2021-05-19)

Bug Fixes

  • The behavior of development and production merge should be consistent. (#694) (7ca7010)
  • shallowReactive: align behavior with vue-next (#696) (3485ecb)

Features

  • add and delete object attributes would trigger update. (#692) (8c27d80)

1.0.0-rc.8 (2021-04-29)

Bug Fixes

  • reactive: align behavior with vue-next (#689) (37fcbaa)
  • Memory leak caused by global variables. (#686) (badff82)

1.0.0-rc.7 (2021-04-18)

Bug Fixes

Features

  • types: export ComponentInternalInstance, close #677, close #675 (ccae670)

1.0.0-rc.6 (2021-03-29)

Bug Fixes

1.0.0-rc.5 (2021-03-11)

1.0.0-rc.4 (2021-03-11)

Bug Fixes

1.0.0-rc.3 (2021-03-03)

Bug Fixes

1.0.0-rc.2 (2021-02-18)

Bug Fixes

Features

1.0.0-rc.1 (2021-01-20)

1.0.0-beta.26 (2021-01-14)

Bug Fixes

1.0.0-beta.25 (2021-01-08)

BREAKING CHANGES

  • useCSSModule renamed to useCssModule to align with Vue 3 (#626)
  • useCSSModule is depreacted.

1.0.0-beta.24 (2021-01-06)

Bug Fixes

1.0.0-beta.23 (2021-01-05)

Bug Fixes

  • useCSSModule to adapt the change of getCurrentInstance, close #620 (#622) (2ddead0)
  • README: The correct option name is emits (#617) (4b2f1ab)

1.0.0-beta.22 (2020-12-19)

Features

  • getCurrentInstance: Aligning with vue3 (#520) (1495a46)

BREAKING CHANGES

  • getCurrentInstance: The internal vm can be accessed with getCurrentInstance().proxy
const vm = getCurrentInstance()

// becomes

const vm = getCurrentInstance().proxy

1.0.0-beta.21 (2020-12-07)

Bug Fixes

  • destructure attrs from context keep reactive, close #264 (#594) (4eecd66)

Features

1.0.0-beta.20 (2020-11-24)

Bug Fixes

Features

1.0.0-beta.19 (2020-11-02)

Bug Fixes

1.0.0-beta.18 (2020-10-21)

Bug Fixes

  • type: vue constructor should not require props with default values (#567) (964f9f3)
  • better vueDependency importing, close #564 (#572) (555f20a)

Features

  • reactivity: add Vue.delete workaround (#571) (b41da83)

1.0.0-beta.17 (2020-10-17)

Bug Fixes

Code Refactoring

BREAKING CHANGES

1.0.0-beta.16 (2020-10-10)

Bug Fixes

1.0.0-beta.15 (2020-10-04)

Bug Fixes

  • reactive: fix issue when using reactive array in the template (#532) (d99b91d)
  • reactive in SSR (#546) (535c829)
  • incorrect warning for getRegisteredVueOrDefault, resolve #544 (3a1d992)
  • reactive for props (#547) (4d39443)
  • vue-test: prevent warning when using multiple localVue (#531) (5484bb7)

1.0.0-beta.14 (2020-09-15)

Bug Fixes

  • circular objects and making all Vue.observable objects isReactive (#512) (f204daa)

Features

  • reactive: allow usage of reactive before Vue.use (#515) (89fd11c)

1.0.0-beta.13 (2020-09-12)

Bug Fixes

  • sets: check for window to avoid SSR errors (#511) (9ea7230)

1.0.0-beta.12 (2020-09-12)

Features

1.0.0-beta.11 (2020-08-22)

Bug Fixes

  • setup: handle updates for directly return a reactive object (#488) (a7f2c25), closes #487
  • watch: check if ob has value before addSub (#477) (d8cd30d)

1.0.0-beta.10 (2020-08-15)

Bug Fixes

  • watch: check if ob has value before addSub (#477) (d8cd30d)

1.0.0-beta.9 (2020-08-11)

Bug Fixes

  • watch: watch will trigger when added new keys using set (#468) (13bfed1)

1.0.0-beta.8 (2020-08-07)

Bug Fixes

1.0.0-beta.7 (2020-08-07)

BREAKING CHANGES

Features

  • proxyRefs method and ShallowUnwrapRefs type (#456) (149821a)

Performance Improvements

1.0.0-beta.6 (2020-07-22)

Features

  • shallowReadonly: add shallowReadonly and set computed to be shallowReadonly (#447) (cfbbcec)

1.0.0-beta.5 (2020-07-20)

1.0.0-beta.4 (2020-07-18)

Bug Fixes

Features

1.0.0-beta.3 (2020-07-09)

Bug Fixes

1.0.0-beta.2 (2020-07-05)

Bug Fixes

Features

1.0.0-beta.1 (2020-06-30)

BREAKING CHANGES

  • change umd exported name to VueCompositionAPI (#399)
  • rename createElement to h (#400)
  • drop createComponent (#389)
  • match dist file naming with vue-next (#413)

Bug Fixes

Features

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

0.6.6 (2020-06-21)

Reverts

0.6.5 (2020-06-19)

Bug Fixes

  • watchEffect: prevent recursive calls when using flush:sync (#389) (f7f1e77)
  • not unwrapping markRaw objects (#386) (575d100)
  • unwrap refs returned by data (#387) (1f07075)

0.6.4 (2020-06-16)

Bug Fixes

  • setup: call stack exceeded when returning circular dependency (#380) (66f58ba)
  • setup: Vue.extend(Comp).extend({}) - vue-test-utils (#383) (ce932bf)

0.6.3 (2020-06-12)

Bug Fixes

0.6.2 (2020-06-11)

Bug Fixes

  • reactivity: unwrap nested refs on the template (#361) (1fd48f5)
  • defineComponent() with array props (#364) (d7048d4)
  • setup: Allow retuning frozen objects on the setup (#366) (bca3a69)
  • mark props as reactive (#359) (bc78428)

0.6.1

Fix

  • __DEV__ is not defined, #355, @yoyo930021

0.6.0

Great thanks to @pikax for #311, making most of the APIs better aligned with the latest vue-next.

BREAKING CHANGE

  • The lazy option of watch has been replaced by the opposite immediate option, which defaults to false. (It's ignored when using the effect signature). more details (#266)
  • Rename nonReactive to markRaw
  • watchEffect now follows the same behaviour as v3 (triggers immediately).
  • UnwrapRef types from vue-next this can cause some incompatibilities.

Bug Fixes

  • Added missing reactivity API from vue-next, #311, @pikax
  • Fix return type of toRefs, #315
  • Fix incorrect ref typing, #344, @antfu
  • Binding context vm when using function without parentheses, #148, @pikax
  • computed: destroy helper vm of computed to prevent memleak, #277, @LinusBorg
  • Remove the surplus Function type from PropType, #352, @pikax

Features

  • Added unref(#309), isReactive (#327), toRef (#313), UnwrapRef (#247)
  • Added shallowReactive, shallowRef
  • Added toRaw
  • getCurrentInstance available on the lifecycle hooks (onMounted, etc)
  • getCurrentInstance returns undefined when called outside setup instead of throwing exception

Types

  • Align reactivity types with vue-next

0.5.0

  • New: watchEffect function, lingin up with the latest version of the RFC (RFC docs) (#275)
  • Fix: setup from a mixin should called before the component's own (#276)
  • Fix(types): Fix corner case in UnWrapRef internal type (#261)
  • types: Add Element to bailout types for unwrapping (#278)

0.4.0

  • Refactor: rename createComponent to defineComponent (the createComponent function is still there but deprecated) #230
  • Fix: correct the symbol check; fixes the compatibility issue in iOS 9 #218
  • Fix: avoid accessing undeclared instance fields on type-level; fixes Vetur template type checking; fixes vue-router type compatibility #189
  • Fix: onUnmounted should not be run on deactivated #217

0.3.4

  • Fixed reactive setter not working on the server.
  • New isServer setup context property.

0.3.3

  • Fixed make __ob__ unenumerable #149.
  • Fixed computed type
  • Expose getCurrentInstance for advanced usage in Vue plugins.
  • New onServerPrefetch lifecycle hook and new ssrContext setup context property #198.

0.3.2

  • Improve TypeScript type infer for props option #106.
  • Fix return type of createComponent not being compatible with vue-router #130.
  • Expose listeners on SetupContext #132.

0.3.1

  • Fix cleaup callback not running when watcher stops #113.
  • Fix watcher callback not flushing at right timing #120.

0.3.0

  • Improve TypeScript type definitions.
  • Fix context.slots not being available before render #84.

Changed

The render function returned from setup no longer receives any parameters.

Previous

export default {
  setup() {
    return props => h('div', prop.msg);
  },
};

Now

export default {
  setup(props) {
    return () => h('div', prop.msg);
  },
};

0.2.1

  • Declare your expected prop types directly in TypeScript:

    import { createComponent, createElement as h } from '@vue/composition-api';
    
    interface Props {
      msg: string;
    }
    
    const MyComponent =
      createComponent <
      Props >
      {
        props: {
          msg: {}, // required by vue 2 runtime
        },
        setup(props) {
          return () => h('div', props.msg);
        },
      };
  • Declare ref type in TypeScript:

    const dateRef = ref < Date > new Date();
  • Fix createComponent not working with import() #81.
  • Fix inject type declaration #83.

0.2.0

Fixed

  • computed property is called immediately in reactive() #79.

Changed

0.1.0

The package has been renamed to @vue/composition-api to be consistent with RFC.

The @vue/composition-api reflects the Composition API RFC.

2.2.0

  • Improve typescript support.
  • Export createElement.
  • Export SetupContext.
  • Support returning a render function from setup.
  • Allow string keys in provide/inject.

2.1.2

  • Remove auto-unwrapping for Array (#53).

2.1.1

  • Export set() function. Using exported set whenever you need to use Vue.set or vm.$set. The custom set ensures that auto-unwrapping works for the new property.
  • Add a new signature of provide: provide(key, value).
  • Fix multiple provide invoking per component.
  • Fix order of setup invoking.
  • onErrorCaptured not triggered (#25).
  • Fix this losing in nested setup call (#38).
  • Fix some edge cases of unwarpping.
  • Change context.slots's value. It now proxies to $scopeSlots instead of $slots.

2.0.6

Fixed

  • watch callback is called repeatedly with multi-sources

Improved

  • reduce watch() memory overhead

2.0.0

Implement the newest version of RFC

Breaking Changes

this is not available inside setup(). See setup for details.

Features

Complex Prop Types:

import { createComponent, PropType } from 'vue';

createComponent({
  props: {
    options: (null as any) as PropType<{ msg: string }>,
  },
  setup(props) {
    props.options; // { msg: string } | undefined
  },
});

1.x

Implement the init version of RFC