[{"createTime":1735734952000,"id":1,"img":"hwy_ms_500_252.jpeg","link":"https://activity.huaweicloud.com/cps.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=V1g3MDY4NTY=&utm_medium=cps&utm_campaign=201905","name":"华为云秒杀","status":9,"txt":"华为云38元秒杀","type":1,"updateTime":1735747411000,"userId":3},{"createTime":1736173885000,"id":2,"img":"txy_480_300.png","link":"https://cloud.tencent.com/act/cps/redirect?redirect=1077&cps_key=edb15096bfff75effaaa8c8bb66138bd&from=console","name":"腾讯云秒杀","status":9,"txt":"腾讯云限量秒杀","type":1,"updateTime":1736173885000,"userId":3},{"createTime":1736177492000,"id":3,"img":"aly_251_140.png","link":"https://www.aliyun.com/minisite/goods?userCode=pwp8kmv3","memo":"","name":"阿里云","status":9,"txt":"阿里云2折起","type":1,"updateTime":1736177492000,"userId":3},{"createTime":1735660800000,"id":4,"img":"vultr_560_300.png","link":"https://www.vultr.com/?ref=9603742-8H","name":"Vultr","status":9,"txt":"Vultr送$100","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":5,"img":"jdy_663_320.jpg","link":"https://3.cn/2ay1-e5t","name":"京东云","status":9,"txt":"京东云特惠专区","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":6,"img":"new_ads.png","link":"https://www.iodraw.com/ads","name":"发布广告","status":9,"txt":"发布广告","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":7,"img":"yun_910_50.png","link":"https://activity.huaweicloud.com/discount_area_v5/index.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=aXhpYW95YW5nOA===&utm_medium=cps&utm_campaign=201905","name":"底部","status":9,"txt":"高性能云服务器2折起","type":2,"updateTime":1735660800000,"userId":3}]
vue-router是vue框架中的一个插件。
vue-router实现原理
vue-router通过hash与History interface两种方式实现前端路由,更新页面内容但不重新请求页面”是前端路由原理的核心之一。
使用的具体方法
1.安装vue-router ,npm install vue-router --save
2.导入vue-router
3.通过vue.user() 使用插件
4.创建路由对象
5.导入组件
6.配置URL和组件直接的映射关系
7.导出router对象
index.js
在vue-router中,通过mode参数来决定采用哪一种方式,默认hash。
history 注:如果浏览器不支持history新特性,则采用hash方式
import VueRouter from 'vue-router' import Vue from 'vue' import Home from
'../components/Home' import About from '../components/About' //
通过vue.use(插件),安装插件 Vue.use(VueRouter) // 创建路由对象 const routes = [{ path: "", //
重定项 redirect: '/home' }, { path: '/home', component: Home }, { path: '/about',
component: About }] const router = new VueRouter({ // 配置URL和组价直接的映射关系 routes,
// history模式 mode: 'history' }) // 将router对象传入到vue实例中 export default router
8.在main.js中导入router对象
import Vue from 'vue' import App from './App' import router from
'./router/index.js' Vue.config.productionTip = false /* eslint-disable no-new
*/ new Vue({ el: '#app', router, render: h => h(App) })
9.在index.html中使用
<template> <div id="app"> <h2>hello world</h2> <router-link
to='home'>首页</router-link> <router-link to='about'>关于</router-link>
<router-view></router-view> </div> </template>
router-view:当路由发生改变时,重新执行render函数。router-view会找到对应的组件进行渲染。
router-link:创建路由链接标签(默认a标签),绑定对应的事件,根据属性执行路由实例不通的方法。
效果: