vue-function-api
provides a way to use Vue3's Composition api in Vue2.x
.
npm
npm install vue-function-api --save
yarn
yarn add vue-function-api
CDN
<script src="https://unpkg.com/vue-function-api/dist/vue-function-api.umd.js"></script>
By using the global variable window.vueFunctionApi
You must explicitly install vue-function-api
via Vue.use()
:
import Vue from 'vue';
import VueFunctionApi from 'vue-function-api';
Â
Vue.use(VueFunctionApi);
After installing the plugin you can use the Composition API to compose your component.
TypeScriptTo let TypeScript properly infer types inside Vue component options, you need to define components with createComponent
:
Limitationsimport Vue from 'vue';
Â
const Component = createComponent({
});
Â
const Component = {
};
Ref
Unwrap
Unwrap
is not working with Array index.
ref
as a direct child of Array
:
Should not useconst state = reactive({
  list: [ref(0)],
});
state.list[0].value === 0;Â
Â
state.list.push(ref(1);
state.list[1].value === 1;Â
ref
in a plain object when working with Array
:
const a = {
  count: ref(0),
};
const b = reactive({
  list: [a],Â
});
Â
b.list[0].count.value === 0;Â
Should always useconst b = reactive({
  list: [
    {
      count: ref(0),Â
    },
  ],
});
Â
b.list[0].count.value === 0;Â
ref
in a reactive
when working with Array
:
const a = reactive({
  count: ref(0),
});
const b = reactive({
  list: [a],
});
b.list[0].count === 0;Â
Â
b.list.push(
  reactive({
    count: ref(1),
  })
);
b.list[1].count === 1;Â
watch()
API
onTrack
and onTrigger
are not available in WatchOptions
.
â Support     â Not Support
â
String ref && return it from setup()
:
<template>
  <div ref="root"></div>
</template>
Â
<script>
  export default {
    setup() {
      const root = ref(null);
Â
      onMounted(() => {
       Â
        console.log(root.value);Â
      });
Â
      return {
        root,
      };
    },
  };
</script>Â
â
String ref && return it from setup()
&& Render Function / JSX:
export default {
  setup() {
    const root = ref(null);
Â
    onMounted(() => {
     Â
      console.log(root.value);Â
    });
Â
    return {
      root,
    };
  },
  render() {
   Â
    return () => <div ref="root" />;
  },
};
â Function ref:
<template>
  <div :ref="el => root = el"></div>
</template>
Â
<script>
  export default {
    setup() {
      const root = ref(null);
Â
      return {
        root,
      };
    },
  };
</script>Â
â Render Function / JSX in setup()
:
export default {
  setup() {
    const root = ref(null);
Â
    return () =>
      h('div', {
        ref: root,
      });
Â
   Â
    return () => <div ref={root} />;
  },
};
If you really want to use template refs in this case, you can access vm.$refs
via SetupContext.refs
.
â ï¸Warning: The
SetupContext.refs
won't existed inVue3.0
.Vue-function-api
provide it as a workaround here.
export default {
  setup(initProps, setupContext) {
    const refs = setupContext.refs;
    onMounted(() => {
     Â
      console.log(refs.root);Â
    });
Â
    return () =>
      h('div', {
        ref: 'root',
      });
Â
   Â
    return () => <div ref="root" />;
  },
};
You may also need to augment the SetupContext
when wokring with TypeScript:
import Vue from 'vue';
import VueFunctionApi from 'vue-function-api';
Â
Vue.use(VueFunctionApi);
Â
declare module 'vue-function-api/dist/component/component' {
  interface SetupContext {
    readonly refs: { [key: string]: Vue | Element | Vue[] | Element[] };
  }
}
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4