【vue-07】vue-router
安装
vue-router是一个插件包,所以我们还是需要用npm 来进行安装。打开命令行工具,进入你的项目目录,输入下面命令。
npm install vue-router --save
如果在一个模块化工程中使用它。必须通过Vue.use()
明确地安装路由功能
注意:先引入vue,再引入vue-router
import Vue from 'vue'
import VueRouter from 'vue-router'
//显示声明使用VueRouter
Vue.use(VueRouter);
起步
创建一个vue-cli程序 参考链接
安装 参考安装标题
新建组件
src/components/Content.vue和Main.vue
Content.vue
<template>
<h1>内容</h1>
</template>
<script>
export default {
name: "Content"
}
</script>
<style scoped>
</style>
Main.vue
<template>
<h1>主页</h1>
</template>
<script>
export default {
name: "Main"
}
</script>
<style scoped>
</style>
- 在src下新建一个文件router/index.js,进行创建路由和路由映射
默认情况下,我们进入一个网站的首页,应该先把首页渲染。所以可以设置默认路径。
import Vue from 'vue'
import VueRouter from 'vue-router'
//导入上面自定义的组件
import Content from "../components/Content";
import Main from "../components/Main";
//安装路由
Vue.use(VueRouter);
//配置导出路由
export default new VueRouter({
routes:[
{
path: '/',
redirect: '/main'
},
{
//路由路径
path:'/content',
//跳转的组件
component:Content
},
{
//路由路径
path:'/main',
//跳转的组件
component:Main
},
]
});
- 在main.js中挂载路由
import Vue from 'vue'
import App from './App.vue'
import router from "./router"
Vue.config.productionTip = false
new Vue({
render: h => h(App),
router
}).$mount('#app')
- 在App.vue中使用路由
<template>
<div id="app">
<img src="./assets/logo.png">
<HelloWorld/>
<router-link to="/main">首页</router-link>
<router-link to="/content">内容</router-link>
<router-view></router-view>
</div>
</template>
....
- 实现效果
我们会发现URL中存在#,那怎么消除呢?
localhost:8081/#/content
使用HTML5的history模式
默认情况下,路径的改变使用的是URL的hash,如果我们希望使用H5的History模式,可以进行如下配置mode: 'history'
。
import Vue from 'vue'
import VueRouter from 'vue-router'
//导入上面自定义的组件
import Content from "../components/Content";
import Main from "../components/Main";
//安装路由
Vue.use(VueRouter);
//配置导出路由
export default new VueRouter({
routes:[
{
path: '/',
redirect: '/main'
},
{
//路由路径
path:'/content',
//跳转的组件
component:Content
},
{
//路由路径
path:'/main',
//跳转的组件
component:Main
},
],
mode: 'history'
});
编程式导航
我们使用router-link
的to属性,可以直接跳转路由。但是有时候,我们需要执行一段js代码之后,再跳转路由,这时候可以使用编程式导航。
语法:
This.$router.push(‘路由url’)
<button @click="toHome">首页</button>
methods: {
toHome() {
console.log('我将要跳转到首页')
this.$router.push('/home')
}
}
嵌套路由
比如在home页中,我们希望通过/home/news这种形式访问一些组件,在home页的基础上,进行组件展示,这时候可以使用嵌套路由。
实现嵌套路由有两个步骤。
创建对应的子组件,并且在路由配置中配置子路由。
在组件内部使用
<router-view>
组件
const routes = [
{
path: '/',
redirect: '/home'
},
{
path: '/home',
component: Home,
children: [ // 配置子路由
//默认路径news
{
path: '',
redirect:'news'
},
{
path: 'news',
component: () => import('../components/news')
},
{
path: 'message',
component: () => import('../components/message')
}
]
},
{
path: '/about',
component: () => import('../components/about')
}
路由传参
路由跳转可进行参数传递,参数传递分为两种:params
和query
params
的类型:
配置路由格式:/news/:id
这样就表示,我们在使用news的时候,需要在后面携带id参数。传递参数后的url:/news/123
query
类型
配置路由方式不变。在路由后面使用?key=value传递
router-link传参
```vue
新闻2
```
JS代码传参
toNews() {
this.$router.push({
path: '/home/news',
query: {name: '李四', age: 24}
})
}
获取参数
获取参数通过$route对象获取的。
created() {
console.log(this.$route.params)
console.log(this.$route.query)
}
router和route
$route
和$router
是有区别的
$router
是VueRouter实例,想要导航到不同的URL,使用$router.push
方法。
$route
是当前router的跳转对象,可以获取name、path等等
【vue-07】vue-router的更多相关文章
- 【前端面试】Vue面试题总结(持续更新中)
Vue面试题总结(持续更新中) 题目参考链接 https://blog.csdn.net/weixin_45257157/article/details/106215158 由于已经有很多前辈深造VU ...
- 【非专业前端】Vue UI 之 建立Vuetify工程
先建立一个工程[Webpack] .. ..建立好之后,进入目录,添加vuetify插件 cd vuetify-demo vue add vuetify[会出错] npm install vuetif ...
- 【转存】Vue组件选项props
原帖地址 前面的话 组件接受的选项大部分与Vue实例一样,而选项props是组件中非常重要的一个选项.在 Vue 中,父子组件的关系可以总结为 props down, events up.父组件通过 ...
- 【vue开发】vue导出Excel表格教程&demo
前端工作量最多的就是需求,需求就是一直在变,比如当前端数据写完之后,需要用Excel把数据下载出来:再比如前端在没有数据库想写些demo玩时,也是很好的选择. 第一步安装依赖包,修改配置 1.装依赖: ...
- 【Vue课堂】Vue.js 父子组件之间通信的十种方式
这篇文章介绍了Vue.js 父子组件之间通信的十种方式,不管是初学者还是已经在用 Vue 的开发者都会有所收获.无可否认,现在无论大厂还是小厂都已经用上了 Vue.js 框架,简单易上手不说,教程详尽 ...
- 8.【nuxt起步】-vue组件之间数据交互
那么现在问题来了,我现在是在index.vue获取了服务端的数据,怎么传值到maincontent.vue?当然你也可以把获取数据放在maincontent.vue,但假如有些数据同时在header, ...
- 【15.07%】【codeforces 625A】Guest From the Past
time limit per test 1 second memory limit per test 256 megabytes input standard input output standar ...
- 【42.07%】【codeforces 558A】Lala Land and Apple Trees
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- 【17.07%】【codeforces 583D】Once Again...
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- 【07】 vue 之 Vue-router
注意: vue-router@2.x 只适用于 Vue 2.x 版本. vue-router@1.x 对应于Vue1.x版本. 的Github地址:vue-router 文档地址 7.2. vue-r ...
随机推荐
- 你只会用 map.put?试试 Java 8 compute ,操作 Map 更轻松!
今天栈长分享一个实用的 Java 8 开发技能,那就是 Map 接口中增加的 compute 方法,给 Map 集合计算更新用的. compute简介 如下所示,Java 8 在 Map 和 Conc ...
- C# 获取网页信息
获取网页源码 ///通过HttpWebResponse public string GetUrlHtml(string url) { string strHtml = string.Empty; Ht ...
- Java系列教程-SpringMVC教程
SpringMVC教程 1.SpringMVC概述 1.回顾MVC 1.什么是MVC MVC是模型(Model).视图(View).控制器(Controller)的简写,是一种软件设计规范. 是将业务 ...
- 前端开发:基于cypress的自动化实践
作为一个伪开发,在一个平台项目中负责前端的开发工作,开发框架为vue,本文我会站在前端开发的角度介绍,我是如何使用cypress的. [x] 如何在vue中使用cypress [x] 如何运行cypr ...
- MongoDB数据库的使用
MongoDB是一个基于分布式 文件存储的NoSQL数据库,适合存储JSON风格文件的形式. 三元素:数据库.集合和文档. 文档:对应着关系数据库中的行,就是一个对象,由键值对构成,是json的扩展B ...
- java例题_23 递归求年龄
1 /*23 [程序 23 求岁数] 2 题目:有 5 个人坐在一起,问第五个人多少岁,他说比第 4 个人大 2 岁.问第 4 个人岁数,他说比第 3 个 3 人大 2 岁.问第三个人,又说比第 2 ...
- MongoDB中“$”操作符表达式汇总
MongoDB中"$"操作符表达式汇总 查询 比较操作 $eq 语法:{ : { $eq: } } 释义:匹配等于(=)指定值的文档 举例: 查询age = 20的文档: db.p ...
- APP | edxposed框架+trustmealredy模块抓包小程序
出品|MS08067实验室(www.ms08067.com) 本文作者:ketchup(Ms08067实验室 SRSP TEAM小组成员) 一.下载edxposed框架,由于安卓5.0版本以下的不支持 ...
- [BFS]A. 【例题1】走迷宫
A . [ 例 题 1 ] 走 迷 宫 解析 简单的BFS模板题 Code #include <bits/stdc++.h> #define N 1005 using namespace ...
- 死磕Spring之AOP篇 - Spring AOP两种代理对象的拦截处理
该系列文章是本人在学习 Spring 的过程中总结下来的,里面涉及到相关源码,可能对读者不太友好,请结合我的源码注释 Spring 源码分析 GitHub 地址 进行阅读. Spring 版本:5.1 ...