上面是将Forecast组件作为了Home的子组件使用,现在我们将其作为一个路由组件使用。

在router/index.js路由系统注册路由:

{
path: '/forecast',
name: 'Forecast',
// route level code-splitting
// this generates a separate chunk (about.[hash].js) for this route
// which is lazy-loaded when the route is visited.
component: () => import(/* webpackChunkName: "about" */ '../components/Forecast.vue')
},

  

app.Vue中更新为:

<template>
<div id="nav">
<router-link to="/">Home</router-link> |
<router-link to="/about">About</router-link>|
<router-link to="/forecast">天气预报</router-link>
</div>
<router-view/>
</template>

1、路由跳转

vue-router提供了2种写法让我们实现页面跳转。

(1)通过router-link来跳转

正如App.Vue中的使用:

<template>
<div id="nav">
<router-link to="/">Home</router-link> |
<router-link to="/about">About</router-link>|
<router-link to="/forecast">天气预报</router-link>|
</div> <router-view/>
</template>

  

(2)通过this.$router来跳转

<template>
<div id="nav">
<router-link to="/">Home</router-link> |
<router-link to="/about">About</router-link>|
<router-link to="/forecast">天气预报</router-link>|
<a href="" @click.prevent="gohome">Home</a>
</div> <router-view/>
</template>
<script>
export default {
name: 'App', // 组件名
data(){
return {
user:"root",
}
},
methods:{
gohome(){
// 页面跳转
if(this.user === "root"){
this.$router.push("/"); // ajax页面跳转到指定的路由地址
// this.$router.back(); // 跳转返回上一页
// this.$router.go(-1); // -1相当于back,后退一页
// this.$router.go(1); // 1表示forward,前进一页
}
}
},
}
</script>
<style>
#app {
font-family: Avenir, Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
text-align: center;
color: #2c3e50;
} #nav {
padding: 30px;
} #nav a {
font-weight: bold;
color: #2c3e50;
} #nav a.router-link-exact-active {
color: #42b983;
}
</style>

  

2、传递参数

vue-router提供了2种用于开发中传递参数的方式给我们使用。

(1)路径参数

url地址的路径作为变量,传递参数到下一个页面组件中进行获取使用。

注册路由:

 {
path: '/article/:year/:month',
name: 'Article',
// route level code-splitting
// this generates a separate chunk (about.[hash].js) for this route
// which is lazy-loaded when the route is visited.
component: () => import(/* webpackChunkName: "about" */ '../components/Article.vue')
},

  

创建Article.vue:

<template>
<h3>
查询{{year}}年{{month}}的系列文章
</h3>
</template> <script>
export default {
name: "Article",
data(){
return {
year: 0,
month: 0,
}
},
created() {
this.year = this.$route.params.year;
this.month = this.$route.params.month;
}
}
</script> <style scoped> </style>

  

最后在App.Vue中添加:

<router-link to="/article/2000/12">文章列表</router-link>|

  

(2)查询参数

url地址的查询字符串作为参数,在下一个页面组件中进行获取使用。

注册路由:

 {
path: '/article2/',
name: 'Article2',
// route level code-splitting
// this generates a separate chunk (about.[hash].js) for this route
// which is lazy-loaded when the route is visited.
component: () => import(/* webpackChunkName: "about" */ '../components/Article2.vue')
},

  

创建Article2.vue:

<template>
<h3>
查询{{year}}年{{month}}的系列文章
</h3>
</template> <script>
export default {
name: "Article",
data(){
return {
year: 0,
month: 0,
}
},
created() {
this.year = this.$route.query.year
this.month = this.$route.query.month
}
}
</script> <style scoped> </style>

  

最后在App.Vue中添加:

<router-link to="/article2/?year=2008&month=12">文章列表2</router-link>|

  

Vue cli路由的更多相关文章

  1. vue cli 3

    介绍 Vue CLI 是一个基于 Vue.js 进行快速开发的完整系统 通过 @vue/cli 搭建交互式的项目脚手架. 通过 @vue/cli + @vue/cli-service-global 快 ...

  2. Vue Cli安装以及使用

      因为公司项目要用vue框架,所以会用vue-cli来新建项目.用过vue的都知道,要全局安装vue以及脚手架vue-cli,然后执行vue init webpack projectname来新建v ...

  3. Vue技术点整理-Vue CLI安装详解

     一,脚手架安装 Node 版本要求 Vue CLI 需要 Node.js +).你可以使用 nvm 或 nvm-windows 在同一台电脑中管理多个 Node 版本. 1,全局安装Vue CLI ...

  4. [Vue 牛刀小试]:第十七章 - 优化 Vue CLI 3 构建的前端项目模板(1)- 基础项目模板介绍

    一.前言 在上一章中,我们开始通过 Vue CLI 去搭建属于自己的前端 Vue 项目模板,就像我们 .NET 程序员在使用 asp.net core 时一样,我们更多的会在框架基础上按照自己的开发习 ...

  5. vue cli 4.0.5 的使用

    vue cli 4.0.5 的使用 现在的 vue 脚手架已经升级到4.0的版本了,前两日vue 刚发布了3.0版本,我看了一下cli 4 和cli 3 没什么区别,既然这样,就只总结一下vue cl ...

  6. vue/cli新旧版本安装方式

    一.老版本安装  Shift+鼠标右键 选择打开命令窗口 1.创建项目之前,需先确保本机已经安装node 在命令窗口中执行node -v npm -v 2.一般情况下用npm安装东西比较慢,可以使用淘 ...

  7. [转]Vue CLI 3搭建vue+vuex 最全分析

    原文地址:https://my.oschina.net/wangnian/blog/2051369 一.介绍 Vue CLI 是一个基于 Vue.js 进行快速开发的完整系统.有三个组件: CLI:@ ...

  8. 使用Vue CLI脚手架搭建vue项目

    本次是使用@vue/cli 3.11.0版本搭建的vue项目 1. 首先确保自己的电脑上的Node.js的版本是8.9版本或者以上 2. 全局安装vue/cli npm install @vue/cl ...

  9. vue/cli 3.0脚手架搭建

    在vue 2.9.6中,搭建vue-cli脚手架的流程是这样的: 首先 全局安装vue-cli,在cmd中输入命令: npm install --global vue-cli  安装成功:  安装完成 ...

  10. VUE CLI环境搭建文档

    VUE CLI环境搭建文档 1.安装Node.js 下载地址 https://nodejs.org/zh-cn/download/ 2.全局安装VUE CLI win+R键打开运行cmd窗口输入一下代 ...

随机推荐

  1. js获取select标签的 value 和 text

    <select name="" id="test"> <option value="a1">yi</optio ...

  2. 重新整理 .net core 实践篇—————Entity的定义[二十五]

    前言 简单介绍一下实体模型的设计. 正文 前文提及了我们的应用分为: 共享层 基础设施层 领域层 应用层 今天来介绍领域模型层. 前文提及到领域模型在共享层有一个领域模型抽象类库. 里面有这些类: 先 ...

  3. HDMI输入SIL9293C配套NR-9 2AR-18的国产GOWIN开发板

  4. vue项目node-scss装不上问题( vue执行npm install报错: Can‘t find Python executable “python“, you can set the PYTHON env variable

    一.描述从网上下载的一个Vue模板项目,导入VsCode,执行npm install命令后,报错了,报错的信息是node-sass安装失败,同时提示需要python环境的错误信息,这是因为安装node ...

  5. 离线语音识别,vosk,离线流式实时静音噪声监测,支持多语言开发python c++ c# java等

    #!/usr/bin/env python3 from vosk import Model, KaldiRecognizer, SetLogLevel import sys import os imp ...

  6. ORA-01555:snapshot too old: rollback segment number X with name "XXXX" too small

    ORA-01555:snapshot too old: rollback segment number X with name "XXXX" too small 在查询快照的时候 ...

  7. 力扣183(MySQL)-从不订购的客户(简单)

    题目: 某网站包含两个表,Customers 表和 Orders 表.编写一个 SQL 查询,找出所有从不订购任何东西的客户. Customers 表: Orders 表:  解题思路: 需要查询出没 ...

  8. 力扣441(java&python)-排列硬币(简单)

    题目: 你总共有 n 枚硬币,并计划将它们按阶梯状排列.对于一个由 k 行组成的阶梯,其第 i 行必须正好有 i 枚硬币.阶梯的最后一行 可能 是不完整的. 给你一个数字 n ,计算并返回可形成 完整 ...

  9. Flink 助力美团数仓增量生产

    简介: 本文由美团研究员.实时计算负责人鞠大升分享,主要介绍 Flink 助力美团数仓增量生产的应用实践.内容包括:1.数仓增量生产:2.流式数据集成:3.流式数据处理:4.流式 OLAP 应用:5. ...

  10. ClickHouse Keeper 源码解析

    简介:ClickHouse 社区在21.8版本中引入了 ClickHouse Keeper.ClickHouse Keeper 是完全兼容 Zookeeper 协议的分布式协调服务.本文对开源版本 C ...