Composition API
❗️ Experimental feature
Installation
yarn add @vue/composition-api
1
Usage
Example 1
<template>
<button ref="btn">Composition API</button>
</template>
<script>
import { ref } from "@vue/composition-api";
import { useTippy } from "vue-tippy/composition";
export default {
setup() {
const btn = ref(null);
useTippy(btn, {
content: "Cool!",
});
return {
btn,
};
},
};
</script>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
Example 2
<template>
<div>
<button @click="changePlace">Change option placement</button>
<button ref="btn">Composition API</button>
</div>
</template>
<script>
import { ref, reactive } from "@vue/composition-api";
import { useTippy } from "vue-tippy/composition";
export default {
setup() {
const btn = ref(null);
const options = reactive({
content: "Reactive options!",
placement: "left",
});
useTippy(btn, options);
const places = ["left", "top", "right", "bottom"];
const changePlace = () => {
options.placement =
places[parseInt(Math.random() * 100) % places.length];
};
return {
btn,
options,
changePlace,
};
},
};
</script>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
Example 3
WHAAAT!
<template>
<div>
<button ref="btn">Composition API</button>
<div ref="content">
WHAAAT!
</div>
</div>
</template>
<script>
import { ref } from "@vue/composition-api";
import { useTippy } from "vue-tippy/composition";
export default {
setup() {
const btn = ref(null);
const content = ref(null);
useTippy(btn, {
content,
});
return {
btn,
content,
};
},
};
</script>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
Example 4
<template>
<div class="mt-4 mb-4">
<button ref="btn" class="btn">Composition API</button>
<button ref="btn2" class="btn">Composition API 2</button>
<button class="btn" @click="inc">{{ ccc }}</button>
</div>
</template>
<script>
import { ref, watch, defineComponent } from "@vue/composition-api";
import { useTippy } from "vue-tippy/composition";
export default defineComponent({
setup() {
const ccc = ref(1);
const btn = ref(null);
const btn2 = ref(null);
useTippy([btn, btn2], {
content: ccc,
});
function inc() {
ccc.value++;
}
return {
btn,
btn2,
ccc,
inc,
};
},
});
</script>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
Example 5
<template>
<base-button ref="btn">Composition API with Vue Component</base-button>
</template>
<script>
import { ref } from "@vue/composition-api";
import { useTippy } from "vue-tippy/composition";
import BaseButton from './BaseButton.vue';
export default {
components: { BaseButton },
setup() {
const btn = ref(null);
useTippy(btn, {
content: "Cool!"
});
return {
btn
};
}
};
</script>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
Example 6
<template>
<div>
<button v-if="shown" @click="hide">Hide and Unmount Tippy</button>
<button v-else @click="show">Show and Mount Tippy</button>
<div v-if="shown">
<button ref="btn">Composition API</button>
</div>
</div>
</template>
<script>
import { defineComponent, ref, nextTick } from "@vue/composition-api";
import { useTippy } from "vue-tippy/composition";
export default defineComponent({
setup() {
const shown = ref(false);
const btn = ref();
const { mount, unmount } = useTippy(btn, {
content: "Cool!"
}, { mount: false });
const show = async () => {
shown.value = true
await nextTick() // wait for ref to be populated
mount()
}
const hide = () => {
unmount()
shown.value = false
}
return {
shown,
show,
hide,
btn
};
}
});
</script>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47