Vue3学习之-- vue-router入门教程和总结

Vue Router 是Vue.js的官方路由器。它与 Vue.js 核心深度集成,使用 Vue.js 构建单页应用程序变得轻而易举


前言

官方文档:https://router.vuejs.org/introduction.html


一、安装和创建router文件

首先我们先安装依赖:

npm install vue-router@4

安装完成后在main.js文件同级创建router.js文件

图片如下(示例):
在这里插入图片描述

二、文件配置

1.main文件配置

代码如下(示例):

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

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

// 使用
const app = createApp(App)
app.use(router).mount('#app')

2.router文件配置

import {createRouter, createWebHistory} from 'vue-router'

const routerHistory = createWebHistory()

import Vindex from './views/index/Vindex.vue'
import Vlogin from './views/login/Vlogin.vue'
// 定义路由
const routes = [
    {
        path: '/index',
        name: 'Vindex',
        component: Vindex
    },
    {
        path: '/login',
        name: 'Vlogin',
        component: Vlogin
    },
]

// 创建路由器
const router = createRouter({
    history: routerHistory,
    routes: routes
})


export default router;

3.vue文件创建

1.在src目录下创建 views 文件:

在这里插入图片描述
2.在views目录下创建 loginindex 文件:
在这里插入图片描述
3.在 loginindex 目录下创建 Vindex.vueVlogin.vue 文件:
在这里插入图片描述

Vindex.vue内容:

<template>
  <div>index</div>
</template>

<script>
export default {
name: "Vindex"
}
</script>

<style scoped>

</style>

Vlogin.vue内容:

<template>
  <div>login</div>
</template>

<script>
export default {
name: "Vlogin"
}
</script>

<style scoped>

</style>

4.vue.config.js 文件配置

lintOnSave: false

图片如下(示例):
在这里插入图片描述

5.App.vue 文件配置

<router-view></router-view>

图片如下(示例):
在这里插入图片描述

三、启动访问

1.直接访问域名无路由

url: http://192.168.4.223:8080/

图片如下(示例):
在这里插入图片描述

2.直接访问index路由

url: http://192.168.4.223:8080/index
在这里插入图片描述

3.直接访问login路由

url: http://192.168.4.223:8080/login
在这里插入图片描述

基础路由配置完成

我的小程序二维码

(https://img-blog.csdnimg.cn/6f0dac267ad741c99198a3e724ffdca8.png)

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

荔枝学习

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值