F
F
Fedooot012021-11-15 21:48:54
Vue.js
Fedooot01, 2021-11-15 21:48:54

How can I access a route in an import file using the Composition API in Nuxt.js?

Hello.

I'm making a Nuxt.js application using the Composition API plugin. In the process of making requests to the API, I need a variable from the current URL.

I created a folder at the root with the name api, put a file with the contents in it:

import axios from 'axios'
import { useRoute } from '@nuxtjs/composition-api'

export const getAllItems = () => {
   const route = useRoute()
   const token = route.value.params.id
   return axios.get(`/${token}/allItems`).then((response) => response.data)
}


On the desired page, in the setup function, I call the action in vuex, which makes the call to this getAllItems. But as a result, the call does not occur, but an error is obtained
Uncaught (in promise) Error: This must be called within a setup function.


If you remove the call to useRoute from getAllItems and replace token with a constant, then there is no error and the code works as required.
How to solve this problem?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question