【Vue】通过vue-router实现页面跳转

一、准备工作

1、创建一个Vue-cli程序

博客链接:CSDN

2、安装vue-router

npm install vue-router --save-dev

d11c92f1907e4ef9bd937ac4b4dc1df2.png

3、删除多余的东西

090d14624cd4497a9ceee783ce44a8e7.png

二、创建router

1、在src下创建router包

1fdc6a884154438c836fddc70e84cbcb.png

2、创建跳转的component

分别创建一个Content.vue和Main.vue文件

895862818eba4688a0da5eb419339160.png

3、在router包下创建index.js文件

index.js文件中包含了所需的路由信息,详细操作如下代码,注释详解。

import Vue from 'vue'//导入vue的语法
import VueRouter from 'vue-router'//导入vue-router
import Content from "../components/Content";//导入创建的组件
import Main from "../components/Main";
//安装路由
Vue.use(VueRouter);//通过此语句使导入的VueRouter路由生效

//配置导出路由,注意VueRouter名要一致
export default new VueRouter({
  routes:[{
    //路由路径 @RequestMapping相似
    path: '/content',
    //名字
    name:'content',
    //跳转的组件
    component:Content
    //以上语句说明,当我们访问到'/content'路由时,就会跳转到Content组件,显示该vue页面
  },{
    //路由路径
    path: '/main',
    //名字
    name:'main',
    //跳转的组件
    component:Main
  }
  ]
})

三、router跳转

上面把我们需要做的东西装备好之后,现在来实现一下路由跳转的功能。

流程:

dc98ddd88bb94d4e863877286ccfd341.png

main.js代码:

import Vue from 'vue'
import App from './App'
//文件在当前目录下的router下,自动扫秒里面的路由配置
import router from './router'
Vue.config.productionTip = false
new Vue({
  el: '#app',
  //配置路由,以便全局使用
  router,
  components:{App},
  template:'<App/>'
})

App.vue代码:

<template>
  <div id="app">
    <h1>Vue-Router</h1>
<!--  控制路由  -->
    <router-link to="/main">首页</router-link>
    <router-link to="/content">内容页</router-link>
<!--  控制页面展示  -->
    <router-view></router-view>
  </div>
</template>

<script>
export default {
  name: 'App',
}
</script>

<style>
#app {
  font-family: 'Avenir', Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}
</style>

效果:

62af7ba4673f439e897404c3c1c034ed.png

783ba2e0442044eca925e22ef6e3ecbe.png

c0e59b4cf216408a852e98353738c759.png

四、总结

     这部分内容是比较简单的了,但是我个人觉得对于原来是后端开发的想学一些关于vue页面跳转,数据交互的小伙伴来说还是有点帮助的。希望自己的博客能清楚的介绍这些知识点,如果有所帮助,记得支持博主一波哦!

 

相关推荐

  1. [Vue3]-router实现基本的页面

    2024-05-04 00:04:01       53 阅读
  2. vue怎么页面

    2024-05-04 00:04:01       40 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-05-04 00:04:01       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-05-04 00:04:01       100 阅读
  3. 在Django里面运行非项目文件

    2024-05-04 00:04:01       82 阅读
  4. Python语言-面向对象

    2024-05-04 00:04:01       91 阅读

热门阅读

  1. C语言什么是指向函数的指针?

    2024-05-04 00:04:01       42 阅读
  2. Linux / Ubuntu 备份数据

    2024-05-04 00:04:01       39 阅读
  3. Mysql

    Mysql

    2024-05-04 00:04:01      38 阅读
  4. 【CAN】知识点:CAN故障与错误帧详解

    2024-05-04 00:04:01       29 阅读
  5. 数据库漫谈-发展简史

    2024-05-04 00:04:01       36 阅读
  6. 【leetcode】二分搜索题目总结

    2024-05-04 00:04:01       32 阅读
  7. 【Leetcode】63- 不同路径II

    2024-05-04 00:04:01       40 阅读
  8. 5.3作业

    2024-05-04 00:04:01       28 阅读
  9. 日拱一卒,月进一步(13)

    2024-05-04 00:04:01       41 阅读