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 ...
随机推荐
- ng2
angularjs2的环境问题解决了好久. 百度到的答案也是各种各样还解决不了我的问题. 好在这几天经过不断的测试终于给解决了. ERROR in AppModule is not an NgModu ...
- C#WFM关于PICBOX 再DIP界面放大125%后,图片显示不完整
外观哪里选中Zoom,就好
- Python 嘉宾列表问题
某书上的练习题,当作复习8 #3-5 修改嘉宾名单 def alter(someone, other): if someone in din_list: din_list.remove(someone ...
- 第一个SDL程序
不废话,就WinMain主体: SDL_Window* window = NULL; SDL_Renderer* renderer = NULL; SDL_Event e; bool q = 0; i ...
- 2018-2019-2 20165215《网络对抗技术》Exp5 MSF基础应用
目录 实验内容 实验原理 实验步骤 (一)一个主动攻击实践 ms08_067(成功) (二)一个针对浏览器的攻击 ms14_064(成功) (三)一个针对客户端的攻击 Adobe(成功) CVE-20 ...
- Cron表达式解析
每一个域可出现的字符如下:Seconds: 可出现 ", - * /" 四个字符,有效范围为0-59的整数Minutes: 可出 ...
- CCF CSP 201509-1 数列分段
题目链接:http://118.190.20.162/view.page?gpid=T32 问题描述 试题编号: 201509-1 试题名称: 数列分段 时间限制: 1.0s 内存限制: 256.0M ...
- Linux环境配置文件的理解
百度百科: .bashrc这个文件主要保存个人的一些个性化设置,如命令别名.路径等.也即在同一个服务器上,只对某个用户的个性化设置相关. 示例: 编辑# User specific aliases a ...
- php,js 对字符串按位异或运算加密解密
异或的符号是^.按位异或运算, 对等长二进制模式按位或二进制数的每一位执行逻辑按位异或操作. 操作的结果是如果某位不同则该位为1, 否则该位为0. xor运算的逆运算是它本身,也就是说两次异或同一个数 ...
- Job for network.service failed because the control process exited with error code
转自:https://blog.csdn.net/dongfei2033/article/details/81124465 今天在centOS 7下更改完静态ip后发现network服务重启不了,翻遍 ...