介绍

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

安装

本地环境安装路由插件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. 引入:
1
2
3
import Vue from 'vue'
 
import Router from 'vue-router'

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

  2. 使用/注册:

1
Vue.use(Router)

  3. 配置

配置路由:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
export default new Router({
  routes: [
   {
        path : ‘/’,  //到时候地址栏会显示的路径
        name : ‘Home’,
        component :  Home   // Home是组件的名字,这个路由对应跳转到的组件。。注意component没有加“s”.
    },
    {
        path : ‘/content’,
        name : ‘Content’,
        component :  Content
    }
],
    mode: "history"
})

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

1
2
3
import Home from '@/components/Home'
 
import Home from '@/components/Content’

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

1
import router from './router'

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

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

1
<router-view></router-view>

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

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

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

贴一个源码:

贴一个源码:

main.js

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

src/router/index.js

 1 import Vue from 'vue'
2 import Router from 'vue-router'
3 import Home from '@/components/Home'
4 import Content from '@/components/Content'
5 import About from '@/components/About'
6 import Submit from '@/components/Submit'
7
8 Vue.use(Router)
9
10 export default new Router({
11 routes: [
12 {
13 path: '/',
14 name: 'Home',
15 component: Home
16 },
17 {
18 path: '/content',
19 name: 'Content',
20 component: Content
21 },
22 {
23 path: '/about',
24 name: 'About',
25 component: About
26 },
27 {
28 path: '/submit',
29 name: 'Submit',
30 component: Submit
31 }
32 ],
33 mode: "history"//干掉地址栏里边的#号键
34 })

App.vue 展示Vue

 1 <template>
2 <div id="app">
3 <app-header></app-header>
4 <app-nav></app-nav>
5 <!-- 展示router -->
6 <router-view></router-view>
7 <app-footer></app-footer>
8 </div>
9 </template>
10
11 <script>
12 import Header from './components/Header'
13 import Footer from './components/Footer'
14 import Navbar from './components/Navbar'
15 export default {
16 name: 'app',
17 data () {
18 return {
19
20 }
21 },
22 components: {//局部注册组件这里,可能会定义多个组件,所以component这个单词加上“s”
23 "app-header": Header,
24 "app-footer": Footer,
25 'app-nav': Navbar
26 }
27 }
28 </script>
29
30 <style>
31
32 </style>

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

 1 <template>
2 <nav class="app-nav">
3 <ul class="ul-father">
4 <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}">
5 <!-- 路由切换组件 -->
6 <router-link v-bind:to="item.url">
7 {{ item.title }}
8 </router-link>
9 <template v-if="item.value">
10 <ul class="ul-child" v-show="item.show">
11 <li class="li-child" v-for="x in item.value">
12 <a href="javascript:;">
13 {{ x }}
14 </a>
15 </li>
16 </ul>
17 </template>
18 </li>
19 </ul>
20 </nav>
21 </template>
22 <script>
23 export default {
24 name: "app-nav",
25 data (){
26 return {
27 arr: [
28 {
29 title: "首页",
30 value: ["一","二","三","四"],
31 url: "/",
32 show: false
33 },
34 {
35 title: "新闻" ,
36 value: ["二","二","三","四"],
37 url: "/content",
38 show: false
39 },
40 {
41 title: "关于",
42 url: "/about"
43 },
44 {
45 title: "投稿",
46 url: "/submit"
47 }
48 ]
49 }
50 }
51 }
52 </script>
53 <style scoped>
54 .app-nav{
55 margin-bottom: 20px;
56 }
57 ul.ul-father {
58 background: Lightgreen;
59 margin: 0;
60 }
61 .li-father {
62 position: relative;
63 display: inline-block;
64 width: 20%;
65 margin: 0;
66 }
67 li a {
68 display: block;
69 padding: 15px 0;
70 color: #333;
71 text-decoration: none;
72 }
73 li a:hover,.bgchange>a{
74 color: #fff;
75 background: #222;
76 }
77 .ul-child{
78 position: absolute;
79 top: 51px;
80 left: 0;
81 width: 100%;
82 background: Lightgreen;
83 }
84 </style>
 1 <template>
2 <nav class="app-nav">
3 <ul class="ul-father">
4 <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}">
5 <!-- 路由切换组件 -->
6 <router-link v-bind:to="item.url">
7 {{ item.title }}
8 </router-link>
9 <template v-if="item.value">
10 <ul class="ul-child" v-show="item.show">
11 <li class="li-child" v-for="x in item.value">
12 <a href="javascript:;">
13 {{ x }}
14 </a>
15 </li>
16 </ul>
17 </template>
18 </li>
19 </ul>
20 </nav>
21 </template>
22 <script>
23 export default {
24 name: "app-nav",
25 data (){
26 return {
27 arr: [
28 {
29 title: "首页",
30 value: ["一","二","三","四"],
31 url: "/",
32 show: false
33 },
34 {
35 title: "新闻" ,
36 value: ["二","二","三","四"],
37 url: "/content",
38 show: false
39 },
40 {
41 title: "关于",
42 url: "/about"
43 },
44 {
45 title: "投稿",
46 url: "/submit"
47 }
48 ]
49 }
50 }
51 }
52 </script>
53 <style scoped>
54 .app-nav{
55 margin-bottom: 20px;
56 }
57 ul.ul-father {
58 background: Lightgreen;
59 margin: 0;
60 }
61 .li-father {
62 position: relative;
63 display: inline-block;
64 width: 20%;
65 margin: 0;
66 }
67 li a {
68 display: block;
69 padding: 15px 0;
70 color: #333;
71 text-decoration: none;
72 }
73 li a:hover,.bgchange>a{
74 color: #fff;
75 background: #222;
76 }
77 .ul-child{
78 position: absolute;
79 top: 51px;
80 left: 0;
81 width: 100%;
82 background: Lightgreen;
83 }
84 </style>

vue项目中router路由配置的更多相关文章

  1. 如何在Vue项目中给路由跳转加上进度条

    1.前言 在平常浏览网页时,我们会注意到在有的网站中,当点击页面中的链接进行路由跳转时,页面顶部会有一个进度条,用来标示页面跳转的进度(如下图所示).虽然实际用处不大,但是对用户来说,有个进度条会大大 ...

  2. Vue 项目中对路由文件进行拆分(解构的方法)

    项目需求场景: 在开发项目过程中,在项目过于庞大,路由信息非常多的情况下,如果将路由配置信息都放在一个文件里面,那么这个JS是不方便维护的, 那么,这个时候需要我们把这个庞大的路由文件,根据项目功能分 ...

  3. vue项目中vscode格式化配置和eslint配置冲突

    问题描述 使用vscode开发vue项目的时候,从远端拉下一个新的项目后,安装完依赖后跑起项目时,发现直接报了一堆语法错误:包括换行.空格.单双引号.分号等各种格式问题 因为我的 vscode 安装使 ...

  4. vue项目中的路由守卫

    路由守卫的意义就相当于一个保安一样,作用很大,在实际的项目中运用也是不少,也就是当客户在登陆自己账号的时候,有可能存在客户有啥事的时候,自己后台或者pc的关闭全部浏览器,没有点击退出登录,或者在退出登 ...

  5. Vue项目中使用webpack配置了别名,引入的时候报错

    chainWebpack(config) { config.resolve.alias .set('@', resolve('src')) .set('assets', resolve('src/as ...

  6. router路由配置

    vue项目中router路由配置   介绍 路由:控制组件之间的跳转,不会实现请求.不用页面刷新,直接跳转-切换组件>>> 安装 本地环境安装路由插件vue-router:    c ...

  7. vue 项目中当访问路由不存在的时候默认访问404页面

    前言: 在Vue项目中,当访问的页面路由不存在或错误时,页面显示为一片空白.然而,通常我们需要对访问url不存在或者错误的情况下添加默认的404页面,即not found页面. 一般的处理方法是: 在 ...

  8. vue项目中路由验证和相应拦截的使用

    详解Vue路由钩子及应用场景(小结):https://www.jb51.net/article/127678.htm vue项目中路由验证和相应拦截的使用:https://blog.csdn.net/ ...

  9. vue项目中postcss-pxtorem的使用及webpack中的配置 css中单位px和em,rem的区别

    移动手机版要求我们在制作嵌入h5的时候去适配不同的手机.适配有多重模式,有flex.百分比等.字体大小的控制也有px.百分比.rem等单位,webpack中 px转rem. vue项目中postcss ...

随机推荐

  1. 【l转】VS2015下解决:无法解析的外部符号 __imp___vsnprintf 及__iob_func

    https://blog.csdn.net/hebbely/article/details/53780562

  2. 如何修改linux 用户登录后默认目录

    1.linux用户登录后默认目录是在/etc/passwd文件设置的.如下图所示,一共显示了四行数据,其中第一行的/root即为root用户登录后的默认目录,第二行daemon用户的默认目录是/usr ...

  3. nginx 和 php

    sudo apt-get install nginx sudo groupadd www sudo useradd -g www www /etc/nginx/nginx.conf service n ...

  4. 阶段1 语言基础+高级_1-3-Java语言高级_08-JDK8新特性_第2节 Stream流式思想概述_3_流式思想概述

  5. 阶段1 语言基础+高级_1-3-Java语言高级_1-常用API_1_第4节 ArrayList集合_16-ArrayList练习一_存储随机数

    循环6次就是6.fori 循环子在外部+1就是得到的1到33的数字 list.fori遍历集合 自动生for循环的代码

  6. linux(centos6.5)常用命令

    前言:由于项目项目使用的是linux服务器,因此会使用到较多linux命令,本文对centos下常用命令进行记录 1.vi的三种模式 2.解压缩相关 3.用户相关 4.文件相关 5.各种查看命令 1. ...

  7. 【ABAP系列】SAP GUI740 PATCH5出现弹窗BUG

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[ABAP系列]SAP GUI740 PATCH ...

  8. linux下安装nginx(nginx(nginx-1.8.0.tar.gz),openssl(openssl-fips-2.0.9.tar.gz) ,zlib(zlib-1.2.11.tar.gz),pcre(pcre-8.39.tar.gz))

    :要按顺序安装: 1:先检查是否安装 gcc ,没有先安装:通过yum install gcc-c++完成安 2:openssl : tar -zxf  openssl-fips-2.0.9.tar. ...

  9. mooc-IDEA live template--006

    十二.IntelliJ IDEA -live template 以定时器为例: 1.创建一个Template Group... 2.在创建的Template Group下面,创建一个Live Temp ...

  10. 关于存储过程的一些sql

    1.关于事务的回滚Set XACT_ABORT ON; 开启为on时 ,如果事务语句产生运行错误 ,将整个事务终止进行回滚,Off时只回滚产生错误的语句. 2.获取事务语句中上一次插入值的行号@@ID ...