介绍

路由:控制组件之间的跳转,不会实现请求、不用页面刷新,直接跳转-切换组件》》》

安装

本地环境安装路由插件vue-router:    cnpm install vue-router --save-dev

*没有安装淘宝镜像的可以将 cnpm 替换成 npm

想要安装的可以看这篇文章http://www.cnblogs.com/padding1015/p/7162024.html,(打开搜索  镜像  即可跳转到对应位置)

配置

两种配置方法:在main.js中 || 在src/router文件夹下的index.js中

这里只说在src/router/index.js中的

  1. 引入:
import Vue from 'vue'

import Router from 'vue-router'  

注意这个Router是自定义的名字,这里叫这个名字后,下边都要用到的

  2. 使用/注册:

 Vue.use(Router)

  3. 配置

配置路由:

export default new Router({
routes: [
{
path : ‘/’, //到时候地址栏会显示的路径
name : ‘Home’,
component : Home // Home是组件的名字,这个路由对应跳转到的组件。。注意component没有加“s”.
},
{
path : ‘/content’,
name : ‘Content’,
component : Content
}
],
mode: "history"
})

  4. 引入路由对应的组件地址:

import Home from '@/components/Home'

import Home from '@/components/Content’

  5. 在main.js中调用index.js的配置:

import router from './router'

  6. App.vue页面使用(展示)路由:<!-- 展示router -->

把这个标签放到对应位置:

<router-view></router-view>

  7. 路由切换(原来的<a href=”XXX.html”>等地方):把切换标签和链接改成:

<router-link  to="/">切换到Home组件</router-link>

<router-link  to="/content">切换到Content组件</router-link>

//这里,to里边的参数和配置时,path的路径一样即可

贴一个源码:

main.js

 // The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
import VueResource from 'vue-resource'//从node_modules里边把vue-resource引入进来,同引入vue文件和引入vue-router一个道理 Vue.config.productionTip = false;
Vue.use(VueResource) //这样以后,就可以在任何组件页面中使用http了
/* eslint-disable no-new */
new Vue({
el: '#app',
router,//引用router
template: '<App/>',
components: { App }
})

main.js

src/router/index.js

 import Vue from 'vue'
import Router from 'vue-router'
import Home from '@/components/Home'
import Content from '@/components/Content'
import About from '@/components/About'
import Submit from '@/components/Submit' Vue.use(Router) export default new Router({
routes: [
{
path: '/',
name: 'Home',
component: Home
},
{
path: '/content',
name: 'Content',
component: Content
},
{
path: '/about',
name: 'About',
component: About
},
{
path: '/submit',
name: 'Submit',
component: Submit
}
],
mode: "history"//干掉地址栏里边的#号键
})

src/router/index.js(router的主要配置文件)

App.vue 展示Vue

 <template>
<div id="app">
<app-header></app-header>
<app-nav></app-nav>
<!-- 展示router -->
<router-view></router-view>
<app-footer></app-footer>
</div>
</template> <script>
import Header from './components/Header'
import Footer from './components/Footer'
import Navbar from './components/Navbar'
export default {
name: 'app',
data () {
return { }
},
components: {//局部注册组件这里,可能会定义多个组件,所以component这个单词加上“s”
"app-header": Header,
"app-footer": Footer,
'app-nav': Navbar
}
}
</script> <style> </style>

App.vue 的router-view 标签

导航页面的路由链接设置,用于切换组件

 <template>
<nav class="app-nav">
<ul class="ul-father">
<li class="li-father" v-for="item in arr" v-on:mouseover="item.show = ! item.show" v-on:mouseout="item.show = ! item.show" v-bind:class="{bgchange: item.show}">
<!-- 路由切换组件 -->
<router-link v-bind:to="item.url">
{{ item.title }}
</router-link>
<template v-if="item.value">
<ul class="ul-child" v-show="item.show">
<li class="li-child" v-for="x in item.value">
<a href="javascript:;">
{{ x }}
</a>
</li>
</ul>
</template>
</li>
</ul>
</nav>
</template>
<script>
export default {
name: "app-nav",
data (){
return {
arr: [
{
title: "首页",
value: ["一","二","三","四"],
url: "/",
show: false
},
{
title: "新闻" ,
value: ["二","二","三","四"],
url: "/content",
show: false
},
{
title: "关于",
url: "/about"
},
{
title: "投稿",
url: "/submit"
}
]
}
}
}
</script>
<style scoped>
.app-nav{
margin-bottom: 20px;
}
ul.ul-father {
background: Lightgreen;
margin: 0;
}
.li-father {
position: relative;
display: inline-block;
width: 20%;
margin: 0;
}
li a {
display: block;
padding: 15px 0;
color: #333;
text-decoration: none;
}
li a:hover,.bgchange>a{
color: #fff;
background: #222;
}
.ul-child{
position: absolute;
top: 51px;
left: 0;
width: 100%;
background: Lightgreen;
}
</style>

NavBar.Vue页面,主要用于页面切换的导航组件

声明:

  请尊重博客园原创精神,转载或使用图片请注明:

  博主:xing.org1^

  出处:http://www.cnblogs.com/padding1015/

Vue-路由配置和使用步骤整理的更多相关文章

  1. vue路由配置,vue子路由配置

    上一篇关于vue环境配置已经写好了!按照操作就行了! 现在一个项目已经部署完成,接下来我们从路由开始! 还记得在初始化项目的时候,有提示是否需要安装vue-router,对没错,vue中路由全靠它! ...

  2. npm vue路由配置

    npm vue路由 复习:1.README.md文件:保存如何使用的命令 (1)     npm install:拷项目时可以不拷node_modules文件,运行该命令时,会自动下载node_mod ...

  3. vue 路由配置

    1.不带参数的路由配置 及 跳转 //路由配置: { name: "a", path: "/a", component: a }   页面跳转: this.$r ...

  4. 【原创】一篇学会vue路由配置 、 动态路由 、多层路由(实例)

    先来看看效果图: 为了方便讲解,我没有使用vue脚手架,如果需要的,可以留言跟我要.不多说开工: 首先,html先组上 <div id="app"> <div&g ...

  5. Vue 路由配置、动态路由

    1.安装 npm install vue-router --save / cnpm install vue-router --save 2.引入并 Vue.use(VueRouter) (main.j ...

  6. vue路由配置

    1.安装 npm install vue-router --save / cnpm install vue-router --save 2.引入并 Vue.use(VueRouter) (main.j ...

  7. Vue路由配置history模式

    我的博客: https://github.com/Daotin/fe-notes/issues vue需要node.js吗? 你可以用 script 标签的形式引入vue.min.js 这样的,不需要 ...

  8. Vue 路由模块化配置

    博客地址:https://ainyi.com/77 企业运营后台页面很多,路由如若不区分模块化配置,所有路由挤在同一个文件将不好维护,所以路由的配置也要模块化 分享两个解决方案 -- Vue 路由配置 ...

  9. vue 路由 以及默认路由跳转

    https://router.vuejs.org/ vue路由配置: 1.安装 npm install vue-router --save / cnpm install vue-router --sa ...

随机推荐

  1. 安装php的memcached模块和扩展支持sasl

    memcached的1.2.4及以上增加了CAS(Check and Set)协议,对于同一key的多进行程的并发处理问题.这种情况其实根数据库很像,如果同时有几个进程对同一个表的同一数据进行更新的话 ...

  2. C. Vasya and String

    原题链接 C. Vasya and String High school student Vasya got a string of length n as a birthday present. T ...

  3. 《python机器学习—预测分析核心算法》笔记1

    参见原书 1.1-1.4节 一.惩罚线性回归模型 基本特性: 1.训练时间快,使用训练好的模型进行预测的时间也快2.应用于高速交易.互联网广告的植入等3.解决回归.分类问题 最重要的特性:能明确指出, ...

  4. [Essay] Apache Flink:十分可靠,一分不差

    Apache Flink:十分可靠,一分不差 Apache Flink 的提出背景 我们先从较高的抽象层次上总结当前数据处理方面主要遇到的数据集类型(types of datasets)以及在处理数据 ...

  5. Python基础学习参考(七):字典和集合

    一.字典 字典跟列表一样是一组数据的集合.它的特点是什么呢? 特点一:字典具有键(key)和值(value),其中键必须是唯一的,不可重复的,即键必须可以哈希的.对于值没有要求. 特点二:字典是无序的 ...

  6. 关于 frame的一些基本知识

    关于 frame的一些基本知识只是摘抄了一部分,供初学者参考. b.帧速率: 帧速率是每秒显示的图像数.标准影片(NTSC) 是29.97 帧第秒 (fps),电影是每秒24 帧fps.欧洲标准是(P ...

  7. 使用mongoVUE删除大量数据的情况下失效问题

    昨天有一个系统出现了问题,导致半夜时大量的错误数据产生,早晨一早接到上边通知让把这些数据尽数删除. 不可否认在数据操作时mongoVUE更加直观,因此一般情况下我也都是使用这个工具,但是今天却出现了问 ...

  8. linux下编译sphinx拓展

    编译libsphinxclient sphinx 源码包里的api文件夹下的libsphinxclient cd /root/api/libsphinxclient/ ./configure make ...

  9. 【mysql】mysql基本操作

    mysql基本操作 1.mysql表复制 mysql 表结构的复制 create table t2 like t2 mysql 表数据的复制 insert into t2 select * from ...

  10. R语言︱构造新序列

    1.数值构造函数rep与seq #数值构造rep与seq rep(1:4,each=2)#依次重复1:4两遍 rep(1:4,2) #注意,重复1:4两遍 seq(from=3,to=5,by=0.2 ...