Vue Router的入门以及简单使用
Vue Router 是Vue官方的路由管理器,是Vue用来实现SPA的插件。它和 Vue.js 的核心深度集成,让构建单页面应用(SPA)变得易如反掌。
基本概念:
路由:是一种映射关系,是 “path =>组件”的映射
路由器:管理路由的,在路由器里配置路由
1、npm 安装
npm install vue-router
2、相关API说明
(1)路由配置 :route是一条路由的实现, 它是一个对象,由两个部分组成: path和component. 或path和redirect。 path 指路径,component 指的是组件。多个route组成一个routes。
routes:[
{//一般路由
path:'/about',
component:About //组件
},
{//自动跳转路由
path:'/',
redirect:'/about'
}
]
(2)VueRouter():用于创建路由器的构造函数,接受routes参数
new Router({
//多个配置项
routes // routes: routes 的简写
})
(3)注册路由器:把router 实例注入到 vue 根实例中
import router from "./router"; //先引入路由
new Vue({
router,
...
})
(4)使用路由组件标签
①<router-link>:用来生成路由链接
<router-link to="./xxx">go to xxx</router-link>
②<router-view>:用来显示当前路由组件界面,匹配了哪个组件就会显示哪个组件的界面
<router-view></router-view>
执行过程:当用户点击 router-link 标签时,会去寻找它的 to 属性, 它的 to 属性和router/index.js 中配置的路径{ path: '/home', component: Home} path 一一对应,从而找到了匹配的组件, 最后把组件渲染到 <router-view> 标签所在的地方。所有的这些实现是基于hash 实现的。
3、简单使用
一:基本路由
基本目录结构:

注意:组件包括两种,路由组件和非路由组件,非路由组件放在components文件夹中,路由组件放在另一个文件夹中,通常命名为views或者pages
(1)定义路由器模块 router/index.js
/*
路由器模块
*/
import Vue from 'vue'
import VueRouter from 'vue-router'
//引入路由组件
import About from '../views/About.vue'
import Home from '../views/Home.vue' Vue.use(VueRouter) export default new VueRouter({//向外暴露一个路由器
//n个路由
routes:[
{
path:'/about',
component:About
},
{
path:'/home',
component:Home
},
{//当请求根路径时,重定向请求home
path:'/',
redirect:'/home'
}
]
})
(2)在入口的main.js中引入路由器,并进行配置
import Vue from 'vue'
import App from './App'
import router from './router' //index可以省略不写,引入路由器 new Vue({//配置对象的属性名都是一些默认的属性名,不能随便修改
el:'#app',
components:{
App
},
template:'<App/>',
router
})
(3)App.vue中使用路由组件标签 <router-link>和<router-view>
<template>
<div class="container">
<div class="row">
<div class="col-xs-offset-2 cool-xs-8">
<div class="page-header"><h2>Router Basic - 01 </h2></div>
</div>
</div> <div class="row">
<div class="col-xs-2 col-xs-offset-2">
<div class="list-group">
<router-link to="/home" class="list-group-item">Home</router-link>
<router-link to="/about" class="list-group-item">About</router-link>
</div>
</div>
<div class="col-xs-6">
<div class="panel">
<div class="panel-body">
<router-view></router-view>
</div>
</div>
</div>
</div>
</div> </template> <script> export default{ }
</script> <style>
.container{
width:1000px;
margin:0 auto;
}
.router-link-active{
color:red !important;
}
</style>
(4)编辑路由组件的内容
Home.vue
<template>
<div>
Home
</div>
</template> <script>
export default{ }
</script> <style> </style>
About.vue
<template>
<div>
About
</div>
</template> <script>
export default{ }
</script> <style> </style>
最终呈现结果:

二:嵌套路由:点击一个路由之后,打开的页面中仍然存在路由
效果图:

(1)修改路由器模块( router/index.js)中的routes配置
/*
路由器模块
*/
import Vue from 'vue'
import VueRouter from 'vue-router'
//引入路由组件
import About from '../views/About.vue'
import Home from '../views/Home.vue'
import Message from '../views/Message.vue'
import News from '../views/News.vue' Vue.use(VueRouter) export default new VueRouter({//向外暴露一个路由器
//n个路由
routes:[
{
path:'/about',
component:About
},
{
path:'/home',
component:Home,
children:[
{
path:'/home/message', //path最左侧的斜杠永远代表根路径
component:Message
},
{
path:'/home/news',
component:News
},
{
path:'/home',
redirect:'/home/news'
}
]
},
{//当请求根路径时,重定向请求home
path:'/',
redirect:'/home'
}
]
})
(2)Home组件中,使用路由组件标签 <router-link>和<router-view>
<template>
<div style="margin-left:10px;">
<h2>Home 组件</h2>
<div>
<ul class="nav nav-tabs">
<li><router-link to="/home/news">News</router-link></li>
<li><router-link to="/home/message">Message</router-link></li>
</ul>
<div>
<router-view></router-view>
<hr>
</div> </div>
</div>
</template> <script>
export default{ }
</script> <style> </style>
(3)编辑新增的News和Message路由组件的内容
News.vue
<template>
<div>
<ul>
<li v-for="(news,index) in newsArr" :key="index">
{{news}}
</li>
</ul> </div>
</template> <script>
export default{
data(){
return {
newsArr:['news1','news2','news3']
}
}
}
</script> <style> </style>
Message.vue
<template>
<ul>
<li v-for="(message,index) in messages" :key="message.id">
<a href="">{{message.title}}</a>
</li>
</ul>
</template>
<script>
export default{
data(){
return {
messages:[]
}
},
mounted(){
var that = this;
//模拟ajax请求从后台请求数据
setTimeout(()=>{
const messages = [
{id:1,title:'message1'},
{id:2,title:'message2'}
]
that.messages = messages;
},1000)
}
}
</script>
<style>
</style>
Vue Router的入门以及简单使用的更多相关文章
- vue router拦截器的简单使用
之前,为了实现router跳转的每个页面的url上都带上addressCode,然后用了一下router拦截器,很好用,当然也可以专门封装一个方法来实现(跳转的页面上带有addressCode),不过 ...
- Vue基础开发入门之简单语法知识梳理(思维导图详解)
基于个人写的以下关于Vue框架基础学习的三篇随笔,在此基础上,做一个阶段性的知识总结,以此来检验自己对Vue这一段时间学习的成果,内容不多,但很值得一看.(思维导图详解)
- Vue Router的简单了解
Vue Router Vue Router官方文档 传统Web项目开发往往采用超链接实现页面之间的切换和跳转.Vue开发的是单页面应用(Single Page Application,SPA),不能使 ...
- .NET Core实战项目之CMS 第六章 入门篇-Vue的快速入门及其使用
写在前面 上面文章我给大家介绍了Dapper这个ORM框架的简单使用,大伙会用了嘛!本来今天这篇文章是要讲Vue的快速入门的,原因是想在后面的文章中使用Vue进行这个CMS系统的后台管理界面的实现.但 ...
- 前端MVC Vue2学习总结(八)——Vue Router路由、Vuex状态管理、Element-UI
一.Vue Router路由 二.Vuex状态管理 三.Element-UI Element-UI是饿了么前端团队推出的一款基于Vue.js 2.0 的桌面端UI框架,手机端有对应框架是 Mint U ...
- python 全栈开发,Day91(Vue实例的生命周期,组件间通信之中央事件总线bus,Vue Router,vue-cli 工具)
昨日内容回顾 0. 组件注意事项!!! data属性必须是一个函数! 1. 注册全局组件 Vue.component('组件名',{ template: `` }) var app = new Vue ...
- vue router 几种方式对比 (转载)
<div id="app"> <h1>Hello App!</h1> <p> <!-- 使用 router-link 组件来导 ...
- 04慕课网《vue.js2.5入门》——Vue-cli开发todolist
主要文件目录: 文件代码: 根实例,初始化vue: <!--index.html,网站入口页面,和main.jsp组成一套.vue文件,包含--> <!DOCTYPE html> ...
- vue router使用query和params传参的使用
传参是前端经常需要用的一个操作,很多场景都会需要用到上个页面的参数,本文将会详细介绍vue router 是如何进行传参的,以及一些小细节问题.有需要的朋友可以做一下参考,希望可以帮到大家. Vue ...
随机推荐
- 浅谈maven中的scope,systempath
scope maven中scope的默认值是compile scope的分类 1)compile 默认是compile.compile表示被依赖项目需要参与当前项目的编译,包括后续的测试,运行周期也 ...
- Python 3.0 写日志时出现乱码
问题描述 python 3.0启用日志, 在pycharm里打开.log文件时中文都显示乱码. 根本原因 默认日志编译用的是GBK, 而python 3.0写程序用的是UTF-8. 所以.log文件中 ...
- Java中对文件的序列化和反序列化
public class ObjectSaver { public static void main(String[] args) throws Exception { /*其中的 D:\\objec ...
- [C++] 用Xcode来写C++程序[6] Name visibility
用Xcode来写C++程序[6] Name visibility 此小结包括了命名空间的一些使用细节 命名空间 #include <iostream> using namespace st ...
- C#(.NET)面试题:做一个能自定义输入命令的表格程序
目前为止,已经面试 5 10 家了... 这个试题面试的公司是某一上市公司. 试题是英文的(后面给出翻译): you're given a task of writing a simple pro ...
- SMTP服务器设置
Web.config中使用如下配置 <system.net> <mailSettings> <smtp from="info@site.c ...
- [Codeforces 321D][2018HN省队集训D4T2] Ciel and Flipboard
[Codeforces 321D][2018HN省队集训D4T2] Ciel and Flipboard 题意 给定一个 \(n\times n\) 的矩阵 \(A\), (\(n\) 为奇数) , ...
- SAP 前端技术的演化史简介
Jerry之前曾经写过一篇微信公众号文章,题目叫<> 关注我的公号"汪子熙"后,在历史菜单"前端开发相关"里即可找到这篇文章: 该文章简单回顾了SA ...
- log4j.properties的配置详解
log4j.rootLogger=ERROR,A1log4j.appender.A1=org.apache.log4j.ConsoleAppenderlog4j.appender.A1.layout= ...
- React Native 基础报错及解决方案记录
刚开始上手RN,碰到很多坑,记录一下.碰到问题多去看看github上面的issue! 启动命令react-native run-ios报错 1.:xcrun: error: unable to fin ...