It says "anything returned here will be available for the rest of the component"(https://v3.vuejs.org/guide/composition-api-introduction.html#setup-component-option), but I got this error on c which is a variable returned in setup: Cannot find name 'c'.ts(2304).
<script lang="ts">
import { defineComponent } from 'vue'
export default defineComponent({
setup(props) {
let c = 123;
return { c }
},
methods: {
increment() {
c;
}
}
})
</script>
Any help?
You need to access the variables exposed via setup
method using this
keyword in your methods.
But this is just a bad way to proceed because you are mixing composition api
with options api
. Refer to this thread to understand the difference between the two.
Vue.createApp({
setup(props) {
let c = 123;
return { c }
},
methods: {
increment() {
console.log(this.c);
}
}
}).mount('#app')
<script src="https://unpkg.com/vue@next"></script>
<div id="app">
<button @click="increment">Increment</button>
</div>
Vue.createApp({
setup(props) {
const c = Vue.ref(123);
const increment = () => c.value++;
return {
c,
increment
}
},
}).mount('#app')
<script src="https://unpkg.com/vue@next"></script>
<div id="app">
<span>{{ c }}</span><br/><br/>
<button @click="increment">Increment</button>
</div>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With