VUE-路由配置及跳转方式

摘要:
在vue中,路由路由器通常用于其他页面跳转和卡交换。具体来说,它主要记录了路线的配置和几种常见的跳转方法。路由配置重定向索引。js:importHomefrom'../components/Home'importPage2from'../components/Page2'importChil1 from'./components/Child1'importChil2 from'

需求

在vue中,页面的他跳转和卡片切换一般会用到路由router,具体不多说,主要记录一下路由的配置和几种常用的跳转方式

路由配置重定向 index.js :

import Home from '../components/Home'
import Page2 from '../components/Page2'
import Chil1 from '../components/Chil1'
import Chil2 from '../components/Chil2'

Vue.use(Router)
export default new Router({
routes: [
    {
      path: '/home',
      name: 'Home',	//可以不用name字段   但有时需要用到name匹配
      component: Home
    } ,
    {
      path: '/page2',
      name: 'Page2',
      component: Page2,
      children:[		//页内切换卡片
                {
                   path: '/chil1',	// 带‘/’ 表示根路径
                   name: 'Chil1',
                   component: Chil1
                },
                {
                    path: 'chil2',	// 不带‘/’ 表示访问路径为/page2/chil2
                    name: 'Chil2',
                    component: Chil2
                }
            ]     
    },
    {
      path: '/',	//匹配根路径 用于默认打开的页面
      name: 'index',
      redirect: '/home'  //redirect重定向到Home组件
    }
  ]  
})

路由跳转的几种方式

一、使用标签路由 router-link

1、不传参

    <router-link :to="{name:'Home'}"> 
    <router-link :to="{path:'/home'}">

2、传参

  <router-link :to="{name:'Home', params: {id:1}}">
  <router-link :to="{path:'/home', params: {id:1}}"> 
  // params传参数
  // 路由配置 path: "/home/:id"
  // 不配置path ,第一次可请求,刷新页面id会消失
  // 配置path,刷新页面id会保留
  // html 取参 $route.params.id
  // script 取参 this.$route.params.id

  <router-link :to="{name:'Home', query: {id:1}}">
  // query传参数 (类似get,页面url后面会显示参数)
  // 路由可不配置
  // html 取参 $route.query.id
  // script 取参 this.$route.query.id

二、编程式路由 this.$router.push()

1、不传参
  this.$router.push('/home')
  this.$router.push({name:'Home'})
  this.$router.push({path:'/home'})

2、传参
  this.$router.push({name:'home',params: {id:'1'}})  // 只能用 name
  // params传参数
  // 路由配置 path: "/home/:id"
  // 不配置path ,第一次可请求,刷新页面id会消失
  // 配置path,刷新页面id会保留
  // html 取参 $route.params.id
  // script 取参 this.$route.params.id

   this.$router.push({name:'Home',query: {id:'1'}})
   this.$router.push({path:'/home',query: {id:'1'}})
  // query传参数 (类似get,页面url后面会显示参数)
  // 路由可不配置
  // html 取参 $route.query.id
  // script 取参 this.$route.query.id

在axios中使用 this.$router.push() 时,如果使用function会取不到this而报错,而要使用箭头函数

如下:

onSubmit() {
      axios.post('http://localhost:8022/user/login',qs.stringify(this.user))
      .then((response)=>{	//正确;	使用function(response){}会报错。。。我也不知为啥。。
        alert(response.data.msg);
        if (response.data.url){
          this.$router.push({path: '/home'})
        }
      })
      .catch((error) => alert(error))

    }

欢迎访问个人博客:http://www.itle.info/

免责声明:文章转载自《VUE-路由配置及跳转方式》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇SAP常见问题与解决方法ORACLE 字符串补零下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

实现一个竖直的显示表头的表格(vue版本)

今天遇到一个问题,实现这样一个竖直的显示表头的表格,如下图。默认显示两列。 vue实现代码如下:   tableComponent.vue:   <template> <table class="mailTable" :style="styleObject" v-if="s_showByRow"> <tr v-...

BGP RR和联邦

    由于IBGP的水平分割问题,所以IBGP需要Full Mesh。由于整个IBGP full mesh的话,需要建的session数为n*(n-1)/2。不具有扩展性。所以产生两种解决方法,路由反射器是其中一种,而另一种则是联邦。 反射器/RR 从EBGP邻居学习过来的路由(或RR本地注入的路由)RR不会创建cluster-list,这时候的环路避免...

vue中的watch监听数据变化以及watch中各属性详解

1、watch使用的几种方法(1)通过watch监听data数据的变化,数据发生变化时,就会打印当前的值 watch: { data(val, newval) { console.log(val) console.log(newval) } } (2)通过watch监听docData数据的变化...

koa 搭建模块化路由/层级路由

搭建node项目目录以及基本的文件 初始化package.json文件 执行下面命令生成package.json文件 npm init --yes 创建项目目录 创建路由目录routes,存放静态资源目录public,视图目录views 安装项目所需的依赖 "dependencies": { "art-template": "^4.12.2",...

简单使用Vuex步骤及注意事项

使用Vuex的步骤: (1)安装: 1.使用npm安装: npm install vuex --save 2.使用script标签引入 <script src="http://t.zoukankan.com/path/to/vue.js"></script> <script src="http://t.zoukankan...

----Vue 中mixin 的用法详解----

说下我对vue中mixin的一点理解   vue中提供了一种混合机制--mixins,用来更高效的实现组件内容的复用。最开始我一度认为这个和组件好像没啥区别。。后来发现错了。下面我们来看看mixins和普通情况下引入组件有什么区别?      组件在引用之后相当于在父组件内开辟了一块单独的空间,来根据父组件props过来的值进行相应的操作,单本质上两者还是...