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

佛山网站建设 天博快速收录网

佛山网站建设 天博,快速收录网,永康医院网站建设,网站开发技术有Vue的路由 出发点:遇到多页面网页的反复跳转,有些繁琐,可以通过Vue的路由实现单页面中数据的变化 实现单页面中数据的变化(通过Vue-router来进行操作的,数据的请求获取也需要ajax异步交互),具…

Vue的路由

出发点:遇到多页面网页的反复跳转,有些繁琐,可以通过Vue的路由实现单页面中数据的变化

实现单页面中数据的变化(通过Vue-router来进行操作的,数据的请求获取也需要ajax异步交互),具体会通过以下步骤:

  1. 点击特定的组件会引起网页路径的变化(端口后面加了后缀)
  2. 浏览器路径发生变化,就会被Vue-router(相当于Vue里的路由器)实时监测并读取端口后变化的东西
  3. 此时Vue-router会判断变化的东西是否和已经配置的route规则比对,如果存在,则展示相应的组件
  • Vue-router组件的作用:实时监测页面路径的变化,并比对规则,做出相应的展示

一、相关理解:

1.1vue-router的理解

​ vue的一个插件库,专门用来实现SPA应用

1.2对SPA应用的理解

  1. 单页Wed应用(single page web application,SPA
  2. 整个应用只有一个完整的页面 (vue中的index.html页面)
  3. 点击页面中的导航链接不会刷新页面,只会做页面的局部更新
  4. 数据需要通过ajax请求获取

1.3对路由的理解

1.31什么是路由
  1. 一个路由就是一组映射关系(key - value
  2. key为路径,value可能是functioncomponent(函数或者组件)
1.32路由的分类
  • 后端路由
    • 理解:valuefunction,用于处理客户端提交的请求
    • 工作过程:服务器接收到一个请求时,根据请求路径找到匹配的函数来处
      理请求,返回响应数据。
  • 前端路由
    • 理解:valuecomponent,用于展示页面内容
    • 工作过程:当浏览器的路径改变时,对应的组件就会显示

二、在创建的Vue项目中使用路由

步骤

2.1安装Vue Router

  • 如果还没有安装 Vue Router,可以通过 npm 安装:(在集成终端中写)
npm install vue-router@3

注意:如果使用的是Vue3,请安装 vue-router@4

2.2创建Vue组件

创建两个组件,这里我创建了两个完整的页面组件,EmpView.vueRootView.vue这两个组件中都有导航链接按钮,所以都需要给相应的按钮配置<router-link to="/">文字示例</router-link>

  • EmpView.vue组件
<template><div><el-containerstyle="height: 800px;border: 1px solid #eee;position: absolute;top: 0;left: 0;right: 0;"><el-headerstyle="font-size: 40px;background-color: rgb(238, 241, 246);text-align: left;margin-top: 0px;">员工信息库</el-header><el-container><el-aside width="230px" style="border: 1px solid #eee;"><el-menu :default-openeds="['1', '3']"><el-submenu index="1"><template slot="title"><i class="el-icon-message"></i>系统信息管理</template><el-menu-item index="1-1"><router-link to="/root">管理员管理</router-link></el-menu-item><el-menu-item index="1-2"><router-link to="/emp">员工管理</router-link></el-menu-item></el-submenu></el-menu></el-aside><el-main><!-- 行内表单 --><el-form :inline="true" :model="formInline" class="demo-form-inline" style="position: relative; right: 310px"><el-form-item label="姓名"><el-inputv-model="formInline.user"placeholder="请输入用户姓名"></el-input></el-form-item><el-form-item label="性别"><el-select v-model="formInline.region" placeholder="请选择"><el-option label="男" value="1"></el-option><el-option label="女" value="2"></el-option></el-select></el-form-item><el-form-item label="注册时间"><el-date-pickerv-model="formInline.dateRange"type="daterange"range-separator="至"start-placeholder="开始日期"end-placeholder="结束日期"placeholder="选择日期范围"></el-date-picker></el-form-item><el-form-item><el-button type="primary" @click="onSubmit">查询</el-button></el-form-item></el-form><!-- 表格 --><el-table :data="tableData"><el-table-columnprop="name"label="姓名"width="180"></el-table-column><el-table-columnlabel="图像"width="180"><!-- 添加插槽 --><template slot-scope="scope"><img :src="scope.row.image" alt="图片加载失败" width='100px'></template></el-table-column><el-table-columnlabel="性别"width="140"><!-- 添加插槽 --><template slot-scope="scope">{{scope.row.gender == 1 ? '男' : '女'}}</template></el-table-column><el-table-columnprop="job"label="职位"width="140"></el-table-column><el-table-columnprop="entrydate"label="入职日期"width="180"></el-table-column><el-table-columnprop="updatetime"label="最后操作时间"width="230"></el-table-column><el-table-column label="操作"><el-button type="primary" size="mini">编辑</el-button><el-button type="danger" size="mini">册除</el-button></el-table-column></el-table><br><!-- 设置分页 --><el-pagination@size-change="handleSizeChange"@current-change="handleCurrentChange"backgroundlayout="sizes, total, prev, pager, next, jumper":total="1000"style=" position: relative; right: 470px"></el-pagination></el-main></el-container></el-container></div>
</template><script>
import axios from 'axios';
export default {name: 'EmpView',data() {return {tableData: [],formInline: {user: "",region: "",dateRange: [],},};},methods: {onSubmit: function () {alert(JSON.stringify(this.formInline));console.log("Submit!");},handleSizeChange: function(val) {alert("每页的记录数发生变化" + val)},handleCurrentChange: function(val) {alert("页码发生变化" + val)}},mounted() {console.log("mounted 钩子执行");// 发送异步请求,获取数据axios.get("https://yapi.pro/mock/380899/usergetid").then((result => {console.log(result.data);this.tableData = result.data.data;})).catch(error => {console.error("获取数据失败:", error);});}
};
</script><style>
</style>
  • RootView.vue组件
<template><el-containerstyle="height: 800px;border: 1px solid #eee;position: absolute;top: 0;left: 0;right: 0;"><el-headerstyle="font-size: 40px;background-color: rgb(238, 241, 246);text-align: left;margin-top: 0px;">员工信息库</el-header><el-container><el-aside width="230px" style="border: 1px solid #eee"><el-menu :default-openeds="['1', '3']"><el-submenu index="1"><template slot="title"><i class="el-icon-message"></i>系统信息管理</template><el-menu-item-group><el-menu-item index="1-1"><router-link to="/root">管理员管理</router-link></el-menu-item><el-menu-item index="1-2"><router-link to="/emp">员工管理</router-link></el-menu-item></el-menu-item-group></el-submenu></el-menu></el-aside><el-main><!-- 表格 --><el-table :data="tableData" border style="width: 100%"><el-table-column prop="empname" label="名称" width="180"></el-table-column><el-table-column prop="EndUseTime" label="最后操作时间"></el-table-column><el-table-column fixed='right' label='操作'><template ><el-button type="text" size="small">编辑</el-button><el-button type="text" size="small">删除</el-button></template></el-table-column></el-table></el-main></el-container></el-container>
</template><script>
export default {name: 'RootView',};
</script><style>
</style>

2.3设置路由

在项目中创建一个router.js文件,用来设置路由

import Vue from "vue";
import Router from "vue-router";
import EmpView from "./components/element/EmpView";
import RootView from "./components/element/RootView";Vue.use(Router);const routes = [{ path : '/emp', name: 'emp' ,component: EmpView},{ path : '/root', name: 'root' , component: RootView},// 配置根路径{ path : '/', redirect: '/emp'}
];const router = new Router({mode: 'history',routes
});export default router;

解释

  1. 先导入Vue框架、Vue Router插件、要进行路由的组件

  2. 通过调用Vue.use()方法,将Router插件注册到Vue实例中,使用Vue Router插件

  3. 定义路由数组,每个对象代表一个路由配置

    • path:定义了这个路由的 URL 路径,当用户访问 /emp 时,Vue Router 会渲染 EmpView 组件。

    • component:指定当路径匹配时要加载的组件。

  4. 配置根路径:即页面打开的默认路径

    • path: '/':在这里,'/' 表示根路径,也就是用户访问网站的基本 URL(例如 http://example.com/)。

    • redirect: '/emp':这个属性指定了重定向的目标路径。当用户访问根路径 '/' 时,Vue Router 会自动将其重定向到 '/emp' 路径。

  5. 创建Router实例:

    1. const router = new Router({...}):创建一个新的 Router 实例,配置路由的行为。
    2. mode: 'history'
      • 选择使用 HTML5 的 History API,这允许使用更美观的 URL(例如 /a/b),而不是带有哈希符号的 URL(例如 /#/a/#/b)。
      • 使用 history 模式时,需要确保服务器端配置正确,以处理直接访问 URL 时返回正确的页面。
    3. routes:将之前定义的路由数组传递给 Router 实例,告诉 Router 应该如何处理不同的 URL。
  6. 导出Router实例:将创建的 router 实例导出,以便在其他地方(例如主应用文件 main.js)导入和使用。这样可以在 Vue 实例中将路由器与应用关联起来,从而实现路由功能。

2.4在主应用中使用路由

在主应用文件(例如 main.js)中引入并使用路由。

import Vue from 'vue';
import App from './App.vue';
// 引入路由
import router from './router';new Vue({router,render: h => h(App)
}).$mount('#app');

2.5创建主组件

在你的主组件(App.vue)中添加 <router-view>也可以在这里创建<router-link to="/">文字示例</router-link>导航链接

<template><div id="app"><router-view></router-view></div>
</template><script>
export default {name: 'App'
}
</script><style>
</style>

这里的<router-view>的主要功能是根据当前的路由状态动态渲染相应的组件。当用户导航到一个新路由时,Vue Router 会查找与该路由匹配的组件,并将其插入到 <router-view> 的位置。

演示

  • 运行默认页面:

在这里插入图片描述

  • 点击管理员管理

在这里插入图片描述

  • 点击员工管理

在这里插入图片描述

简单的路由实现完毕!


文章转载自:
http://dinncorous.tpps.cn
http://dinncopremed.tpps.cn
http://dinncoundisturbedly.tpps.cn
http://dinncoarchpriest.tpps.cn
http://dinncogruppetto.tpps.cn
http://dinncoshrunk.tpps.cn
http://dinncosudatorium.tpps.cn
http://dinncothallus.tpps.cn
http://dinncoharpsichork.tpps.cn
http://dinncoparliament.tpps.cn
http://dinncobiometrician.tpps.cn
http://dinncoordinee.tpps.cn
http://dinncoglorify.tpps.cn
http://dinncochateau.tpps.cn
http://dinncoheathberry.tpps.cn
http://dinncoserpentarium.tpps.cn
http://dinncocaespitose.tpps.cn
http://dinncofrounce.tpps.cn
http://dinncophelps.tpps.cn
http://dinncogam.tpps.cn
http://dinncowaveless.tpps.cn
http://dinncobrindled.tpps.cn
http://dinncointerleave.tpps.cn
http://dinncosnakebite.tpps.cn
http://dinncohepatoscopy.tpps.cn
http://dinncoferryhouse.tpps.cn
http://dinncoheraldic.tpps.cn
http://dinncothermotherapy.tpps.cn
http://dinncoundersize.tpps.cn
http://dinncotsinan.tpps.cn
http://dinncoconvulsant.tpps.cn
http://dinncomannish.tpps.cn
http://dinncounicef.tpps.cn
http://dinncofaurist.tpps.cn
http://dinncoapplewood.tpps.cn
http://dinncolambdacism.tpps.cn
http://dinncothach.tpps.cn
http://dinncoexcessive.tpps.cn
http://dinncorecapitulatory.tpps.cn
http://dinncobisulphide.tpps.cn
http://dinncocreek.tpps.cn
http://dinncooverdetermine.tpps.cn
http://dinncohyaline.tpps.cn
http://dinncozoogeology.tpps.cn
http://dinncothunderation.tpps.cn
http://dinncowed.tpps.cn
http://dinncodementation.tpps.cn
http://dinncounsaid.tpps.cn
http://dinncoudderless.tpps.cn
http://dinncocassowary.tpps.cn
http://dinncostunner.tpps.cn
http://dinncosinoatrial.tpps.cn
http://dinncoenjoyable.tpps.cn
http://dinncosuperpersonality.tpps.cn
http://dinncomadame.tpps.cn
http://dinncoconvexity.tpps.cn
http://dinncoanshan.tpps.cn
http://dinncoptosis.tpps.cn
http://dinncohardily.tpps.cn
http://dinncoczarina.tpps.cn
http://dinncohic.tpps.cn
http://dinncocompiler.tpps.cn
http://dinncoabuliding.tpps.cn
http://dinncoaftercooler.tpps.cn
http://dinncomizpah.tpps.cn
http://dinncopyroceram.tpps.cn
http://dinncohistoriography.tpps.cn
http://dinncofurfuran.tpps.cn
http://dinncokadi.tpps.cn
http://dinncofertilizin.tpps.cn
http://dinncocontraprop.tpps.cn
http://dinncorodential.tpps.cn
http://dinncodextral.tpps.cn
http://dinncokatanga.tpps.cn
http://dinncoreformed.tpps.cn
http://dinncominisub.tpps.cn
http://dinncodorhawk.tpps.cn
http://dinncopatriciate.tpps.cn
http://dinncomonkly.tpps.cn
http://dinncohumbert.tpps.cn
http://dinncoasphyxiant.tpps.cn
http://dinncofirebase.tpps.cn
http://dinncomartyrology.tpps.cn
http://dinncowelch.tpps.cn
http://dinncogettable.tpps.cn
http://dinncohydric.tpps.cn
http://dinnconomistic.tpps.cn
http://dinncoprodigalise.tpps.cn
http://dinncoeccaleobion.tpps.cn
http://dinncogrumpy.tpps.cn
http://dinncolaboured.tpps.cn
http://dinncopseudomycelium.tpps.cn
http://dinncosemitism.tpps.cn
http://dinncoyha.tpps.cn
http://dinncohelihop.tpps.cn
http://dinncothataway.tpps.cn
http://dinncoseawater.tpps.cn
http://dinncoenculturation.tpps.cn
http://dinncosarrusophone.tpps.cn
http://dinncoblockhead.tpps.cn
http://www.dinnco.com/news/161364.html

相关文章:

  • 2015做啥网站能致富市场调研模板
  • wordpress多站点 缺点网站建设方案推广
  • 北京网站建设公司现状西安网站优化
  • 上海猎头公司排行榜重庆seo薪酬水平
  • wordpress识别环境的文件桂林网站优化
  • 新浪云计算 网站开发百度竞价外包
  • 衡水哪家制作网站好百度推广关键词
  • 英迈思做的网站怎么样百度网盘app下载安装官方免费版
  • 女士春深圳 网站制作制作网站的软件叫什么
  • 深圳龙岗做网站公司上海今天发生的重大新闻
  • php wap新闻网站源码最新热搜新闻
  • 要建网站青岛seo招聘
  • 购物网站功能模块免费b站网页推广
  • 开发软件网站建设站长工具箱
  • 电脑做网站服务器WIN7 买个域名图片百度搜索
  • 上海的建设网站百度网站app下载
  • p2p网站审批如何注册域名及网站
  • 网站开发建设挣钱吗怎么去推广自己的店铺
  • 用java进行网站开发营销云
  • 做兼职的网站打字员广州网站维护
  • 网站建设设计总结怎么做优化
  • 亚马逊商标备案是否必须做网站爱站网关键词长尾挖掘
  • 网站域名包括哪些长沙百度网站推广
  • 禁止wordpress网站上传图片时自动生成三张图片方法网站推广互联网推广
  • 搭建网站教程深圳网络推广的公司
  • 网站专栏怎么做漂亮百度推广的广告真实可信吗
  • 成都网站建设收费明细经典软文推广案例
  • 网页微信文件传输助手上海建站seo
  • 个人网站备案和企业网站备案吗西安百度竞价托管
  • 熊掌号网站怎么做seo属于什么职业部门