配置路由之前,先检查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. Raize 重新编译

    最近项目用到了Raize5的日历控件, 需要在中文版本与英文版本中切换显示, 这个需要修改 RzPopups.pas, 修改了需要重新编译. 费老大劲了.   首选修改 RzBorder.pas, 不 ...

  2. C# 正则表达式提取字符串中括号里的值

    version = Regex.Replace(str, @"(.*\()(.*)(\).*)", "$2"); //小括号() Regex rgx = new ...

  3. Angular.js指令

    ng-style   <input type="button" value="set color" ng-click="myStyle={col ...

  4. 介绍Kubernetes监控Heapster

    什么是Heapster? Heapster是容器集群监控和性能分析工具,天然的支持Kubernetes和CoreOS,Kubernetes有个出名的监控agent—cAdvisor.在每个kubern ...

  5. VUE引入字体图标库

    1. 下载阿里图标 2. 解压文件,并复制文件到VUE项目内 3. 找到添加的字体图标的.css文件,将.iconfont改成[class^="iconfont"], [class ...

  6. rsync如何不指定密码文件

    平时用rsync做数据同步时,都是通过--password-file指定一个密码文件 这个密码文件权限要求比较高,一般是600,属主属组都是rsync命令执行者 如果是在脚本中执行rsync,比如定时 ...

  7. vue 显示 webpack-dev-server不是内部命令的解决办法

    然后在cmd中cd到项目目录,依次运行命令: npm install 和 npm run build 最后运行 npm run dev 后项目成功运行.

  8. [c/c++] programming之路(29)、阶段答疑

    一.指针不等于地址 指针不仅有地址,还有类型,是一个存储了地址的变量,可以改变指向:而地址是一个常量 #include<stdio.h> #include<stdlib.h> ...

  9. java为什么要重写hashCode和equals方法?

    如果不被重写(原生)的hashCode和equals是什么样的? 不被重写(原生)的hashCode值是根据内存地址换算出来的一个值. 不被重写(原生)的equals方法是严格判断一个对象是否相等的方 ...

  10. mui中confirm在苹果出现bug,confirm点击确定跳转页面再返回后,页面被遮罩盖住无法使用

    项目中使用confirm mui.confirm('您还未抽奖,现在去抽奖吗?', function (res) { if (res.index === 1) { window.location.hr ...