Can we have a Route by default in Vue.js like Asp.net MVC?

460 Views Asked by At

In Asp.net MVC by default a Route as

routes.MapRoute (
                 name: "Default",
                 url: "{controller}/{action}/{id}",
                 defaults: new
                 {
                     controller = "Dashboard",
                     action = "Home",
                     id = UrlParameter.Optional
                 },
                 namespaces: new string [] {"***.Controllers"}
                 ).DataTokens ["area"] = "Main";

It is written by default and the rest of the routes in the project are determined in the controllers section with ActionResults. Is this possible in Vue.js or should all the routes and components be introduced to createRouter?

As follows:

import {createRouter, createWebHistory} from "vue-router";
import App from "@/App";
import App from "@/Post";

const router = createRouter({
    history: createWebHistory(),
    routes: [
        {path: '/', component: App},
        {path:'/post',component:Post}
    ]
});

export default router;

I want all the routes to run inside the route without introducing their component and the page to be changed, is this possible?

1

There are 1 best solutions below

0
Ruslan Hryshyn On

In vue-router you can create a route that handles all path that is not listed in your routes config

{
  path: '*',
  redirect: '/post', // for example
}

Or without redirect, if you don't want to change the user URL

{
  path: '*',
  component: Post,
}