1、路由作用

用vue.js + vue-router创建单页面应用。页面不需要刷新就可以页面跳转,提供用户更好体验。

2、路由配置

new Router({
routes: [{
path: '/', //匹配路径
name: 'Hello', //路由的别名
component: Hello //需要加载的组件名
}, {
//使用变量名的形式传递参数。例如:/detail/10086
//在Detail组件中使用{{$router.params.id}}来接收
path: '/detail/:id',
name: 'Detail',
component: Detail
}
]
})

 3、路由跳转

<router-link :to="{name:'Detail',params:{id:10086}}">详情</router-link>

 4、实践:两组件之间跳转

代码实例

4.1、index.js
import Vue from 'vue'
import Router from 'vue-router'
import Hello from '@/component/Hello'
import Detail from '@/component/Detail' Vue.use(Router) export defaultnew Router({
router: [{
path: '/',
name: 'Hello',
component: Hello
}, {
path: '/detail/:id',
name: 'Detail',
component: Detail
}]
}) 4.2、Hello.vue:src->component->Hello.vue <template>
<div class = "hello" >
<h1>这是hello页面 </h1>
< router - link: to = {name: 'Detail',params: {id: 10086}}>详情</router-link>
</div>
</template> <script>
export default{
name: 'hello',
data() {
return {
msg: 'hello vue'
}
}
}
</script> < !--add "scoped" attribute limit css to this compent only-- >
<style scoped>
h1, h2 {
font - weight: normal;
} ul {
list - style - type: none;
padding: 0;
} li {
display: inline - block;
margin: 0 10px;
} a {
color: # 42b983
}
</style> 4.3、Detail.vue:src->component->Detail.vue < template >
< div >
< h1 > 这是detail页面 </h1>
{{$route.params.id}}
</div>
</template> <script>
export default{
name: 'hello',
data() {
return {}
}
}
</script> < !--add "scoped" attribute limit css to this compent only-- >
< style scoped >
</style> 4.4、启动项目
my_vue_app > npm run dev

5、vue组件由三部分组成

vue组件:template:html代码、 script:javascript代码、style:css代码

vue-router路由的使用的更多相关文章

  1. 前端MVC Vue2学习总结(八)——Vue Router路由、Vuex状态管理、Element-UI

    一.Vue Router路由 二.Vuex状态管理 三.Element-UI Element-UI是饿了么前端团队推出的一款基于Vue.js 2.0 的桌面端UI框架,手机端有对应框架是 Mint U ...

  2. Vue系列:Vue Router 路由梳理

    Vue Router 是 Vue.js 官方的路由管理器.它和 Vue.js 的核心深度集成,让构建单页面应用变得易如反掌.包含的功能有: 嵌套的路由/视图表 模块化的.基于组件的路由配置 路由参数. ...

  3. 04 Vue Router路由管理器

    路由的基本概念与原理 Vue Router Vue Router (官网: https://router.vuejs.org/zh/)是Vue.js 官方的路由管理器. 它和vue.js的核心深度集成 ...

  4. Vue Router路由管理器介绍

    参考博客:https://www.cnblogs.com/avon/p/5943008.html 安装介绍:Vue Router 版本说明 对于 TypeScript 用户来说,vue-router@ ...

  5. Vue Router 路由守卫:完整的导航解析流程

    完整的导航解析流程 1 导航被触发. 2 在失活的组件里调用离开守卫. 3 调用全局的 beforeEach 守卫. 4 在重用的组件里调用 beforeRouteUpdate 守卫 (2.2+). ...

  6. Vue Router路由守卫妙用:异步获取数据成功后再进行路由跳转并传递数据,失败则不进行跳转

    问题引入 试想这样一个业务场景: 在用户输入数据,点击提交按钮后,这时发起了ajax请求,如果请求成功, 则跳转到详情页面并展示详情数据,失败则不跳转到详情页面,只是在当前页面给出错误消息. 难点所在 ...

  7. Vue Router 路由实现原理

    一.概念 通过改变 URL,在不重新请求页面的情况下,更新页面视图. 二.实现方式 更新视图但不重新请求页面,是前端路由原理的核心之一,目前在浏览器环境中这一功能的实现主要有2种方式: 1.Hash  ...

  8. Vue - Router 路由

    路由的注册 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF ...

  9. vue router路由(三)

    当环境搭建及Vue语法与指令都有所了解,该说下router. build目录是打包配置文件 (不建议动) config是vue项目基本配置文件 dist是构建后文件 js 手动创建 (根据需要) no ...

  10. vue router路由跳转了,但是页面没有变(已解决)

    小白学习 router.js:两个组件之间跳转 但是路由变了,页面没有改变的原因是因为app.vue里面没有router-view(很关键)

随机推荐

  1. JavaScript公共函数

    [在此处输入文章标题] // JScript 文件 /* ================================================================== JS 公 ...

  2. 《web与移动开发》征文活动

    活动简介: 首次征文活动有PHPChina主办,是面向全国所有的IT界活动,希望大家大家以PHPChina论坛为载体,积极的分享技术.希望技术的分享为大家的工作猪跑,也希望大家早日成为技术行业的中流砥 ...

  3. Dapper 的输出参数使用示范

    -- 普通SQL 示范-- Queries with output parameters. Hide Shrink Copy Code // output parameters // the para ...

  4. PHP执行insert语句报错“Data too long for column”解决办法

    PHP执行mysql 插入语句, insert语句在Navicat for mysql(或任意的mysql管理工具) 中可以正确执行,但是用mysql_query()函数执行却报错. 错误 : “Da ...

  5. 导入数据库备份报错1067 – Invalid default value for ‘create_time’

    通过navicat工具导入psc数据库备份文件,报错如下,mysql版本5.7 执行如下语句不通过 DROP TABLE IF EXISTS `guard_user`; CREATE TABLE `g ...

  6. RS:关于协同过滤,矩阵分解,LFM隐语义模型三者的区别

    项亮老师在其所著的<推荐系统实战>中写道: 第2章 利用用户行为数据 2.2.2 用户活跃度和物品流行度的关系 [仅仅基于用户行为数据设计的推荐算法一般称为协同过滤算法.学术界对协同过滤算 ...

  7. 开发 Swift 和 Objective-C 混编的 Framework

    来源:黄文臣 blog.csdn.net/hello_hwc/article/details/58320433 前言 为什么要写这样一篇文章,因为昨天和一个朋友讨论到Swift和Objective C ...

  8. Windbg使用方法

    hgy413的专栏 http://blog.csdn.net/hgy413/article/category/1143065

  9. 在Linux上rpm安装运行Redis 3.0.4

    http://www.rpmfind.net搜索redis,找到redis3.0.4的rpm源选做 wget ftp://fr2.rpmfind.net/linux/remi/enterprise/6 ...

  10. CentOS7.2安装python2.7.12

    目前CentOS7.2自带的python版本是python2.7.5.由于yum这个软件需要系统自带的python工作. 如果冒然用自己安装的python替换掉系统自带的,可能造成yum不工作. 先安 ...