Vue学习笔记进阶篇之vue-router安装及使用方法

时间:2021-05-26

介绍

vue-router是Vue.js官方的路由插件,它和vue.js是深度集成的,适合用于构建单页面应用。vue的单页面应用是基于路由和组件的,路由用于设定访问路径,并将路径和组件映射起来。传统的页面应用,是用一些超链接来实现页面切换和跳转的。在vue-router单页面应用中,则是路径之间的切换,也就是组件的切换。

本文是基于上一篇文章(Vue学习笔记进阶篇——vue-cli安装及介绍)vue-cli脚手架工具的。

安装

在终端通过cd命令进入到上一篇文章中创建的my-demo1项目目录里,然后使用以下命令进行安装:

npm install vue-router --save

--save参数的作用是在我们的包配置文件package.json文件中添加对应的配置。安装成功后, 可以查看package.json文件,你会发现多了"vue-router": "^2.7.0"的配置。如下:

"dependencies": { "vue": "^2.3.3", "vue-router": "^2.7.0" },

使用

通过以上步骤,我们已经安装好了vue-router,但是在vue-cli中我们如何使用呢?
首先,我们需要在main.js文件中导入并注册vue-router:

//ES6语法导入import VueRouter from 'vue-router'//注册Vue.use(VueRouter)

然后便是实例化:

const router = new VueRouter({ mode: 'history', routes:[ {path: '/', component:DemoHome}, {path: '/about', component:DemoAbout}, {path: '/contact', component:DemoContact} ]})

path: 是路由的路径。

component: 是该路由需要渲染的组件。

上面代码中的DemoHome, DemoAbout, DemoContact都是单文件组件,所以我们同样需要创建上面三个组件,并导入到当前文件。这三个组件我们只是作为示例来使用,所以比较简单,代码分别如下:

DemoHome.vue:

<template> <div id="home"> <h2>this is home</h2> </div></template><script> export default({ name:'home' })</script><style scoped> #home{ width: 100%; height: 500px; background-color: khaki; }</style>

DemoAbout.vue:

<template> <div id="about"> <h2>this is about</h2> </div></template><script> export default({ name:'about' })</script><style scoped>#about{ width: 100%; height: 500px; background-color: antiquewhite;}</style>

DemoContact.vue:

<template> <div id="contact"> <h2>this is contact</h2> </div></template><script> export default({ name:'contact' })</script><style scoped> #contact{ width: 100%; height: 500px; background-color: lightskyblue; }</style>

创建好以上组件后,再使用ES6语法导入到main.js:

import DemoHome from './components/DemoHome'import DemoAbout from './components/DemoAbout'import DemoContact from './components/DemoContact'

最后在Vue实例中加入路由属性就可以了

new Vue({ el: '#app', router, template: '<App/>', components: { App }})

完整的main.js应该是这样:

import Vue from 'vue'import VueRouter from 'vue-router'import App from './App'import DemoHome from './components/DemoHome'import DemoAbout from './components/DemoAbout'import DemoContact from './components/DemoContact'Vue.use(VueRouter)Vue.config.productionTip = falseconst router = new VueRouter({ mode: 'history', routes:[ {path: '/', component:DemoHome}, {path: '/about', component:DemoAbout}, {path: '/contact', component:DemoContact} ]})new Vue({ el: '#app', router, template: '<App/>', components: { App }})

在这里我们为了学习,所以我们简单的做个布局。接下来,我会再创建两个组件,一个叫DemoHeader, 一个叫DemoFooter。DemoHeader里面我放一个logo的图片,和导航,而这个导航的路由也将会使用我们前面定义的路由;DemoFooter就比较简单了,放一些footer信息。

下面我们看下这两个组件的代码:

DemoHeader.vue:

<template> <div id="header" class="wrap"> <div class="header"> <h1 class="logo"> <router-link to="/"> ![](../assets/logo.png) </router-link> </h1> </div> <div class="top-nav"> <div id="navList" class="navlist-wrap"> <div class="navlist clearfix"> <span class="nav-btn"> <router-link to="/">首页</router-link> </span> <span class="nav-btn"> <router-link to="/about">关于</router-link> </span> <span class="nav-btn"> <router-link to="/contact">联系方式</router-link> </span> </div> </div> </div> </div></template><script> export default({ name:'header', data:function () { return { 'nav-btn': 'nav-btn' } } })</script><style scoped> .header{width:1105px;margin:0 auto;height:111px;padding:12px 0 18px;position:relative;*z-index:1} .header .logo{height:86px;width:256px;margin-top:25px} .top-nav .navlist-wrap{width:1050px;margin:0 auto;position:relative} .top-nav .navlist{position:absolute;right:130PX;top:-40PX} .top-nav .navlist .nav-btn { float:left; margin-left:60px; color:#666; vertical-align: middle; text-decoration:none; font-size: large; }</style>

在上面的代码中,我们看到了一个陌生的标签,<router-link>这个是什么玩意呢?其实他就是vue-router集成的一个组件,渲染出来的是一个<a>标签。而他的属性to其实就是一个props属性,这里面的意思就是路由的路径,与前面定义的路由path对应。关于router-link的更多介绍可以看官网router-link API文档

DemoFooter.vue:

<template> <div id="footer"> <span>Copyright © <a href="http://ponents: { DemoHeader, DemoFooter }

在上面的代码中我们又发现了个陌生标签<router-view>这个标签同样是vue-router的一个内部组件,实际上它是一个是一个 functional 组件。具体信息可以去官网router-viewAPI文档详细了解。它的作用就是渲染路由导航过来的组件,也就是这个标签内就是我们放置DemoHome, DemoAbout, DemoContact的地方。

因为它也是个组件,所以可以配合 <transition> 和 <keep-alive> 使用。如果两个结合一起用,要确保在内层使用 <keep-alive>, 添加上述两个标签后的template代码如下:

<template> <div id="app"> <demo-header></demo-header> <transition name="fade" mode="out-in"> <keep-alive> <router-view></router-view> </keep-alive> </transition> <demo-footer></demo-footer> </div></template>

再添加一个简单的淡入淡出的样式:

.fade-enter-active, .fade-leave-active{ transition: all .3s;}.fade-enter, .fade-leave-to{ opacity: 0;}

通过上面的代码,我们发现之前学过的过渡这里都可以使用,可参考Vue学习笔记进阶篇——单元素过度

最后我们看下我们做了半天的成果吧:

首页

关于

联系方式

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章