W
W
WebDev2018-12-21 12:23:30
Vue.js
WebDev, 2018-12-21 12:23:30

How to call asyncData on an imported file?

Pages directory structure :

contacts.vue
index.vue
landing.vue

In index.vue, under certain circumstances, you need to open contacts, and in other cases, landing.
//index.vue

<contacts v-if="show === 'contacts'"></contacts>
<landing v-else></landing>

import contacts from './contacts';
import landing from './landing';
...
asyncData() {
    let show = 'landing';
    if (someCondition) {
         show = 'contacts';
    } 

    return {show};
}

The example is exaggerated, but the essence is indicated correctly.
In general, the asyncData method in landing.vue and in contacts.vue does not work.
What to do? Duplicate code?
UPD:
The documentation says that asyncData doesn't work in components, it only works in pages, which is why I'm trying to import the page. But apparently it doesn't work when importing other files at all.
In a normal vue-cli, I would just substitute the desired file in routes.
For example:
import contacts from '~/components/contacts';
import landing from '~/components/landing';

routes: [
        {path: '/',  name: 'Home',  component: (someCondition ? contacts : landing)},
]

But due to the fact that nuxt has routing on files, I can’t do this.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
nvdfxx, 2018-12-21
@nvdfxx

Why do you need asyncData method in components? It is also needed to display these very components. Some kind of logical error, no?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question