question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Can I introduce router objects in ts files

See original GitHub issue

App.vue

<template>
  <button @click="toRoute('A')">
    router To A
  </button>
</template>

<script setup lang="ts">
import { toRoute } from '../utils/router'
</script>

that router how use

router.ts

export function toRoute(name: string) {
  router // that router how use
}

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5

github_iconTop GitHub Comments

1reaction
reainkcommented, Dec 23, 2021

@techieoriname main.ts

import generatedRoutes from 'virtual:generated-pages'
import { setupLayouts } from 'virtual:generated-layouts'
import { setRouter } from '~/router'

const routes = setupLayouts(generatedRoutes)

export const createApp = ViteSSG(
    App,
    {
        routes,
    },
    (ctx) => {
      const { isClient, router } = ctx
      if (!isClient) return

      setRouter(router)
    }
)

~/router.ts

import { Router } from 'vue-router'

let router: Router

export function setRouter(r: Router): void {
  router = r
}

export { router }

custom.ts

import { router } from '~/router'
import routes from 'virtual:generated-pages'

console.log(router, routes)

0reactions
techieorinamecommented, Dec 23, 2021

@nshusr that would work with the regular vue structure

But looking at something as shown below, the routes file only exports an array of routes.

main.ts

import routes from "./routes"; // RouteRecordRaw[]

export const createApp = ViteSSG(
    App,
    {
        routes,
    },
    (ctx) => {}
)

routes.ts

import type { RouteRecordRaw } from "vue-router";

export default <RouteRecordRaw[]>[
    {
        path: "/",
        component: () => import("./pages/Index.vue")
    }
]
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to Use React Router in Typescript | Pluralsight
React Router is one of the most popular routing libraries in the React ecosystem. It is a well thought out library with an...
Read more >
TypeScript/Angular: Adding an interface to data property in ...
In the app-routing-module.ts file, we can use the data property to insert our customized fields/variables like so:
Read more >
TypeScript Express tutorial #1. Routing, controller, middleware
Another way to set up routing is to use the router object. Once you create a router object you can call the methods...
Read more >
Routing - ts - TUTORIAL - Angular
Create a configuration file for the app routes. Routes tell the router which views to display when a user clicks a link or...
Read more >
Introduction to Routing-Controllers - Web dev etc
Setting up the express server to use Routing Controllers. Create a file ./src/server.ts : // src/server.ts import ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found