vue2.0配置路由
配置路由之前,先检查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>这样就会把渲染出来的页面挂载到这个id为app的div里了。 实例演示:
<div id="app"></div>
</body>
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
报错:
用的是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配置路由的更多相关文章
- 总结vue2.0 配置的实例方法
总结vue2.0 配置的实例方法 http://www.php.cn/js-tutorial-369603.html
- vue2.0 配置环境总结(都是泪啊)
最近有点空闲时间,终于把一直想学的vue提上了日程,以下是收集的一些帮助入门的链接 1:https://vuefe.cn/v2/guide/ vue2.0中文官网 2:https://router.v ...
- Vue2.0实现路由
Vue2.0和1.0实现路由的方法有差别,现在我用Vue 2.0实现路由跳转,话不多说,直接上代码 HTML代码 <div class="tab"> <route ...
- vue2.0 配置 选项 属性 方法 事件 ——速查
全局配置 silent 设置日志与警告 optionMergeStrategies 合并策略 devtools 配置是否允许vue-devtools errorHandler 错误 ...
- vue2.0 配置build项目打包目录、资源文件(assets\static)打包目录
vue项目默认的打包路径:根目录下的dist文件夹下: 但是在项目开发中,我们肯定希望项目提交到svn目录或者git目录下,否则每次复制过去,太麻烦了: 那怎么配置打包路径呢?下面来看看: 我们找到打 ...
- vue2.0 配置sass
一.配置sass依赖 npm install node-sass --save-dev npm install sass-loader --save-dev 二.打开build文件夹下的webpack ...
- vue1.0配置路由
1,//创建 router 实例 var router = new VueRouter() 2,//components下新建home.vue组件,并在app.vue中引入模块: import hom ...
- vue2.0:(五)、路由vue-router
好的,接下来,我们来写路由.用的是vue2.0的路由. 步骤一:配置main.js import Vue from 'vue'; import App from './App'; import rou ...
- vue2.0 路由学习笔记
昨天温故了一下vue2.0的路由 做个笔记简单记录一下! 1.首相和vue1.0一样 要使用vuejs的路由功能需要先引入vue-router.js 2.然后修改原有a标签处代码 这里以一个ul li ...
随机推荐
- [Atcoder SoundHound Contest 2018]E.+ Graph
题面 Time limit : 2sec / Memory limit : 1024MB Score : 600 points Problem Statement-题目描述 Kenkoooo foun ...
- Log4j2 简介
介绍 Log4j2是Log4j的升级版,与之前的版本Log4j 1.x相比.有重大的改进,修正了Logback固有的架构问题的同事,改进了许多Logback所具有的功能. 特性 一.API 分离 Lo ...
- Mysql 导入CSV文件,中文内容乱码问题
项目中用到含有中文字段的数据CSV文件,导入Mysql数据中发现中文内容乱码. 分析原因:因为数据库字符编码问题引起. [1]创建utf-8字符集数据库 CREATE DATABASE db_name ...
- Cannot locate BeanDefinitionParser for element [scoped-proxy]
指定使用 CGLIB 而不使用 JDK 生成代理对象:注意:此两个标签必须同时出现,不然会报:Cannot locate BeanDefinitionParser for element [scope ...
- decimal(19,6)什么意思
decimal(19,6)什么意思 数字长度19位,精确到小数点后6位例如0.123456 mysql中varchar(50)最多能存多少个汉字 首先要确定mysql版本4.0版本以下,varchar ...
- vue单文件组件实例1:简单单文件组件
HelloWorld.vue: <template> <div class="hello"> <h1>{{msg}}</h1> ...
- 通过宝塔webhook,实现git自动拉取服务器代码
1.宝塔安装webhook,添加一条记录,脚本内容为: #!/bin/bash echo "" #输出当前时间 date --date='0 days ago' "+%Y ...
- java线程学习之notify方法和notifyAll方法
notify(通知)方法,会将等待队列中的一个线程取出.比如obj.notify();那么obj的等待队列中就会有一个线程选中并且唤醒,然后被唤醒的队列就会退出等待队列.活跃线程调用等待队列中的线程时 ...
- 记一次JAVAWEB项目部署
需求 原本服务器上tomcat部署了一个javaweb项目在80端口,这次要部署另一个javaweb项目在8090端口,或者同时部署在同一端口不同目录下. 解决方法 不同端口部署 不同端口部署我们需要 ...
- Python Iterables Iterators Generators
container 某些对象包含其它对象的引用,这个包含其它对象引用的对象叫容器.例如list可以包含int对象,或者由其它数据类型(或数据结构)的对象组成一个list. 对其他对象的引用是容器值的一 ...