当前位置: 首页 > article >正文

vue3安装vue-router

环境

node 18.14.2
yarn 1.22.19
windows 11

vite快速创建vue项目

参考

安装vue-touter

官网

yarn add vue-router@4

在这里插入图片描述
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

修改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");

修改app.vue

<script setup lang="ts">
import { RouterView } from 'vue-router';
</script>

<template>
  <router-view></router-view>
</template>

<style scoped></style>

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

<script lang="ts" setup>
</script>
<template>
    <h1>Home</h1>
</template>

<script lang="ts" setup>
</script>
<template>
    <h1>About Us</h1>
</template>

修改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

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


http://www.kler.cn/news/133978.html

相关文章:

  • 云计算(Docker)
  • 文件转换,简简单单,pdf转word,不要去找收费的了,自己学了之后免费转,之后就复制粘贴就ok了
  • how to find gcc openbug
  • mysql 设置远程登录
  • 【数据结构&C++】二叉平衡搜索树-AVL树(25)
  • 系列五、怎么查看默认的垃圾收集器是哪个?
  • Java 语言关键字有哪些
  • 【0234】PgBackendStatus 记录当前postgres进程的活动状态
  • GDPU 数据结构 天码行空10
  • 华为OD机试 - 转盘寿司(Java JS Python C)
  • Springboot更新用户头像
  • 大语言模型的三阶段训练
  • vim指令
  • promise时效架构升级方案的实施及落地 | 京东物流技术团队
  • 【Go入门】 Go搭建一个Web服务器
  • 340条样本就能让GPT-4崩溃,输出有害内容高达95%?OpenAI的安全防护措施再次失效
  • 电路的基本原理
  • DeepStream--测试resnet50分类模型
  • 大数据-玩转数据-Centos7 升级JDK11
  • Flink之KeyedState
  • R语言——taxize(第二部分)
  • 036、目标检测-锚框
  • 集合的运算
  • uni-app下,页面跳转后wacth持续监听的问题处理
  • LeetCode:689. 三个无重叠子数组的最大和(dp C++)
  • Java基础- StringBuilder StringBuffer
  • Android图片涂鸦,Kotlin(1)
  • k8s_base
  • 物联网AI MicroPython学习之语法 I2C总线
  • 基于SpringBoot+Redis的前后端分离外卖项目-苍穹外卖(五)