Skip to content

Set instance options before the router resolves #106

Answered by mvrlin
mvrlin asked this question in Q&A
Discussion options

You must be logged in to vote

@frandiox, Thank you for your help. I thought for a while and came up with this solution, maybe someone will find it useful:

Initialization

// modules/asyncData.ts

import type { RouteComponent, RouteLocationNormalized } from 'vue-router'
import type { SSRContext } from '..'

type RouteComponentWithAsync = RouteComponent & {
  asyncData?: <T>(context: SSRContext) => Promise<T> | T
}

type RouteLocationWithHref = RouteLocationNormalized & {
  href?: string
}

export function installAsyncData(context: SSRContext) {
  context.router.beforeResolve(async (to: RouteLocationWithHref, from: RouteLocationWithHref, next) => {
    const initialState = context.initialState || {}

    if (from.href) {

Replies: 2 comments 3 replies

Comment options

You must be logged in to vote
2 replies
@mvrlin
Comment options

@frandiox
Comment options

Comment options

You must be logged in to vote
1 reply
@frandiox
Comment options

Answer selected by mvrlin
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants