I
I
Igor2019-08-20 18:29:12
Vue.js
Igor, 2019-08-20 18:29:12

Lazy loading in Nuxt?

I am using plugin "@nuxtjs/router": "^1.4.0"
It doesn't work like this

import Vue from 'vue'
import Router from 'vue-router'
import paths from './routes/paths'

Vue.use(Router)

function route (path, view, name, meta) {
  return {
    name: name || view,
    path,
    meta,
    component: (resovle) => import(`~/pages/${view}.vue`).then(resovle)
  }
}

export function createRouter() {
  return new Router({
    mode: 'history',
    routes: paths.map(path => route(path.path, path.view, path.name, path.meta)).concat([{ path: "*", redirect: "/" }])
  })
}

This is how it works
import Vue from 'vue'
import Router from 'vue-router'
import Index from '~/pages/index.vue' 

Vue.use(Router)

function route (path, view, name, meta) {
  return {
    name: name || view,
    path,
    meta,
    component: Index
  }
}

export function createRouter() {
  return new Router({
    mode: 'history',
    routes: paths.map(path => route(path.path, path.view, path.name, path.meta)).concat([{ path: "*", redirect: "/" }])
  })
}

If I directly import the view into the router, it works.
The plug is here:
component: (resovle) => import(`~/pages/${view}.vue`).then(resovle)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Igor, 2019-08-20
@IgorPI

Attention!
Correct solution!

import Vue from 'vue'
import Router from 'vue-router'
import paths from './routes/paths'

Vue.use(Router)

function route (path, view, name, meta) {
  return {
    name: name || view,
    path,
    meta,
    component: () => import(`~/pages/${view}.vue`).then(m => m.default || m) // здесь гребаный компонент
  }
}

export function createRouter() {
  return new Router({
    mode: 'history',
    routes: paths.map(path => route(path.path, path.view, path.name, path.meta)).concat([{ path: "*", redirect: "/" }])
  })
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question