vue-router路由的使用
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路由的使用的更多相关文章
- 前端MVC Vue2学习总结(八)——Vue Router路由、Vuex状态管理、Element-UI
一.Vue Router路由 二.Vuex状态管理 三.Element-UI Element-UI是饿了么前端团队推出的一款基于Vue.js 2.0 的桌面端UI框架,手机端有对应框架是 Mint U ...
- Vue系列:Vue Router 路由梳理
Vue Router 是 Vue.js 官方的路由管理器.它和 Vue.js 的核心深度集成,让构建单页面应用变得易如反掌.包含的功能有: 嵌套的路由/视图表 模块化的.基于组件的路由配置 路由参数. ...
- 04 Vue Router路由管理器
路由的基本概念与原理 Vue Router Vue Router (官网: https://router.vuejs.org/zh/)是Vue.js 官方的路由管理器. 它和vue.js的核心深度集成 ...
- Vue Router路由管理器介绍
参考博客:https://www.cnblogs.com/avon/p/5943008.html 安装介绍:Vue Router 版本说明 对于 TypeScript 用户来说,vue-router@ ...
- Vue Router 路由守卫:完整的导航解析流程
完整的导航解析流程 1 导航被触发. 2 在失活的组件里调用离开守卫. 3 调用全局的 beforeEach 守卫. 4 在重用的组件里调用 beforeRouteUpdate 守卫 (2.2+). ...
- Vue Router路由守卫妙用:异步获取数据成功后再进行路由跳转并传递数据,失败则不进行跳转
问题引入 试想这样一个业务场景: 在用户输入数据,点击提交按钮后,这时发起了ajax请求,如果请求成功, 则跳转到详情页面并展示详情数据,失败则不跳转到详情页面,只是在当前页面给出错误消息. 难点所在 ...
- Vue Router 路由实现原理
一.概念 通过改变 URL,在不重新请求页面的情况下,更新页面视图. 二.实现方式 更新视图但不重新请求页面,是前端路由原理的核心之一,目前在浏览器环境中这一功能的实现主要有2种方式: 1.Hash ...
- Vue - Router 路由
路由的注册 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF ...
- vue router路由(三)
当环境搭建及Vue语法与指令都有所了解,该说下router. build目录是打包配置文件 (不建议动) config是vue项目基本配置文件 dist是构建后文件 js 手动创建 (根据需要) no ...
- vue router路由跳转了,但是页面没有变(已解决)
小白学习 router.js:两个组件之间跳转 但是路由变了,页面没有改变的原因是因为app.vue里面没有router-view(很关键)
随机推荐
- VB API判断数组为空
1. 'API判断数组为空或没有初始化 Private Declare Function SafeArrayGetDim Lib "oleaut32.dll" (ByRef saA ...
- Ajax的优缺点及工作原理?
定义和用法: AJAX = Asynchronous JavaScript and XML(异步的 JavaScript 和 XML).Ajax 是一种用于创建快速动态网页的技术.Ajax 是一种在无 ...
- Plus One 加一运算
Given a non-negative number represented as an array of digits, plus one to the number. The digits ar ...
- python 之html的headers提取操作
# -*- coding: cp936 -*- #python 27 #xiaodeng #python 之html的headers提取操作 # import urllib,urllib2 html= ...
- Golang Json文件解析为结构体工具-json2go
代码地址如下:http://www.demodashi.com/demo/14946.html 概述 json2go是一个基于Golang开发的轻量json文件解析.转换命令行工具,目前支持转换输出到 ...
- 软件申请获取root权限
申请root的工具类 //获取root权限 RootManager manager=new RootManager(); manager.upgradeRootPermission(getPack ...
- ios 中基本控件的定义
我的开发笔记--UILabel的详细使用及特殊效果 我的开发笔记--UIButton的详细使用 我的开发笔记---UISegmentedControl的详细使用 我的开发笔记---UITextFiel ...
- C结构体数组的应用
#include <stdio.h> //定义结构体存储学生成绩信息 struct address_list{ ]; ]; ]; } info[]; void save(char *nam ...
- CentOS 7 systemd的坑
一.概述 在从 CentOS 6 迁移到 CentOS 7 的过程中,可能有一些地方需要调整,最显著的地方莫过于 systemd 带来的改变,不同的管理服务的方式,不同的日志方式,设置时区,时间等等. ...
- spring 使用注解注入 list 或 map
1.定义一个接口或抽象类AClass 2.定义两个类实现或继承AClass,(BClass,MClass) 3.在第三个类XClass 中注入List 形如: @Autowired private L ...