3

I'm working hard to introduce typescript to Nuxt.js with Option API.

In Current Code, we use middleware, asyncData and fetch methods. After I switched lang to ts (<script lang="ts">), typescript says error for the above methods.

like this (about middleware)

 Object literal may only specify known properties, and 'middleware' does not exist in type 'ComponentOptions<Vue, DefaultData<Vue>, DefaultMethods<Vue>, DefaultComputed, PropsDefinition<DefaultProps>, DefaultProps>'.

Then, I googled this problem and found this solution, but it does not work maybe because this solution is about Nuxt with Class API...

summary of my .vue file:

<script lang="ts">
import Vue from 'vue'

export default Vue.extend({
  async middleware({store, params, from}) {
    // Error is occuring here...
    // Object literal may only specify known properties, and 'middleware' does not exist in type 'ComponentOptions<Vue, DefaultData<Vue>, DefaultMethods<Vue>, DefaultComputed, PropsDefinition<DefaultProps>, DefaultProps>'.
  },
  asyncData({params}) {
    ~~~
  },
});
</script>

tsconfig.json:

{
  "compilerOptions": {
    "target": "ES2018",
    "module": "ESNext",
    "moduleResolution": "Node",
    "lib": [
      "ESNext",
      "ESNext.AsyncIterable",
      "DOM"
    ],
    "esModuleInterop": true,
    "allowJs": true,
    "sourceMap": true,
    "strict": true,
    "noEmit": true,
    "baseUrl": ".",
    "paths": {
      "~/*": [
        "./*"
      ],
      "@/*": [
        "./*"
      ]
    },
    "types": [
      "@types/node",
      "@nuxt/types"
    ]
  },
  "exclude": [
    "node_modules"
  ]
}

Is there any way to use those methods in Nuxt with Option API ?

2
  • I found this error is said by 'Vetur', but I'm not sure about this can be helpful for resolving this probrem... Commented Apr 6, 2021 at 9:34
  • After I restarting Vetur, I solved it !!! Commented Apr 6, 2021 at 9:40

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.