Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

composition api doesn't work with Nuxt-TS

Created nuxt app using npx create-nuxt-app. Followed documentation at https://typescript.nuxtjs.org, however i have an issue using @vue/composition-api:

example component.vue:

<template>
  <div>
    {{ msg }}
  </div>
</template>

<script lang="ts">
import { createComponent, ref } from '@vue/composition-api'

export default createComponent({
  setup() {
    const msg = ref('hello')

    return {
      msg
    }
  }
})
</script>

Doesn't work, throws an error "Property or method "msg" is not defined on the instance but referenced during render." because it doesn't see my ref. I've added composition API as plugin in "plugins/composition-api.ts":

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

Vue.use(VueCompositionApi)

Then in nuxt.config.ts:

plugins: ['@/plugins/composition-api']

like image 622
Alexander Kim Avatar asked Jan 18 '26 11:01

Alexander Kim


1 Answers

My bad, forgot about TS-runtime in nuxt:

npm i -S @nuxt/typescript-runtime

Then you need to update package.json: https://typescript.nuxtjs.org/guide/runtime.html#installation

like image 128
Alexander Kim Avatar answered Jan 20 '26 01:01

Alexander Kim



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!