首页 > 其他分享 >038、Vue3+TypeScript基础,使用router.push进行路由跳转并传参

038、Vue3+TypeScript基础,使用router.push进行路由跳转并传参

时间:2024-08-20 23:04:59浏览次数:5  
标签:传参 vue title 跳转 content TypeScript router import news

01、main.js

// 引入createApp用于创建Vue实例
import {createApp} from 'vue'
// 引入App.vue根组件
import App from './App.vue'

//引入路由
import router from './router'

const app = createApp(App);
//使用路由
app.use(router);
// App.vue的根元素id为app
app.mount('#app')

02、index.ts代码如下:

//创建路由并暴露出去
import {createRouter, createWebHistory} from 'vue-router'
import Home from '@/view/Home.vue'
import About from '@/view/About.vue'
import News from '@/view/News.vue'
import Detail from '@/view/Detail.vue'

const router = createRouter({
    history: createWebHistory(),
    routes: [
        {name: 'zhuye', path: '/home', component: Home},
        {name: 'guanyu', path: '/about', component: About},
        {
            name: 'xinwen', path: '/news', component: News,
            //子类的path不需要加斜杠
            children: [
                {
                    // 一定要使用name
                    name: 'neirong',
                    path: 'detail',
                    component: Detail,
                    props(route) {
                        return route.query
                    },
                },
            ]
        },
    ]
})

export default router

03、App.vue代码如下:

<template>
  <div class="app">
    <h2 class="title">App.Vue路由测试</h2>
    <Header></Header>
    <!-- 导航区-->
    <div class="navigate">
      <router-link :to="{name:'zhuye'}" class="nav-button">首页</router-link>
      <router-link :to="{name:'xinwen'}" class="nav-button">新闻</router-link>
      <router-link :to="{path:'/about'}" class="nav-button">关于</router-link>
    </div>
    <!-- 内容区-->
    <div class="mai-content">
      <RouterView></RouterView>
    </div>
  </div>
</template>

<script lang="ts" setup name="App">
// 界面会根据当前路由的变化,在RouterView所在的位置渲染不同的组件
import {RouterView} from 'vue-router'
import Header from './components/Header.vue'
</script>

<style scoped>
.app {
  background-color: #ddd;
  box-shadow: 0 0 10px;
  border-radius: 10px;
  padding: 20px;
}

.nav-button {
  display: inline-block; /* 让链接显示为块级元素,以便应用宽度和高度 */
  padding: 10px 20px; /* 内边距 */
  margin: 0 5px; /* 外边距,用于按钮之间的间隔 */
  text-decoration: none; /* 移除下划线 */
  color: white; /* 文本颜色 */
  background-color: #007bff; /* 背景颜色 */
  border-radius: 5px; /* 边框圆角 */
  transition: background-color 0.3s; /* 平滑过渡效果 */
}

.nav-button:hover {
  background-color: #0056b3; /* 鼠标悬停时的背景颜色 */
}

.nav-button.router-link-active {
  background-color: #28a745; /* 当前激活(路由匹配)时的背景颜色 */
}

.mai-content {
  /* 添加边框样式 */
  border: 2px solid #000; /* 边框宽度、样式和颜色 */
  border-radius: 5px; /* 可选:添加边框圆角 */
  padding: 20px; /* 可选:给内部内容添加一些内边距 */
  margin: 20px; /* 可选:给元素添加一些外边距,以便与其他元素隔开 */
}
</style>

04、Header.vue代码如下:

<template class="hop-head">
  <h2 class="title">Vue路由测试header</h2>
</template>

<script setup lang="ts" name="hop-head">
import {onMounted, onUnmounted} from 'vue'

onMounted(() => {
  console.log('header组件被挂载')
})
onUnmounted(() => {
  console.log('header组件被卸载')
})

</script>
<style scoped>
.title {
  text-align: center;
  word-spacing: 5px;
  margin: 50px 0;
  height: 70px;
  line-height: 70px;
  background-image: linear-gradient(45deg, gray, white);
  border-radius: 15px;
  box-shadow: 0 0 10px;
  font-size: 20px;
}
</style>

05、News.vue代码如下:

<template>
  <div class="app-container">
    <!-- 导航区域容器 -->
    <div class="sidebar">
      <ul class="news-list">
        <!--第三种写法-->
        <li v-for="news in newsList" :key="news.id">
          <button @click="showNewsDetail(news)">查看新闻</button>
          <router-link :to="{
            name: 'neirong',
            query: {
              id: news.id,
              title: news.title,
              // 就算路由不写这个参数,也可以正常跳转,因为路由带个问号
              content: news.content
            }
          }">
            {{ news.title }}
          </router-link>
        </li>
      </ul>
    </div>
    <!-- 内容区域容器 -->
    <div class="main-content">
      <RouterView></RouterView>
    </div>
  </div>
</template>

<script setup lang="ts" name="news">
import {reactive} from "vue";
import {RouterLink, RouterView, useRouter} from "vue-router";

const newsList = reactive([
  {id: 1, title: '新闻1', content: '内容1'},
  {id: 2, title: '新闻2', content: '内容2'},
  {id: 3, title: '新闻3', content: '内容3'},
])

interface NewsInster {
  id: string;
  title: string;
  content: string;
}

const router = useRouter();

function showNewsDetail(news: NewsInster) {
  console.log(news);
  router.push({
    name: 'neirong',
    query: {
      id: news.id,
      title: news.title,
      // 就算路由不写这个参数,也可以正常跳转,因为路由带个问号
      content: news.content
    }
  })
}
</script>

<style scoped>
.app-container {
  display: flex; /* 使用Flexbox布局 */
}

.sidebar {
  width: 180px; /* 导航栏宽度 */
  padding: 20px;
  box-sizing: border-box; /* 防止padding影响元素总宽度 */
}

.news-list {
  list-style-type: none;
  padding: 0;
}

.news-list li {
  margin-bottom: 10px; /*每一条间距*/
}

.main-content {
  flex-grow: 1; /* 内容区域占据剩余空间 */
  padding: 20px;
  overflow-y: auto; /* 如果内容过多,允许垂直滚动 */
}
</style>

06、About.vue代码如下:

<template>
  <div class="about">
    <h2>我是About页面</h2>
  </div>
</template>

<script setup lang="ts" name="about">
</script>

<style scoped>
</style>

07、Detail.vue代码如下:

<template>
  <ul class="news-list">
    <li>编号:{{ id }}</li>
    <li>编号:{{ title }}</li>
    <li>编号:{{ content }}</li>
  </ul>
</template>

<script setup lang="ts" name="home">
defineProps(['id', 'title', 'content'])
</script>

<style scoped>
.news-list {
  /* 添加边框样式 */
  border: 2px solid #000; /* 边框宽度、样式和颜色 */
  border-radius: 5px; /* 可选:添加边框圆角 */
  padding: 20px; /* 可选:给内部内容添加一些内边距 */
  margin: 20px; /* 可选:给元素添加一些外边距,以便与其他元素隔开 */
}
</style>

08、Home.vue代码如下:

<template>
  <div class="home">
    <h2>我是Home页面</h2>
  </div>
</template>

<script setup lang="ts" name="home">
</script>

<style scoped>
</style>

09、效果如下、点击之后,可以跳转。

 

标签:传参,vue,title,跳转,content,TypeScript,router,import,news
From: https://www.cnblogs.com/tianpan2019/p/18370530

相关文章

  • 037、Vue3+TypeScript基础,使用router.push进行导航式路由跳转
    01、main.js代码如下://引入createApp用于创建Vue实例import{createApp}from'vue'//引入App.vue根组件importAppfrom'./App.vue'//引入路由importrouterfrom'./router'constapp=createApp(App);//使用路由app.use(router);//App.vue的根元素id为ap......
  • 036、Vue3+TypeScript基础,路由中使用replace不让前进后退
    01、代码如下:<template><divclass="app"><h2class="title">App.Vue路由测试</h2><!--导航区--><divclass="navigate"><router-linkreplaceto="/Home"class="nav......
  • TypeScript学习之旅--编译选项-tsconfig.json
    上一篇文章提到了tsconfig.json文件中的简单配置项,如include、extends、exclude,本篇文章我们了解一下编译器选择compilerOptions1、target  用来指定ts被编译为js的ES版本有固定值,例如:‘es3’ 'es5' 'es6''es2015' 'es2018'等“target”:"es2915"2、moud......
  • 033、Vue3+TypeScript基础,路由传参时候把层级脱掉
    01、Datail.vue代码如下:<template><ulclass="news-list"><li>编号:{{route.query.id}}</li><li>编号:{{route.query.title}}</li><li>编号:{{route.query.content}}</li></ul></tem......
  • TypeScript学习之旅--编译选项
    本文简单聊一下TS文件的编译,以及TS编译的配置文件入门我们都知道TS文件需要先编译成js文件后才可以运行,编译TS文件可以在命令行执行  tsc ts文件名,得到对应的同名js文件,但每次改完代码后都需要重新执行编译,或者多个ts文件都需要多次编译,给我们的编码工作带来极大不便~编译......
  • TypeScript学习之旅--对象数据类型
    1、对象类型声明1leta:{name:string,age:number};2a={name:"John",age:30};如果想设定多个参数且不确定参数类型,可用any,类似于参数列表,不限制参数个数和类型1leta:{name:string,age:number,[pro:string]:any};2a={name:"John",age:30,pro1:100,pro2:"Hello"};......
  • TypeScript学习之旅--数据类型
    TypeScript为JavaScript的超集(ECMAScript6),这个语言添加了基于类的面向对象编程。TypeScript作为JavaScript很大的一个语法糖,本质上是类似于css的less、sass,都是为了易于维护、开发,最后还是编译成JavaScript。Types所有类型都是any类型的子类型,其他类型被分成元类型(pri......
  • 030、Vue3+TypeScript基础,路由中History和HashHistory的区别
    01、index.ts路由代码如下://创建路由并暴露出去import{createRouter,createWebHistory}from'vue-router'importHomefrom'@/view/Home.vue'importAboutfrom'@/view/About.vue'importNewsfrom'@/view/News.vue'constrouter=cr......
  • 029、Vue3+TypeScript基础,路由组件和一般组件的存放位置,以及页面生命周期
    01、main.js代码如下://引入createApp用于创建Vue实例import{createApp}from'vue'//引入App.vue根组件importAppfrom'./App.vue'//引入路由importrouterfrom'./router'constapp=createApp(App);//使用路由app.use(router);//App.vue的根元素id为ap......
  • 028、Vue3+TypeScript基础,使用路由功能实现页面切换效果
    01、在main.js中引入路由并使用路由,代码如下://引入createApp用于创建Vue实例import{createApp}from'vue'//引入App.vue根组件importAppfrom'./App.vue'//引入路由importrouterfrom'./router'constapp=createApp(App);//使用路由app.use(router);//App......