posva / unplugin-vue-router

Next Generation file based typed routing for Vue Router
https://uvr.esm.is
MIT License
1.7k stars 82 forks source link

Support definePage() with import other js #487

Closed wsi18n closed 3 months ago

wsi18n commented 3 months ago

current

in xxxx.vue

<script lang="ts" setup>
import router from '@/router'

definePage({
  meta: {
    title: () => `project: ${router.currentRoute?.query.code})`,
  },
  props: route => route.query,
})

but parsed to xxxx.vue?definePage&vue like this:

export default {
    meta: {
        title: ()=>`project: ${router.currentRoute?.query.code})`,
    },
    props: route=>route.query,
}

throw error Uncaught (in promise) ReferenceError: router is not defined

expect

plan A:parsed to xxxx.vue?definePage&vue with useful import by AST analysis plan B:<route /> support lang="ts":

<route lang="ts/js">
import router from '@/router'
export default {
    meta: {
        title: ()=>`project: ${router.currentRoute?.query.code})`,
    },
    props: route=>route.query,
}
</route>
posva commented 3 months ago

Imported variables already works, it seems like you have an outdated version from what I tested locally.

That being said, you can't use the router instance because it creates a cyclic import. If you need the router, pass it as an argument:

definePage({
  meta: {
    title: (router) => `project: ${router.currentRoute?.query.code})`,
  },
  props: route => route.query,
})