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

vite ts vue中增加路由

一.首先建立脚手架工程:

npm init vite@4.4 projectname vue-tempelete

npm init vite@4.4 test2001 vue-tempelate

二.packages.json中增加这个

dependencies": {"vue": "^3.3.4","vue-router": "^4.2.4",

之后npm install来获取vue-router4.2.4

三.在src目录下

1.建立router目录,目录下建立index.ts文件

2.建立views目录,目录下建立About.vue与Home.vue

index.ts

// 本文件 router/index.ts
import {createRouter, createWebHistory, RouterOptions} from "vue-router";const routes = [{path: "/",component: () => import("../views/Home.vue"),},{path: "/about",component: () => import("../views/About.vue"),},
];const router = createRouter({history: createWebHistory(),routes,
} as RouterOptions);export default router;

Home.vue

<!-- src/views/Home.vue --><template><div class="vertical-layout"><header><h1>Home Page</h1></header><main><button @click="goToOtherPage">跳转到其他页面</button><p>Your main content goes here.</p></main><footer><p>Footer content goes here.</p></footer></div>
</template>
<script setup lang="ts">import { useRouter } from 'vue-router';const router = useRouter();const goToOtherPage = () => {console.log('Clicked!');// 在这里指定你想要跳转的路径router.push('/about');};
</script>
<style scoped>.vertical-layout {display: flex;flex-direction: column;align-items: center;justify-content: space-between;height: 100vh; /* 设置整个布局高度为视口高度,以确保内容在竖屏中居中显示 */}header, main, footer {width: 80%; /* 设置宽度为80% */min-width: 800px; /* 设置最小宽度为400px */max-width: 1200px; /* 设置最大宽度为1200px */padding: 20px;box-sizing: border-box;text-align: center;background-color: #f0f0f0;margin: 10px 0;}@media screen and (max-width: 600px) {/* 在小屏幕上,设置宽度为100% */header, main, footer {width: 100%;}}
</style>

About.vue

<!-- src/views/About.vue --><template><div class="vertical-layout"><header><h1>About Page</h1></header><main><button @click="goToOtherPage">跳转到其他页面</button><p>Your main content goes here.</p></main><footer><p>Footer content goes here.</p></footer></div>
</template>
<script setup lang="ts">import { useRouter } from 'vue-router';const router = useRouter();const goToOtherPage = () => {console.log('Clicked!');// 在这里指定你想要跳转的路径router.push('/');};
</script>
<style scoped>.vertical-layout {display: flex;flex-direction: column;align-items: center;justify-content: space-between;height: 100vh; /* 设置整个布局高度为视口高度,以确保内容在竖屏中居中显示 */}header, main, footer {width: 80%; /* 设置宽度为80% */min-width: 800px; /* 设置最小宽度为400px */max-width: 1200px; /* 设置最大宽度为1200px */padding: 20px;box-sizing: border-box;text-align: center;background-color: #f0f0f0;margin: 10px 0;}@media screen and (max-width: 600px) {/* 在小屏幕上,设置宽度为100% */header, main, footer {width: 100%;}}
</style>

main.ts当中这样写:

import { createApp } from 'vue'
import './style.css'
import App from './App.vue'

import router from './router';

createApp(App).use(router).mount('#app');

import { createApp } from 'vue'
import './style.css'
import App from './App.vue'import router from './router';createApp(App).use(router).mount('#app');

App.vue tempelete改写成以下(为的是加载到路由,不然还是只显示App.vue的内容):

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

另:可以考虑在vite.config.ts中增加别名配置

import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'// export default defineConfig({
//   plugins: [vue()],
// })export default defineConfig({base: "./",plugins: [vue()],resolve: {// src 别名配置alias: {// "@": resolve(__dirname, "src"),"@":  "/src",},// 使用路径别名时想要省略的后缀名,可以自己增减extensions: [".js", ".json", ".ts"],},
});

http://www.xdnf.cn/news/7680.html

相关文章:

  • 【HarmonyOS 5】金融应用开发鸿蒙组件实践
  • 基于PyTorch的医学影像辅助诊断系统开发教程
  • unity XCharts插件生成曲线图在UICanvas中
  • python训练 60天挑战-day31
  • 查看mysql配置文件my.cnf的位置
  • Docker-Harbor 私有镜像仓库使用指南
  • MyBatis 动态 SQL 标签详解教程:_set_、_trim_、_sql_、_choose_、_when_
  • 抢占短剧商业蓝海!AI 驱动 CPS 系统开发定制赋能高效变现
  • 数据结构*排序
  • tigase源码学习笔记-事件总线EventBus
  • 跨境外贸电商供应链一体化ERP管理系统
  • 波次拉料在精益生产中下的应用
  • OpenHarmony 5.0设置应用设置手势导航开关打开后重新关闭导航栏和设置界面重合
  • onlyoffice 源码 调试说明 -ARM和x86双模式安装支持
  • [Harmony]获取设备参数
  • SpringBoot 商城系统高并发引起的库存超卖库存问题 乐观锁 悲观锁 抢购 商品秒杀 高并发
  • 机械安全标准示例说明
  • 机器学习算法-聚类K-Means
  • 园区无人机智能巡检项目方案
  • 机器学习自然语言处理
  • SAP ECC即将停止支持,CIO如何应对S/4HANA的迁移挑战?
  • 【机器学习】logistic回归
  • Java 02入门,封装继承多态
  • 随记1-LLM多轮对话的陷阱
  • 操作系统学习笔记第5章 (竟成)
  • [解决方案] Word转PDF
  • Android SharedPreferences:从零到一的全面解析与实战指南
  • win10使用nginx做简单负载均衡测试
  • 省赛备考~全国青少年信息素养大赛-图形化编程复赛/省赛-模拟题-判断质数合数
  • JavaScript 数组方法详解:全面指南