Join GitHub today
GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together.
Sign upComposition functions support and `this` value changes #416
Comments
|
I really like it. Well thoughts. One question is would it be possible to pass another class property to the composition function? This might not be the best example, but something like this. See function useCounter (amount: number) {
const count = ref(0)
function increment () {
count.value + amount
}
return {
count,
increment
}
}
export default class Counter extends Vue {
amount = 10
get counter () {
reactive(useCounter(this.amount))
}
} |
|
@kiaking You mean like the following code? (passing primitive loses its reactivity and computed does not handle composition functions in this proposal) function useCounter (amount: Ref<number>) {
const count = ref(0)
function increment () {
count.value + amount.value
}
return {
count,
increment
}
}
export default class Counter extends Vue {
amount = 10
counter = reactive(useCounter(
toRef(this, 'amount')
))
}I think we should discourage this usage because I would say the dependency direction between composition function and component instance should be composition <- instance. This is as same as the canonical Vue api ( function useCounter () {
const amount = ref(10)
const count = ref(0)
function increment () {
count.value + amount.value
}
return {
count,
amount,
increment
}
}
export default class Counter extends Vue {
counter = reactive(useCounter())
mounted() {
this.counter.amount = 11
}
} |
|
Ah OK good point. Yes I was afraid of that. I don't have good example here but I suspected there might be shared 3rd party composition function that might require argument to be passed. But as you mentioned, we could always "wrap" such composition function to be used before assigning to the class component. // 3rd party composition function. It requires argument.
function useCounter (amount: Ref<number>) {
const count = ref(0)
function increment () {
count.value + amount.value
}
return { count, increment }
}
// Wrap the above composition function.
function useWrappedCounter () {
const amount = ref(10)
return {
amount,
...useCounter(amount)
}
}
// Use wrapped function.
export default class Counter extends Vue {
counter = reactive(useWrappedCounter())
mounted() {
this.counter.amount = 11
}
} |
|
An idea to allow such usecase would be to require wrapping a composition function and delay its initalization: export default class Counter extends Vue {
amount = 10
counter = setup(() => {
return useCounter(toRef(this, 'amount'))
})
}The current problems of
To solve 1. we can just configure proxy getter / setter to actual reactive data for each initialized property. As for 2. we need to delay the invocation of composition function to make sure accessed properties are converted reactive value. We can solve 2. with I noticed that mandating |
|
I have updated the proposal regarding the above |
|
Nice! I really dig the new |
|
Since Vue core changed the unwrapping behavior of |
|
Since setup() for variables used on the state add a bunch a unwanted boilerplate I am asking similarly as for props, would there be ways to define (state) variables used on the template inside the class ? |
|
@LifeIsStrange I'm afraid I don't understand what you mean. Could you elaborate "ways to define (state) variables used on the template inside the class"? I'm not sure how we can integrate class component with the new |
|
Please file a new story for a bug report. Thanks. |
<template>
<div>Count: {{ counter.count }}</div>
<button @click="counter.increment()">+</button>
</template>
<script lang="ts">
import { ref, reactive, onMounted } from 'vue'
import { Vue, setup } from 'vue-class-component'
function useCounter () {
const count = ref(0)
function increment () {
count.value++
}
onMounted(() => {
console.log('onMounted')
})
return {
count,
increment
}
}
export default class Counter extends Vue {
counter = setup(() => useCounter())
}
</script>If I understand correctly, this (your) example show how we can make hybrid class components using composition functions, which is nice even if I don't get much the value of it because part of the logic is outside the class. <template>
<div>Count: {{ count }}</div>
<button @click="increment()">+</button>
</template>
<script lang="ts">
import { ref, reactive, onMounted } from 'vue'
import { Vue, setup } from 'vue-class-component'
export default class Counter extends Vue {
count = 0
increment () {
this.count++
}
}
</script>If so do we have guarantee that this existing way will remain supported for the foreesable future and not be deprecated by the composing function api ? |
|
@LifeIsStrange You can still do the existing way. The purpose of adding the |

Formed in 2009, the Archive Team (not to be confused with the archive.org Archive-It Team) is a rogue archivist collective dedicated to saving copies of rapidly dying or deleted websites for the sake of history and digital heritage. The group is 100% composed of volunteers and interested parties, and has expanded into a large amount of related projects for saving online and digital history.

Summary
setuphelper.setupfunction under the hood.$props(and its derived prop values),$attrs,$slotsand$emitare available onthisin class property initializers.Example:
Details
Prior to v7, class component constructor is initialized in
datahook to collect class properties. In v8, it will be initialized insetuphook so that the users can use composition function in class property initializer.The above class component definition is as same as following canonical component definition.
setuphelperWrapping a composition function with
setuphelper is needed because we need to delay the invocation of the composition function. Let's see the following example:In the above example,
this.postIdwill be referred bywatchhelper to track reactive dependencies immediately but it is not reactive value at that moment. Then the watch callback won't be called whenpostIdis changed.setuphelper will delay the invocation untilthis.postIdbecome a proxy property to the actual reactive value.setupunwrappingAs same as
setupin the Vue core library,setupin Vue Class Component unwrapsrefvalues. The unwrapping happens shallowly:In addition, if you return a single ref in
setuphelper, the ref will also be unwrapped:Available built in properties on
thisSince the class constructor is used in
setuphook, only following properties are available onthis.$propsthisas well. (e.g.this.$props.foo->this.foo)$emit$attrs$slotsExample using
$propsand$emitin a composition function.Alternative Approach
Another possible approach is using super class and mixins.
Pros
this.Cons
Need duplicated props type definition.