注意,我们将在指南中使用es 2015代码样本。此外,所有示例都将使用VUE的完整版本来使在线模板编译成为可能。请参阅这里的更多细节。

用vue路由器创建单页应用程序是非常简单的。使用vue.js,我们已经在用组件组合我们的应用程序。当将vue路由器添加到混合时,我们所需要做的就是将组件映射到路由,让vue路由器知道在哪里呈现它们。下面是一个基本示例:

HTML

<script src="https://unpkg.com/vue/dist/vue.js"></script>
<script src="https://unpkg.com/vue-router/dist/vue-router.js"></script> <div id="app">
<h1>Hello App!</h1>
<p>
<!-- use router-link component for navigation. -->
<!-- specify the link by passing the `to` prop. -->
<!-- `<router-link>` will be rendered as an `<a>` tag by default -->
<router-link to="/foo">Go to Foo</router-link>
<router-link to="/bar">Go to Bar</router-link>
</p>
<!-- route outlet -->
<!-- component matched by the route will render here -->
<router-view></router-view>
</div>

 

JavaScript

// 0. If using a module system (e.g. via vue-cli), import Vue and VueRouter
// and then call `Vue.use(VueRouter)`. // 1. Define route components.
// These can be imported from other files
const Foo = { template: '<div>foo</div>' }
const Bar = { template: '<div>bar</div>' } // 2. Define some routes
// Each route should map to a component. The "component" can
// either be an actual component constructor created via
// `Vue.extend()`, or just a component options object.
// We'll talk about nested routes later.
const routes = [
{ path: '/foo', component: Foo },
{ path: '/bar', component: Bar }
] // 3. Create the router instance and pass the `routes` option
// You can pass in additional options here, but let's
// keep it simple for now.
const router = new VueRouter({
routes // short for `routes: routes`
}) // 4. Create and mount the root instance.
// Make sure to inject the router with the router option to make the
// whole app router-aware.
const app = new Vue({
router
}).$mount('#app') // Now the app has started!

通过注入路由器,在任何组件的内部,我们可以访问它。this.$router,以及当前路由。

// Home.vue
export default {
computed: {
username () {
// We will see what `params` is shortly
return this.$route.params.username
}
},
methods: {
goBack () {
window.history.length > 1
? this.$router.go(-1)
: this.$router.push('/')
}
}
}

在整个文档中,我们经常使用路由器实例。记住这一点。$router与使用路由器完全一样。我们使用它的原因是。$router是因为我们不想在每个需要操作路由的组件中导入路由器。您也可以查看这个示例livevee。注意,当一个<router-link> 的目标路由匹配时,它会自动获得.router-link-activeclass。在它的API参考中了解更多有关它的信息。

vue-router教程二(要素篇之新手入门)的更多相关文章

  1. DataVeryLite入门教程(二) Entity篇

    DataVeryLite 是基于.net 4.0的数据库持久化ORM框架. 目前支持的数据库有Sqlserver,Mysql,Oracle,Db2,PostgreSql,Sqlite和Access. ...

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

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

  3. 总结Vue 第四天:vue-cli(Vue2.0 新手入门 — 从环境搭建到发布)

    总结Vue 第四天:vue-cli(Vue2.0 新手入门 - 从环境搭建到发布) 一.Vue CLI----(Vue2.0 新手入门 - 从环境搭建到发布): ■   CLI是Command-Lin ...

  4. C#微信公众号开发系列教程二(新手接入指南)

    http://www.cnblogs.com/zskbll/p/4093954.html 此系列前面已经更新了两篇博文了,都是微信开发的前期准备工作,现在切入正题,本篇讲解新手接入的步骤与方法,大神可 ...

  5. [Vue 牛刀小试]:第十二章 - 使用 Vue Router 实现 Vue 中的前端路由控制

    一.前言 前端路由是什么?如果你之前从事的是后端的工作,或者虽然有接触前端,但是并没有使用到单页面应用的话,这个概念对你来说还是会很陌生的.那么,为什么会在单页面应用中存在这么一个概念,以及,前端路由 ...

  6. 二、Flex 布局教程:实例篇

    注:本文转自大神阮一峰,自己加了少许改动~ 上一篇文章介绍了Flex布局的语法,今天介绍常见布局的Flex写法. 你会看到,不管是什么布局,Flex往往都可以几行命令搞定. 我只列出代码,详细的语法解 ...

  7. 【LaTeX】E喵的LaTeX新手入门教程(1)准备篇

    昨天熄灯了真是坑爹.前情回顾[LaTeX]E喵的LaTeX新手入门教程(1)准备篇 [LaTeX]E喵的LaTeX新手入门教程(2)基础排版上一期测试答案1.大家一开始想到的肯定是\LaTeX{}er ...

  8. vue Router——进阶篇

    vue Router--基础篇 1.导航守卫 正如其名,vue-router 提供的导航守卫主要用来通过跳转或取消的方式守卫导航.有多种机会植入路由导航过程中:全局的, 单个路由独享的, 或者组件级的 ...

  9. 「vue基础」一篇浅显易懂的 Vue 路由使用指南( Vue Router 上)

    大家好,今天的内容,我将和大家一起聊聊 Vue 路由相关的知识,如果你以前做过服务端相关的开发,那你一定会对程序的URL结构有所了解,我没记错的话也是路由映射的概念,需要进行配置. 其实前端这些框架的 ...

随机推荐

  1. Linux操作系统简介

    一:Linux系统概述 1.常见操作系统 - 服务端操作系统 : linux.unix.windows server - 单机操作系统 : windows(dos .ucdos.win95.win98 ...

  2. 005-对象——对象的 final const

    <?php /** * */ /*class shouji { public $pinpai; final function chongdian() { //final 最终的 return $ ...

  3. linux中的/usr,/var,/opt目录详解

    转自:http://it.greenblogs.org/archives/2008/20113.shtml/ /usr文件系统  /usr 文件系统经常很大,因为所有程序安装在这里. /usr 里的所 ...

  4. 通过消费者和生产者的多线程程序,了解Java的wait()和notify()用法

    仓库类 public class Store { private int size = 0;//当前容量 private final int MAX = 10;//最大容量 //向仓库中增加货物 pu ...

  5. C++设计模式之-建造者模式

    建造者模式的定义将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示(DP).<大话设计模式>举了一个很好的例子——建造小人,一共需建造6个部分,头部.身体.左右手.左 ...

  6. 12.18 分布式系统下的session

    广义的session: 会话控制,可以理解成为一种保存key-value的机制 从key的方面来看:sessionId和token sessionId: 服务端请求客户端的时候,服务端通过setcoo ...

  7. 《APUE》第7章 进程环境-读书笔记

    一.main函数. main函数的原型如下.argc是命令行参数的数目,argv是指向参数的各个指针所构成的数组. int main(int argc, char *argv[]) 当内核执行C程序时 ...

  8. myeclipes快捷键

    package com.Test02;//alt+shift+s+s 自动创建toString()//ctrl+ shift+ o  自动导包//* alt+ shift +s+o 有参构造//* a ...

  9. SpringInAction--自动化装配Bean(隐式装配)

    关于Bean的介绍就具体不多介绍了,,, Spring在配置时候有三种方案可选 1.在xml中进行显示配置 2.在java中进行显示配置 3.隐式的Bean发现机制和自动装配 今天学习的就是自动化装配 ...

  10. PostgreSQL统计信息挖掘

    PG提供了丰富的统计信息,但是没有将这些统计信息使用的简单查询搞成存储过程,需要我们自己根据需要灵活的去挖掘,最近做了数据库监控,用了一些简单的东西,于是想往深了挖一下. 首先看看系统表和视图,他们都 ...