English 中文(简体)
Nuxt 3 useRoute import cannot find
原标题:

I am using latest nuxt version and when I tried to use the useRoute method is triggers a "Cannot Find name useRoute" but it works so I would like to know what could I be missing

enter image description here

My package json has these versions

"devDependencies": {
  "@nuxt/devtools": "latest",
  "@types/node": "^18",
  "nuxt": "^3.6.1"
},
"dependencies": {
  "@nuxt/image": "^1.0.0-rc.1",
  "@nuxtjs/tailwindcss": "^6.8.0",
  "@vuepic/vue-datepicker": "^5.3.0",
  "nuxt-icon": "^0.4.1",
  "nuxt-swiper": "^1.1.1"
}

The useRoute autocompletes fine in pages/index.vue but if I try to use it in different vue file or directory inside pages it triggers the same error

问题回答

You are not importing the useRoute method from the Nuxt composables module. The useRoute method is a composable function that allows you to access the current route object in Nuxt 3 and Composition API. You need to import it from the @nuxtjs/composition-api module and use it inside the tag or the setup() function of your component.

<script setup>
    import { useRoute } from "@nuxtjs/composition-api";
    const route = useRoute();
</script>

Or

<script>
    import { useRoute } from "@nuxtjs/composition-api";
    export default {
      setup() {
        const route = useRoute();
        console.log(route.name);
      },
    };
</script>

To learn more, see https://nuxt.com/docs/api/composables/use-route





相关问题
store data in memory with nestjs

I am trying to persist some data in my nestjs server so I can then use that data in my client app through http requests. I have created a products.service.ts file with the function getAllData() that ...

React Hook Form Error on custom Input component

I am having a problem when I use react hook form + zod in my application, in short the inputs never change value and I get the following error in the console: Warning: Function components cannot be ...

Updatable promises using proxy in JavaScript

EDIT: I ve updated this question, and the old question is moved here. A POC can be found on this jsfiddle or the snippet below GOAL: The goal here is to make or rather simulate a promise that can be ...

热门标签