IgorPI
@IgorPI

Ленивая загрузка в Nuxt?

Использую плагин "@nuxtjs/router": "^1.4.0"

Вот так не работает
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: "/" }])
  })
}


Вот так работает
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: "/" }])
  })
}

Если я напрямую импортирую представление в роутер, то работает.

Затык вот здесь:
component: (resovle) => import(`~/pages/${view}.vue`).then(resovle)
  • Вопрос задан
  • 488 просмотров
Решения вопроса 1
IgorPI
@IgorPI Автор вопроса
Внимание!

Правильное решение!

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: "/" }])
  })
}
Ответ написан
Комментировать
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы