gitHub地址:https://github.com/huangpna/vue_learn/example里面的lesson14

安装

直接引入

<script src="vue.js"></script>
<script src="vue-router.js"></script>

vue-router下载链接:点击复制代码

NPM下载

npm install vue-router

如果在一个模块化工程中使用它,必须要通过 Vue.use() 明确地安装路由功能:
在你的文件夹下的 src 文件夹下的 main.js 文件内写入以下代码

import Vue from 'vue'
import VueRouter from 'vue-router'Vue.use(VueRouter)

入门

先简单写一个例子:

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>index1</title>
</head>
<body><div id="app"><h1>HELLO APP!</h1><p><router-link to="/foo">to foo</router-link><router-link to="/bar">to bar</router-link></p><router-view></router-view></div>
</body>
<script src="../js/vue.js"></script>
<script src="../js/vue-router.js"></script>
<script>const Foo = { template: '<div>foo</div>' }const Bar = { template: '<div>bar</div>' }  //配置路由const routes = [{ path: '/foo', component: Foo },{ path: '/bar', component: Bar }];  //实现路由实例化const router = new VueRouter({routes});  //挂载到Vue的实例上const app = new Vue({router}).$mount('#app');
</script>
</html>

<router-link> 默认会被渲染成一个 <a> 标签  >> to=" " 为我们定义的路由

<router-view> 路由匹配到的组件将渲染在这里

------上述例子实现效果可拷贝代码在本地查看。

动态路由匹配

我们经常需要将具有给定模式的路线映射到同一个组件。例如,我们可能有一个User应该为所有用户呈现但具有不同用户ID的组件。在vue-router我们可以在路径中使用动态段以实现:

栗子:

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>index2</title>
</head>
<body>
<div id="app2"><p><router-link to="/user/1">to 用户一</router-link><router-link to="/user/2">to 用户二</router-link></p><router-view></router-view>
</div>
</body>
<script src="../js/vue.js"></script>
<script src="../js/vue-router.js"></script>
<script>const user = { template: '<div>{{$route.params.id}}</div>' };const routes = [{ path: '/user/:id', component: user}];const router = new VueRouter({routes});const app = new Vue({router}).$mount('#app2');
</script>
</html>

动态段由冒号表示:。匹配路径时,动态段的值将this.$route.params在每个组件中公开。

除此之外$route.params,该$route对象还公开了其他有用的信息,例如$route.query(如果URL中有查询)$route.hash等。您可以在API参考中查看完整的详细信息。

------上述例子实现效果可拷贝代码在本地查看。

嵌套路由

我们经常将动态路由和嵌套路由共同使用,嵌套路由即是在原路由的基础上增加一个 children ,children 是一个数组.并且我们还需要在原来的组件上添加< router-view >来渲染 chlidren 里面的路由.

栗子:

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>index3</title><style>#app3>p>a{padding:5px 10px;}.router-link-exact-active{background-color: darkslateblue;border-radius: 4%;color:#FFF;}</style>
</head>
<body><div id="app3"><p><router-link to="/user/foo">/user/foo</router-link><router-link to="/user/foo/profile">/user/foo/profile</router-link><router-link to="/user/foo/posts">/user/foo/posts</router-link></p><router-view></router-view></div>
</body>
<script src="../js/vue.js"></script>
<script src="../js/vue-router.js"></script>
<script>const user = {template: `<div><h1>User {{$route.params.id}}</h1><router-view></router-view></div>`};const userHome = { template: '<div>userHome</div>' };const userProfile = { template: '<div>userProfile</div>' };const userPosts = { template: '<div>userPosts</div>' };const routes = [{path: '/user/:id',component: user,children: [{path: '',      //通过上述配置,当您访问时/user/foo,内部User的任何内容都不会呈现,因为没有子路径匹配。也许你确实希望在那里渲染一些东西。在这种情况下,您可以提供一个空的子路径路径component: userHome},{path: 'profile',component: userProfile},{path: 'posts',component: userPosts}]}];const router = new VueRouter({routes});const app3 = new Vue({router}).$mount('#app3');
</script>
</html>

------上述例子实现效果可拷贝代码在本地查看。

程序化导航

除了使用<router-link>为声明性导航创建锚标签之外,我们可以使用路由器的实例方法以编程方式执行此操作。

router.push(location)

想要导航到不同的 URL,则使用 router.push 方法。这个方法会向 history 栈添加一个新的记录,所以,当用户点击浏览器后退按钮时,则回到之前的 URL。

当你点击 <router-link> 时,这个方法会在内部调用,所以说,点击 <router-link :to="..."> 等同于调用 router.push(...)。

该方法的参数可以是一个字符串路径,或者一个描述地址的对象。例如:

// 字符串
router.push('home')// 对象
router.push({ path: 'home' })// 命名的路由
router.push({ name: 'user', params: { userId: 123 }})// 带查询参数,变成 /register?plan=private
router.push({ path: 'register', query: { plan: 'private' }})

下述router.replace跟 router.push 很像,唯一的不同就是,它不会向 history 添加新记录,而是跟它的方法名一样 ——而是替换掉当前的 history 记录。

router.replace(location)

router.go这个方法的参数是一个整数,意思是在 history 记录中向前或者后退多少步,类似 window.history.go(n)。

// 在浏览器记录中前进一步,等同于 history.forward()
router.go(1)// 后退一步记录,等同于 history.back()
router.go(-1)// 前进 3 步记录
router.go(3)// 如果 history 记录不够用,那就默默地失败呗
router.go(-100)
router.go(100)

命名路由

有时我们通过一个名称来标识一个路由显得更方便一些,特别是在链接一个路由,或者是执行一些跳转的时候。你可以在创建 Router 实例的时候,在 routes 配置中给某个路由设置名称。

我们直接在路由下添加一个 name 即可.

var routes = [{path:"/one",name:"one",component:{template:"#a"}},{path:"/two",name:"two",component:{template:"#b"},},
]

要链接到一个命名路由,可以给 router-link 的 to 属性传一个对象:

<router-link :to="{ name: 'one', params: { userId: 123 }}">User</router-link>

<router-link :to="{ name: 'two'}">User</router-link>

另外以编程的方式实现以上效果可使用router.push()

router.push({ name: 'user', params: { userId: 123 }})

路由传参获取:

this.$route.params.userId

命名视图

有时您需要同时显示多个视图而不是嵌套它们,例如创建具有sidebar视图和main视图的布局。这就是命名视图派上用场的地方。您可以拥有多个并为每个插座指定一个名称,而不是在您的视图中只有一个插座。router-view没有名字的A 将default作为其名称。

<router-view class="view one"></router-view>
<router-view class="view two" name="a"></router-view>
<router-view class="view three" name="b"></router-view>

通过使用组件呈现视图,因此多个视图需要同一路径的多个组件。确保使用components(带有s)选项:

const router = new VueRouter({routes: [{path: '/',components: {default: Foo,a: Bar,b: Baz}}]
})

栗子:链接

嵌套命名视图

栗子:链接

重定向和别名

重定向:

重定向也在routes配置中完成。要重定向/a/b

const router = new VueRouter({routes: [{ path: '/a', redirect: '/b' }]
})

栗子:链接

重定向也可以定位到命名路线:

const router = new VueRouter({routes: [{ path: '/a', redirect: { name: 'foo' }}]
})

栗子:链接

别名

/a的别名是/b,意味着,当用户访问/b时,URL会保持/b,但是路由匹配为/a,就像用户访问/a一样。

对应的路由配置为:

const router = new VueRouter({routes: [{ path: '/a', component:A, alias: '/b' }]})

别名的功能让我们可以自由地将ui结构映射到任意的URL, 而不是受限于配置的嵌套路由结构。

转载于:https://www.cnblogs.com/1156063074hp/p/10233814.html

vue-router的基本使用相关推荐

  1. vue router 入门笔记

    vue router 入门笔记 tips: components优先级大于component,即当一个route对象里同时配置了component和components时component视为无效 即 ...

  2. 你可能不清楚的 Vue Router 深度用法(一)

    Vue Router 简单易上手,能实现大部分的需求.但是,如果在项目里需要更细致的控制路由,以实现与其同步的效果,就需要挖掘其文档里没详细提及的内容.第一章为路由元信息用途挖掘. 路由元信息用途 ( ...

  3. vue router html,vue-router.html

    Document Hello App! Go to Foo Go to Bar // 1. 定义(路由)组件. // 可以从其他文件 import 进来 const Foo = { template: ...

  4. Vue.js 从 Vue Router 0.7.x 迁移

    从 Vue Router 0.7.x 迁移 只有 Vue Router 2 是与 Vue 2 相互兼容的,所以如果你更新了 Vue ,你也需要更新 Vue Router .这也是我们在主文档中将迁移路 ...

  5. 你可能不清楚的 Vue Router 深度用法(二)

    此为 Vue Router 深度用法的第二篇,主要讲述动态路由匹配用法.第一篇的链接在此: https://segmentfault.com/a/11... 动态路由匹配是用于把某种模式匹配到的所有路 ...

  6. vue router html后缀,vue-router.html

    Document Hello App! Go to Foo Go to Bar // 1. 定义(路由)组件. // 可以从其他文件 import 进来 const Foo = { template: ...

  7. vue require css html,requirejs vue vue.router简单框架

    index.html 入口页面html> vue `menu`.`name` base.js requirejs 配置文件(function(){    requirejs.config({   ...

  8. Vue Router 4.0 正式发布!焕然一新。

    关注若川视野,回复"pdf" 领取资料,回复"加群",可加群长期交流学习 12月8日,Vue Router 4 正式发布稳定版本. 在经历了 14 个 Alph ...

  9. vue router 参数_Vue.js项目开发技术解析

    Vue.js项目开发技术解析 一.Vue.js实例 在一个Vue.js工程中,用于显示内容最基层的实例称之为根实例.通过该实例可以进行页面或组件的更新和显示.对于项目本身而言,无论是什么样的页面,都要 ...

  10. Vue Router的详细教程

    Vue Router的详细教程 安装 #直接下载 / CDN https://unpkg.com/vue-router/dist/vue-router.js Unpkg.com 提供了基于 NPM 的 ...

最新文章

  1. 聊聊 HashMap 和 TreeMap 的内部结构
  2. kali linux提示安装系统失败,kali“安装系统”失败分析及解决
  3. 进程,线程与信息共享
  4. 如果知道一个控件类型的对话框句柄是编辑框控件
  5. ccna设备的NAT地址转换
  6. Ubuntu16.04 安装RabbitMQ
  7. 数据结构与算法 / 排序算法(3)
  8. 解决:the selection is not within a valid module
  9. C#经典名著:《C#入门经典》(第4版)
  10. iOS NSURLSession VS NSURLConnection
  11. HDU 2604 Queuing( 递推关系 + 矩阵快速幂 )
  12. Atitit 类库冲突 解决方案
  13. Ajax方法实现登录页面
  14. installshield2020项目部署打包详细教程
  15. 使用APICloud开发物流仓储app项目实践
  16. python设置excel单元格数据类型为文本_Python xlwt设置excel单元格字体及格式
  17. SX1278、SX1276、SX1262的简单详解
  18. win10家庭版解决“管理员已阻止你运行此应用”
  19. 左手画方右手画圆代码
  20. 自己修好了乐视电视!

热门文章

  1. nodejs npm常用命令
  2. C#编程(四十五)----------格式字符串
  3. PHP之路——MySql查询语句
  4. 人生的意义—我们为什么活着?
  5. php 删除指定html标签,总结php删除html标签和标签内的内容的方法
  6. Vivado中MIG核中DDR的读写控制
  7. C++实现统计从1到n中1出现的次数(另外一种方法)
  8. 在计算机科学中算法这个术语是,计算机科学与基础考试模拟一.docx
  9. neural network神经网络识别手写字体
  10. 北京超级云计算GPU服务器的使用教程