配置路由之前,先检查vue版本,(案例适用vue2.0)

采用npm包的形式进行安装。

安装路由依赖:npm install vue-router(代码中,如果在一个模块化工程中使用它,必须要通过 Vue.use() 明确地安装路由功能)

更新一下 vue-cli:npm update vue-cli

一、使用路由
在main.js中,需要明确安装路由功能:

import Vue from 'vue'
import VueRouter from 'vue-router'
import App from './App.vue'
Vue.use(VueRouter)

1.定义组件,两种方式:

方式(1):从其他文件import进来

import index from './components/index.vue'
import hello from './components/hello.vue' 另一种方式(2)直接在js文件创建组件:如下:
//创建组件

  var index={
    template:'<h3>我是主页</h3>'
  };

  var hello={
    template:'<h3>我是主页</h3>'
  };

2.定义路由

const routes = [

    { path: '/index', component: index },
{ path: '/hello', component: hello },
]

3.创建 router 实例,然后传 routes 配置

const router = new VueRouter({
routes
})

4.创建和挂载根实例。通过 router 配置参数注入路由,从而让整个应用都有路由功能

const app = new Vue({
router,
render: h => h(App)
}).$mount('#app')

经过上面的配置之后呢,路由匹配到的组件将会渲染到App.vue里的<router-view></router-view>

那么这个App.vue里应该这样写:

<template>
<div id="app">
<router-view></router-view> <!-- 路由匹配到的组件将渲染在这里 -->
</div>
</template>

<router-link to="/goods">主页</router-link>  <!--在vue-router 2中,使用了<router-link></router-link>实现路由跳转-->

index.html里呢要这样写:

<body>
<div id="app"></div>
</body>
这样就会把渲染出来的页面挂载到这个id为app的div里了。 实例演示:
app.vue 页面代码: <template>

<div id="">
<div class="tab-item">
<router-link to="/goods">主页</router-link>
</div>

<!-- <div class="content">content</div>-->

<!-- 路由匹配到的组件将渲染在这里 -->
<router-view></router-view>

</div>

</template>

<script type="text/javascript">
import header from './components/header/header.vue';
export default{
};
</script>

main.js代码:

//main.js是 入口js
import Vue from 'vue';
import App from './App';
import VueRouter from 'vue-router';//VueRouter是一个变量,

Vue.config.productionTip = false

Vue.use(VueRouter);//如果在一个模块化工程中使用它,必须要通过 Vue.use() 明确地安装路由功能


//组件
var Goods={
template:'<h3>我是主页</h3>'
};
//定义路由
const routes = [
{ path: '/goods', component: Goods },

]

//创建 router 实例,然后传 routes 配置
const router = new VueRouter({
routes
})

//创建和挂载根实例。通过 router 配置参数注入路由,从而让整个应用都有路由功能
const app = new Vue({
router,
render: h => h(App)
}).$mount('#app')

嵌套路由:

//  定义路由如下:
const routes = [
   { path: '/', redirect: '/home' },
    {
       path: '/home',
       component: Home,
       children:[
         { path: '/home/login', component: Login},
         { path: '/home/reg', component: Reg}
       ]
    },
  { path: '/news', component: News}
]

参考:http://www.jb51.net/article/106326.htm

报错:

router.map is not a function

用的是vue2.0,配置路由时,vue中不识别vue1.0中的map

Cannot read property 'component' of undefined (即vue-router 0.x转化为2.x

vue项目原本是用0.x版本的vue-router,但是却报出:Cannot read property 'component' of undefined

这是因为版本问题,由于vue2删除了vue1的内部指令,而vue-router1.x依赖vue的一个内部指令

参考:http://www.jianshu.com/p/cb918fe14dc6
   http://blog.csdn.net/m0_37754657/article/details/71269988
 

vue2.0配置路由的更多相关文章

  1. 总结vue2.0 配置的实例方法

    总结vue2.0 配置的实例方法 http://www.php.cn/js-tutorial-369603.html

  2. vue2.0 配置环境总结(都是泪啊)

    最近有点空闲时间,终于把一直想学的vue提上了日程,以下是收集的一些帮助入门的链接 1:https://vuefe.cn/v2/guide/ vue2.0中文官网 2:https://router.v ...

  3. Vue2.0实现路由

    Vue2.0和1.0实现路由的方法有差别,现在我用Vue 2.0实现路由跳转,话不多说,直接上代码 HTML代码 <div class="tab"> <route ...

  4. vue2.0 配置 选项 属性 方法 事件 ——速查

    全局配置 silent  设置日志与警告  optionMergeStrategies   合并策略  devtools  配置是否允许vue-devtools  errorHandler    错误 ...

  5. vue2.0 配置build项目打包目录、资源文件(assets\static)打包目录

    vue项目默认的打包路径:根目录下的dist文件夹下: 但是在项目开发中,我们肯定希望项目提交到svn目录或者git目录下,否则每次复制过去,太麻烦了: 那怎么配置打包路径呢?下面来看看: 我们找到打 ...

  6. vue2.0 配置sass

    一.配置sass依赖 npm install node-sass --save-dev npm install sass-loader --save-dev 二.打开build文件夹下的webpack ...

  7. vue1.0配置路由

    1,//创建 router 实例 var router = new VueRouter() 2,//components下新建home.vue组件,并在app.vue中引入模块: import hom ...

  8. vue2.0:(五)、路由vue-router

    好的,接下来,我们来写路由.用的是vue2.0的路由. 步骤一:配置main.js import Vue from 'vue'; import App from './App'; import rou ...

  9. vue2.0 路由学习笔记

    昨天温故了一下vue2.0的路由 做个笔记简单记录一下! 1.首相和vue1.0一样 要使用vuejs的路由功能需要先引入vue-router.js 2.然后修改原有a标签处代码 这里以一个ul li ...

随机推荐

  1. 使用 dom4j 处理 xml (1)

    解决问题需要,自己简单学习了一下dom4j 的基本用法: (1)读取 xml 文件: (2)修改 xml 文件. 需要的 jar 包: dom4j-xxx.jar (可以在 https://dom4j ...

  2. 二进制方式安装mysql5.7.24

    1.实验环境 [root@test-mysql ~]# cat /etc/redhat-release CentOS Linux release 7.3.1611 (Core) 2.浏览器下载mysq ...

  3. liunx驱动----构造和运行模块

    以hello world模块为例 #include <linux/init.h> #include <linux/module.h> //在执行 insmod hlello 的 ...

  4. redis知识点汇总

    1. redis是什么 2. 为什么用redis 3. redis 数据结构 4. redis中的对象类型 5. redis都能做什么?怎么实现的的? 6. redis使用过程中需要注意什么 7. 数 ...

  5. vue-cli3使用webpack-spritesmith配置雪碧图

    一.背景问题 项目中如果有大量的小图标,如果不使用阿里的iconfont.UI给一个加一个,加一个引用一个,每个图标虽然很小,但是也是一次请求,每次请求都是消耗性能资源的. 二.解决思路 使用webp ...

  6. 5、Storm集成Kafka

    1.pom文件依赖 <!--storm相关jar --> <dependency> <groupId>org.apache.storm</groupId> ...

  7. 谷歌浏览器导出excel失败问题解决(网上都没解决)

    java poi导出excel报了网络错误,信息已经写回到chrome浏览器(IE/FF均无此问题).如下所示: 从chrome的network大小部分也可以看出是正确的. 网上很多答案说将file. ...

  8. xampp——apache服务启动问题(端口占用)

    Apache启动提示 20:39:02 [Apache] Error: Apache shutdown unexpectedly.20:39:02 [Apache] This may be due t ...

  9. springMVC注解总结

    由于BookController类加了value="/book"的@RequestMapping的注解,所以相关路径都要加上"/book",即请求的url分别为 ...

  10. 启动Weblogic问题集锦

    报错1:Could not obtain the localhost address. The most likely cause is an error in the network configu ...