官方文档

// 0. 如果使用模块化机制编程,导入Vue和VueRouter,要调用 Vue.use(VueRouter)

// 1. 定义(路由)组件。
// 可以从其他文件 import 进来
const Foo = { template: '<div>foo</div>' }
const Bar = { template: '<div>bar</div>' } // 2. 定义路由
// 每个路由应该映射一个组件。 其中"component" 可以是
// 通过 Vue.extend() 创建的组件构造器,
// 或者,只是一个组件配置对象。
// 我们晚点再讨论嵌套路由。
const routes = [
{ path: '/foo', component: Foo },
{ path: '/bar', component: Bar }
] // 3. 创建 router 实例,然后传 `routes` 配置
// 你还可以传别的配置参数, 不过先这么简单着吧。
const router = new VueRouter({
routes // (缩写)相当于 routes: routes
}) // 4. 创建和挂载根实例。
// 记得要通过 router 配置参数注入路由,
// 从而让整个应用都有路由功能
const app = new Vue({
router
}).$mount('#app') // 现在,应用已经启动了!

开始

通过注入路由器,我们可以在任何组件内通过 this.$router 访问路由器,也可以通过 this.$route 访问当前路由:

// Home.vue
export default {
computed: {
username () {
// 我们很快就会看到 `params` 是什么
return this.$route.params.username
}
},
methods: {
goBack () {
window.history.length > 1
? this.$router.go(-1)
: this.$router.push('/')
}
}
}

Home.vue

1:先下载路由
npm install vue-router --save
2:导入
import VueRouter from "vue-router"
// 定义(路由)组件。 3:
如果使用模块化机制编程,导入Vue和VueRouter,要调用 Vue.use(VueRouter)
Vue.use(VueRouter)
1定制路由(组件)
// 导入路由组件
import Index from './Index'
import Luffy from './Luffy' // 2、创建 router 实例,然后传 `routes` 配置 const router = new VueRouter({
mode: 'history',
routes:[
{ path: '/', component: Index },
{ path: '/luffy', component: Luffy }
]
}) 3
new Vue({
el: '#app',
router, // 挂载router实例
render: h => h(App)
}) 4,在主主件写出口
<!-- 路由出口 所有路由匹配的组件都会被渲染到这里 -->
<router-view></router-view> 5:a标签要换成router-link
<router-link v-for='(item,index) in urls' :to="item.href" :class='{active:currentIndex==index}' @click.native='clickHandler(index)' >{{item.name}}</router-link> <!-- 给router-link添加事件 会阻止click事件的触发,需要加上.navtive就可以了。加上.navtive 才是真正点击了a标签的click事件,在组件中不加.native 不会触发原生的事件。注意了注意了 -->
<!-- <router-link to="/luffy">路飞学城</router-link> -->

主主件

<template>
<div id="app">
<img src="./assets/logo.png">
<h1>{{ msg }}</h1>
<h2>Essential Links</h2>
<ul> <router-link v-for='(item,index) in urls' :to="item.href" :class='{active:currentIndex==index}' @click.native='clickHandler(index)' >{{item.name}}</router-link> <!-- 给router-link添加事件 会阻止click事件的触发,需要加上.navtive就可以了。加上.navtive 才是真正点击了a标签的click事件,在组件中不加.native 不会触发原生的事件。注意了注意了 -->
<!-- <router-link to="/luffy">路飞学城</router-link> --> </ul> <!-- 路由出口 所有路由匹配的组件都会被渲染到这里 -->
<router-view></router-view> </div>
</template> <script>
export default {
name: 'app',
data () {
return {
msg: 'Welcome to Your Vue.js App',
urls:[
{href:'/',name:"首页"},
{href:'/luffy',name:"路飞学城"} ],
currentIndex:0
}
},
methods:{
clickHandler(index){
console.log(index)
this.currentIndex = index; }
}
}
</script> <style>
#app {
font-family: 'Avenir', Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
text-align: center;
color: #2c3e50;
margin-top: 60px;
} h1, h2 {
font-weight: normal;
} ul {
list-style-type: none;
padding: 0;
} li {
display: inline-block;
margin: 0 10px;
} a {
color: #42b983;
}
a.active{
color: yellow;
}
</style>

app。vue

子主件

<template>
<div class="luffy">
<h4>我是路飞</h4>
</div>
</template>
<script>
export default{
name:'luffy',
data(){
return { }
}
}
</script>
<style> </style>

luffy.vue

<template>
<div class="index">
<h3>我是首页</h3>
</div>
</template>
<script>
export default{
name:'index',
data(){
return { }
}
}
</script>
<style> </style>

index。vue

vue 之 vue-router的更多相关文章

  1. Vue 组件之 Router

    Vue 组件之 Router Vue 开发单页应用的时候,免不了使用Vue组件.在单页应用上如何进行组件切换? 结构如下图所示: 主页面包含Foo组件与Bar组件,在主页面中可以进行Foo与 Bar的 ...

  2. vue路由请求 router

    创建一个Router.js文件 // 路由请求//声明一个常量设置路菜单// import Vue from "vue/types/index";import Vue from ' ...

  3. vue学习之router

    路由文档:https://router.vuejs.org/zh/guide/ 使用vue做spa应用的话,一定会涉及到路由. 安装 安装router插件 npm install vue-router ...

  4. vue学习之用 Vue.js + Vue Router 创建单页应用的几个步骤

    通过vue学习一:新建或打开vue项目,创建好项目后,接下来的操作为: src目录重新规划——>新建几个页面——>配置这几个页面的路由——>给根实例注入路由配置 src目录重整 在项 ...

  5. 六、vue路由Vue Router

    一.基本概念 route, routes, router 1, route,它是一条路由,由这个英文单词也可以看出来,它是单数, Home按钮  => home内容, 这是一条route,  a ...

  6. vue +ts 在router的路由中import报错的解决方案

    在router.ts中引入.vue文件,会提示打不到module,但是编译可能成功,运行也不报错 找了好久,发现了这个答案 https://segmentfault.com/a/11900000167 ...

  7. vue项目中router路由配置

    介绍 路由:控制组件之间的跳转,不会实现请求.不用页面刷新,直接跳转-切换组件>>> 安装 本地环境安装路由插件vue-router:    cnpm install vue-rou ...

  8. Vue的路由Router之导航钩子和元数据及匹配

    一.文件结构 二.vue.js 打开此链接 https://cdn.bootcss.com/vue/2.6.10/vue.js 复制粘贴页面的所有内容 三.vue-router.js 打开此链接  h ...

  9. vue路由--使用router.push进行路由跳转

    手机赚钱怎么赚,给大家推荐一个手机赚钱APP汇总平台:手指乐(http://www.szhile.com/),辛苦搬砖之余用闲余时间动动手指,就可以日赚数百元 route-link是在html中静态定 ...

  10. vue全家桶router、vuex、axios

    main.js import Vue from 'vue' import App from './App' import router from './router' import store fro ...

随机推荐

  1. ubuntu下源码安装wget

    1.背景 ubuntu18.04 64bit 2.安装方法如下: 2.1.获取源码 curl -o wget-1.20.tar.gz ftp://ftp.gnu.org/gnu/wget/wget-1 ...

  2. 多线程资源隔离之ThreadLocal

    上篇讲到多线程线程安全问题的解决思路,这篇将详细讲解资源隔离ThreadLocal的实践. ThreadLocal也叫线程局部变量,类似Map结构,以当前线程为key.既然是以资源隔离的思想保证线程安 ...

  3. git pull 分支问题

    问题: 是因为本地分支与远程分支没有链接关系, 让他们建立链接关系

  4. ScrambleString, 爬行字符串,动态规划

    问题描述: Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty su ...

  5. ubuntu mac terminal install software

    http-server // ubuntu sudo npm install http-server -g npm node.js yarn

  6. Jenkins基础复习

  7. 15 个有趣的 JS 和 CSS 库

    开发者们,一起来看看有木有你需要的前端库. 1. DisplayJS DisplayJS 是一个帮助你渲染 DOM 的简易框架.使用它,你可以更容易地将 JS 变量遍历到特定的 HTML 元素中,类似 ...

  8. 微信小程序------基本组件

    今天主要是简单的讲一下小程序当中的一些组件,微信文档上也是有的.但我还是坚持写一下,因为写博客可以再一次得到提高,印象更深刻,虽然很简单,但贵在坚持. 先来看看效果图: 1:进度条(progress) ...

  9. 创建一个最简单的SpringBoot应用

    已经来实习了一段时间了,从开始接触到SpringBoot框架到现在一直都感觉SpringBoot框架实在是为我们带来了巨大遍历之处,之前一直在用并没有总结一下,现在有空从零开始写点东西,也算是对基础的 ...

  10. 3.java内存模型以及happens-before规则

    1. JMM的介绍 在上一篇文章中总结了线程的状态转换和一些基本操作,对多线程已经有一点基本的认识了,如果多线程编程只有这么简单,那我们就不必费劲周折的去学习它了.在多线程中稍微不注意就会出现线程安全 ...