• vue3安装vue-router


    环境

    node 18.14.2
    yarn 1.22.19
    windows 11

    vite快速创建vue项目

    参考

    安装vue-touter

    官网

    yarn add vue-router@4
    
    • 1

    在这里插入图片描述
    src下新建router文件夹,该文件夹下新建index.ts

    // router/index.ts 文件
    import { createRouter, createWebHashHistory, RouterOptions, Router, RouteRecordRaw } from 'vue-router'
    //由于router的API默认使用了类型进行初始化,内部包含类型定义,所以本文内部代码中的所有数据类型是可以省略的
    //RouterRecordRaw是路由组件对象
    const routes: RouteRecordRaw[] = []
    // RouterOptions是路由选项类型
    const options: RouterOptions = {
     history: createWebHashHistory(),
     routes,
    }
    
    // Router是路由对象类型
    const router: Router = createRouter(options)
    
    export default router
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    修改mian.ts

    import { createApp } from "vue";
    import "./style.css";
    import App from "./App.vue";
    import router from "./router/index";
    
    const app = createApp(App);
    app.use(router);
    app.mount("#app");
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    修改app.vue

    <script setup lang="ts">
    import { RouterView } from 'vue-router';
    script>
    
    <template>
      <router-view>router-view>
    template>
    
    <style scoped>style>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    新建views文件夹,该文件夹下新建Home.vue和About.vue

    <script lang="ts" setup>
    script>
    <template>
        <h1>Homeh1>
    template>
    
    <script lang="ts" setup>
    script>
    <template>
        <h1>About Ush1>
    template>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    修改ruouter下index.ts

    // router/index.ts 文件
    import { createRouter, createWebHashHistory, RouterOptions, Router, RouteRecordRaw } from 'vue-router'
    //由于router的API默认使用了类型进行初始化,内部包含类型定义,所以本文内部代码中的所有数据类型是可以省略的
    //RouterRecordRaw是路由组件对象
    const routes: RouteRecordRaw[] = [
     { path: '/', name: 'Home', component: () => import('../views/Home.vue') },
     { path: '/about', name: 'About', component: () => import('../views/About.vue') },
    ]
    
    // RouterOptions是路由选项类型
    const options: RouterOptions = {
     history: createWebHashHistory(),
     routes,
    }
    
    // Router是路由对象类型
    const router: Router = createRouter(options)
    
    export default router
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    访问
    在这里插入图片描述
    在这里插入图片描述
    说明配置成功了

  • 相关阅读:
    日志打印管理
    永恒之蓝漏洞复现
    【webpack】HMR热更新原理
    java源码系列:HashMap底层存储原理详解——3、技术本质-原理过程-算法之哈希算法、哈希code、ascii码计算、取模运算等
    Java 异步编程 (5 种异步实现方式详解)
    .npy转.mat
    系分 - 系统测试与维护
    Oracle-大表改造分区表实施步骤
    断言语句 assertion
    “优化”城市出行体验——山海鲸智慧交通解决方案
  • 原文地址:https://blog.csdn.net/qq_36437991/article/details/134474050