Exemple #1
0
async function mountApp(__app) {
  // Set global variables
  app = __app.app
  router = __app.router
  

  // Resolve route components
  const Components = await Promise.all(resolveComponents(router))

  // Create Vue instance
  const _app = new Vue(app)

  
    // Load layout
  const layout = NUXT.layout || 'default'
  await _app.loadLayout(layout)
  _app.setLayout(layout)
  

  // Mounts Vue app to DOM element
  const mount = () => {
    _app.$mount('#__nuxt')

    // Listen for first Vue update
    Vue.nextTick(() => {
      // Call window.onNuxtReady callbacks
      nuxtReady(_app)
      
      // Enable hot reloading
      hotReloadAPI(_app)
      
    })
  }

  // Enable transitions
  _app.setTransitions = _app.$options.nuxt.setTransitions.bind(_app)
  if (Components.length) {
    _app.setTransitions(mapTransitions(Components, router.currentRoute))
    _lastPaths = router.currentRoute.matched.map(route => compile(route.path)(router.currentRoute.params))
  }

  // Initialize error handler
  _app.$loading = {} // To avoid error while _app.$nuxt does not exist
  if (NUXT.error) _app.error(NUXT.error)

  // Add router hooks
  router.beforeEach(loadAsyncComponents.bind(_app))
  router.beforeEach(render.bind(_app))
  router.afterEach(normalizeComponents)
  router.afterEach(fixPrepatch.bind(_app))

  // If page already is server rendered
  if (NUXT.serverRendered) {
    mount()
    return
  }

  // First render on client-side
  render.call(_app, router.currentRoute, router.currentRoute, (path) => {
    // If not redirected
    if (!path) {
      normalizeComponents(router.currentRoute, router.currentRoute)
      showNextPage.call(_app, router.currentRoute)
      // Dont call fixPrepatch.call(_app, router.currentRoute, router.currentRoute) since it's first render
      mount()
      return
    }

    // Push the path and then mount app
    router.push(path, () => mount(), (err) => {
      if (!err) return mount()
      console.error(err)
    })
  })
}
Exemple #2
0
async function mountApp(__app) {
  // Set global variables
  app = __app.app
  router = __app.router
  store = __app.store 

  // Resolve route components
  const Components = await Promise.all(resolveComponents(router))

  // Create Vue instance
  const _app = new Vue(app)

  // Load layout
  const layout = NUXT.layout || 'default'
  await _app.loadLayout(layout)
  _app.setLayout(layout)

  // Mounts Vue app to DOM element
  const mountApp = () => {
    _app.$mount('#__nuxt')

    // Listen for first Vue update
    Vue.nextTick(() => {
      // Call window.onNuxtReady callbacks
      nuxtReady(_app)
      
    })
  }

  // Enable transitions
  _app.setTransitions = _app.$options._nuxt.setTransitions.bind(_app)
  if (Components.length) {
    _app.setTransitions(mapTransitions(Components, router.currentRoute))
    _lastPaths = router.currentRoute.matched.map(route => compile(route.path)(router.currentRoute.params))
    _lastComponentsFiles = Components.map(Component => Component.options.__file)
  }

  // Initialize error handler
  _app.error = _app.$options._nuxt.error.bind(_app)
  _app.$loading = {} // To avoid error while _app.$nuxt does not exist
  if (NUXT.error) _app.error(NUXT.error)

  // Add router hooks
  router.beforeEach(loadAsyncComponents.bind(_app))
  router.beforeEach(render.bind(_app))
  router.afterEach(normalizeComponents)
  router.afterEach(fixPrepatch.bind(_app))

  // If page already is server rendered
  if (NUXT.serverRendered) {
    mountApp()
    return
  }

  render.call(_app, router.currentRoute, router.currentRoute, path => {
    if (!path) {
      normalizeComponents(router.currentRoute, router.currentRoute)
      fixPrepatch.call(_app, router.currentRoute, router.currentRoute)
      mountApp()
      return
    }

    // Push the path and then mount app
    let mounted = false
    router.afterEach(() => {
      if (mounted) return
      mounted = true
      mountApp()
    })
    router.push(path)
  })
}