Vue3+router路由重定向-别名

重定向 redirect 别名 alias

//引入路由对象
import { createRouter, createWebHistory, RouteRecordRaw } from "vue-router";

//路由数组的类型 RouteRecordRaw
const routes: Array<RouteRecordRaw> = [
  {
    path: "/",
    name: "Login",
    component: () => import("../components/login.vue"),
    /* redirect: '/home', //路由重定向第一种书写方式 */
    /*  redirect: {
      path: '/home'
    }, //路由重定向第二种书写方式 */
    redirect: (to) => {
      return {
        path: "/home",
        query: {
          name: "zs",
        },
      };
    },//路由重定向第三种书写方式 
    alias: ['/login', '/login1'], //别名 alias 可以起多个名字
    children: [
      {
        path: "/home", // 默认展示
        name: "Home",
        component: () => import("../components/home.vue"),
      },
      {
        path: "/hello",
        name: "Hello",
        component: () => import("../components/HelloWorld.vue"),
      },
    ],
  },
];

const router = createRouter({
  history: createWebHistory(),
  routes,
});

export default router;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 Vite + Vue 3 + TypeScript 的项目中,你可以使用 Vue Router 来处理路由,并调用接口。 首先,确保你已经安装了 Vue Router 和 axios(或其他用于发送 HTTP 请求的库)。你可以使用以下命令进行安装: ``` npm install vue-router axios ``` 接下来,在你的项目中创建一个 `router` 文件夹,并在其中创建一个 `index.ts` 文件。在该文件中,你可以配置你的路由。以下是一个示例: ```typescript import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router'; import Home from '@/views/Home.vue'; import About from '@/views/About.vue'; const routes: Array<RouteRecordRaw> = [ { path: '/', name: 'Home', component: Home, }, { path: '/about', name: 'About', component: About, }, ]; const router = createRouter({ history: createWebHistory(), routes, }); export default router; ``` 在上面的示例中,我们定义了两个路由:`Home` 和 `About`。你可以根据你的需求进行修改和扩展。 然后,在你的入口文件(如 `main.ts`)中,引入并使用你的路由: ```typescript import { createApp } from 'vue'; import App from './App.vue'; import router from './router'; const app = createApp(App); app.use(router); app.mount('#app'); ``` 现在,你可以在你的组件中使用 `vue-router` 进行路由导航和调用接口。例如,在 `Home.vue` 组件中,你可以这样使用: ```vue <template> <div> <h1>Home</h1> <button @click="fetchData">Fetch Data</button> </div> </template> <script> import axios from 'axios'; export default { methods: { fetchData() { axios.get('/api/data').then((response) => { console.log(response.data); }).catch((error) => { console.error(error); }); }, }, }; </script> ``` 在上面的示例中,我们使用了 axios 发送 GET 请求来获取数据。你可以根据你的需求调整和扩展这个例子。 希望这能帮助到你!如果你有任何其他问题,请随时问我。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值