vue3路由

router/index.ts

import {
  createRouter,
  createWebHistory,
  RouteRecordRaw,
  createWebHashHistory,
  RouteLocationNormalized,
  NavigationGuardNext,
} from "vue-router";

import layout from "../views/layout.vue";

const routes: Array<RouteRecordRaw> = [
  {
    path: "",
    redirect: "/home",
    component: layout,
    children: [
      {
        path: "/home",
        name: "Home",
        component: () => import("../views/Home.vue"),
        meta: {
          title: "首页",
          parentpath: "/home",
        },
      },
      {
        path: "/login",
        name: "login",
        component: () => import("../views/login.vue"),
        meta: {
          title: "登录/注册",
          parentpath: "/login",
        },
      },
    ],
  },
];
//决定路由模式
const isPro: boolean = process.env.NODE_ENV === "prodution";

const router = createRouter({
  //history是路由模式(线上线下)
  history: isPro
    ? createWebHashHistory(process.env.BASE_URL)
    : createWebHistory(process.env.BASE_URL),
  routes,
});

router.beforeEach(
  (
    to: RouteLocationNormalized,
    from: RouteLocationNormalized,
    next: NavigationGuardNext
  ) => {
    document.title = to.meta.title;
    console.log(from);
    next();
  }
);

export default router;


main.ts

import router from './router'

const app = createApp(App)

app.use(router)

app.mount('#app')

App.vue

<template>
  <div id='root'>
    <div id='nav'>
      <router-link to='/'> Home</router-link>
      <router-link to='/contact'>Contact </router-link>
    </div>
    <router-view />
  </div>
</template>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值