VUE3-路由

router: 存放路由的文件夹

1.创建三个文件(相当于电脑)

About.vue Home.vue News.vue

2.创建路由index.ts(相当于路由器)

javascript 复制代码
//创建一个路由器,并暴露出去

//第一步:引入createRouter
import {createRouter,createWebHistory} from 'vue-router'
//引入一个一个可能要呈现组件
import Home from '@/components/Home.vue' 
import News from'@/components/News.vue'
import About from '@/components/About.vue'

//第二步:创建路由器
const router = createRouter({
    history:createwebHistory(),//路由器的工作模式
    routes:[//一个一个的路由规则
        {
            path:'/home'
            component:Home
        },
        {
            path:'/news'
            component:News
        },
        {
            path:'/about'
            component:About
        }
    ]
})

//暴露出去router
export default router

3.在main.ts中配置路由器(注意:第一步创建的三个文件夹要有内容)

javascript 复制代码
//引入路由器
import router from './router'
//创建一个应用
const app = createApp(App)
//使用路由器
app.use(router)
//挂载整个应用到app容器中
app.mount('#app')

4.展示页面(App.vue文件)

javascript 复制代码
<template>
    <div class="app'>
        <h2 class="title">Vue路由测试</h2>
        <!--导航区--><div class="navigate">
        <div>
            <RouterLink to="/home" active-class="xiaozhupeiqi">首页</RouterLink>
            <RouterLink to="/news" active-class="xiaozhupeiqi">新闻</RouterLink>
            <RouterLink to="/about" active-class="xiaozhupeiqi">关于</RouterLink>
        </div>

        <!--展示区-->
        <div class="main-content">
            <RouterView></RouterView>
        </div>
    </div>
</template>

<script lang="ts" setup name="App">
    import {RouterView,RouterLink} from'vue-router'
</script>

<style>
</style>

存放的l两个注意点

1.路由组件通常存放在pages或views文件夹,一般组件通常存放在components文件夹。

2.通过点击导航,视觉效果上"消失"了的路由组件,默认是被卸载掉的,需要的时候再去挂载"。

3.路由组件:靠路由的规则渲染出来的 一般组件:亲手写标签出来的

两种工作模式

  1. history模式

优点:URL更加美观,不带有#,更接近传统的网站URL。

缺点:后期项目上线,需要服务端配合处理路径问题,否则刷新会有404错误。

2.hash模式

优点:兼容性更好,因为不需要服务器端处理路径。

缺点:URL带有#不太美观,且在SE0优化方面相对较差。

to的两种写法

<!--第一种:to的字符串写法-->

<router-link active-class="active" to="/home">主</router-link>

<!--第二种:to的对象写法-->

<router-link active-class="active" :to=" (path: /home'}">Home</router-link>

嵌套路由

1.创建一个文件 Detail.vue

javascript 复制代码
<template>
    <ul class="news-list">
        <li>编号:xxx</li>
        <li>标题:xxx</li>
        <li>内容:xxx</li>
    </ul>
</template>

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

<style scoped>...
</style>

2.在index.ts路由器中追加子集路由children

javascript 复制代码
//引入一个要呈现组件
import Detail from@/pages/Detail.vue



//在第二步中的index.js中添加的children
        {
            path:'/news'
            component:News
            children:[
                {
                    path:'detail',
                    component:Detail
                }
            ]
        },

2.在News.vue文件里嵌套路由,需要在路由文件router文件夹中的index.ts中嵌套

javascript 复制代码
<template>
    <div class="news
        <ul>
            <li v-for="news in newsList" :key="news.id">
                <a href="#">{{news.title}}</a>
            </1i>
        </ul>
        <!--展示区-->
        <div class="news-content">
            <RouterView></RouterView>
        </div>
    </div>
</template>

<script setup lang="ts" name="News">

import {reactive} from 'vue

const newsList = reactive([
    {id:'asfdtrfayo1',title:'很好的抗癌食物',content:'西蓝花'},
    {id:'asfdtrfayo2',title:'如何一夜暴富',content:'学IT'},
    {id:'asfdtrfay03',title:震惊,万万没想到',content:'明天是周一'},
    {id:'asfdtrfay04',title:'好消息!',content:'快过年了'}
]}

</script>
相关推荐
BS_Li1 天前
【Linux网络编程】NAT、代理服务、内网穿透
linux·网络·智能路由器
普马萨特1 天前
当无线电波睁开双眼:从“盲管”到下一代空间感知模态
智能路由器
minxihou1 天前
TRINITY-Router: 用数据证伪LLM路由假设 - 8模型316题实验报告
人工智能·深度学习·语言模型·智能路由器
从零开始的代码生活_2 天前
NAT、代理服务与内网穿透详解
linux·服务器·网络·c++·http·智能路由器
儒雅的烤地瓜2 天前
计算机网络 | 路由器局域网(内网)IP和外网(公网)IP的区别与联系
网关·tcp/ip·智能路由器·ip·外网·内网·nat技术
星恒讯工业路由器2 天前
公网IP、NAT、端口映射:外网访问家里的“通关攻略”
网络·智能路由器·信息与通信·nat·端口映射·公网ip
fei_sun3 天前
AD管理距离(思科)
智能路由器
江华森20 天前
TCP/IP 协议栈实战 — 7 个实验详解
网络·tcp/ip·智能路由器
行走__Wz20 天前
【网工入门-eNSP模拟-07】单臂路由
网络·智能路由器
不方便,你要方便吗?20 天前
华为数通 HCIA 到 HCIE 学习大纲(第二部分:HCIP 篇)
智能路由器