网站首页 > 教程文章 正文
在vue2中 vue-router 通过插件的形式进行工作,回顾下具体操作步骤如下:
在router文件中
// 在router文件中引入
import Router from 'vue-router'
// 使用Router
Vue.use(Router)
let routes = [
{
path: '/',
name: 'HelloWorld',
component: HelloWorld
},
{
path: '/test',
name: 'test',
component: test
}
];
// 导出Router对象
export default new Router({
routes: routes,
mode: 'hash',
})
在main.js中使用router对象
import router from './router'
new Vue({
el: '#app',
router,
components: { App },
template: '<App/>'
})
经过以上步骤设置就能正常使用router了
那么如何实现一个简单的hash模式的vue-router插件呢?
- 能够在项目初始化的时候进行组件注册
- 能够支持 router-link 和 router-view
- 能够在template使用 $router 对象
- ····
1、新建 my-router 文件夹 新建index.js;
2、把router.js文件导入vue-router替换为自己的创建的my-router;
import Router from 'vue-router'
替换成
import Router from './../my-router/index'
3、实现自己的my-router
let Vue;
// 创建Router对象
class Router {
constructor(options){
// 把options 绑定在Vue实例上
this.$options = options;
// 初始化获取hash路由数据
let initial = window.location.hash.slice(1) || '/';
// 设置current为响应式
Vue.util.defineReactive(this,'current',initial);
// 监听hash变化 改变current
window.onhashchange = () =>{
this.current = window.location.hash.slice(1)
}
}
}
// 在mian.js 中的 Vue.use()其实调用的就是绑定的插件的install 方法
Router.install = function(_Vue){
// 保存vue在Router对象中
Vue = _Vue;
// 使用mixin将调用时机延迟至数据渲染
Vue.mixin({
beforeCreate(){
// 此处是为了在页面中可以正常使用$router对象
if(this.$options.router){
this.prototype.$router = this.$options.router
}
}
})
// 实现router-link功能(目前只考虑hash模式)
Vue.component('router-link',{
props:{
// <router-link to='test'> testComp </router-link>
// 获取router-link 上传递的属性
to:{
type:String
}
},
render(h){
return h('a',{
attrs:{
href:'#'+this.to
},this.$slots.default
})
}
})
Vue.component('router-view',{
render(h){
let router = this.$router.$options.routes.find(item => item.path === this.$options.current)
let component = router ? router.component :null;
return h(component);
}
})
}
export default Router
试了试···
还行···
猜你喜欢
- 2025-05-24 在vue3中动态加载远程组件
- 2025-05-24 Motion for Vue:为Vue量身定制的强大动画库
- 2025-05-24 前端流行框架Vue3教程:14. 组件传递Props效验
- 2025-05-24 高性能 vue.js+ztree 树形组件Vue-GiantTree
- 2025-05-24 vue浏览器不可不看的宝藏插件
- 2025-05-24 重磅推荐:一个基于 Vue 的 (大转盘/九宫格) 抽奖插件
- 2025-05-24 Vue父子组件,利用条件延迟创建子组件,达到参数传递目的
- 2025-05-24 最近很火的Vue Vine是如何实现一个文件中写多个组件
- 2025-05-24 超好用 Vue.js 图片裁切组件Vue-ImgCutter
- 2025-05-24 Vue 中 强制组件重新渲染的正确方法
- 05-25干货 | 一步步部署 Flask 应用
- 05-25别再去找Docker命令了,你要的常用的全都在这
- 05-25如果您删除Windows11上的“Program Files”文件夹会发生什么?
- 05-25家用nas最常用的docker容器及部署方法
- 05-25你好 dotnet run file, 再见 csproj
- 05-25China committed to continuing contributions to global health: delegation
- 05-25Chinese, German experts urge cooperation during Eurasia relations seminar
- 05-25Peace of paramount importance for region
- 最近发表
-
- 干货 | 一步步部署 Flask 应用
- 别再去找Docker命令了,你要的常用的全都在这
- 如果您删除Windows11上的“Program Files”文件夹会发生什么?
- 家用nas最常用的docker容器及部署方法
- 你好 dotnet run file, 再见 csproj
- China committed to continuing contributions to global health: delegation
- Chinese, German experts urge cooperation during Eurasia relations seminar
- Peace of paramount importance for region
- after和in用法解析
- China's top diplomat to chair third China-Pacific Island countries foreign ministers' meeting
- 标签列表
-
- location.href (44)
- document.ready (36)
- git checkout -b (34)
- 跃点数 (35)
- 阿里云镜像地址 (33)
- qt qmessagebox (36)
- mybatis plus page (35)
- vue @scroll (38)
- 堆栈区别 (33)
- 什么是容器 (33)
- sha1 md5 (33)
- navicat导出数据 (34)
- 阿里云acp考试 (33)
- 阿里云 nacos (34)
- redhat官网下载镜像 (36)
- srs服务器 (33)
- pico开发者 (33)
- https的端口号 (34)
- vscode更改主题 (35)
- 阿里云资源池 (34)
- os.path.join (33)
- redis aof rdb 区别 (33)
- 302跳转 (33)
- http method (35)
- js array splice (33)