美文网首页
vue-router路由传参

vue-router路由传参

作者: 湘兰沅芷 | 来源:发表于2020-03-03 15:26 被阅读0次

tip: 用params传参,F5强制刷新参数会被清空,用query,由于参数适用路径传参的所以F5强制刷新也不会被清空。(传参强烈建议适用string)
也可以选用sessionstorage/localstorage/cookie存储

1.params:参数不会显示到路径上
配置路径rutes

export default new Router({
  routes: [
    {
       path: '/path1',
       name: 'path1',//一定要写name,params必须用name来识别路径
       component: Path1
    }
  ]
})

2.传递参数:用$router

<!-- test-vue-router页面 -->
<template>
  <div>
    <a @click="routerTo()">query传参</a>
  </div>
</template>
<script>
export default {
  methods: {
    routerTo() {
      this.$router.push({
        name: `TestVueRouterTo`,
        params: {
          page: '1', code: '8989'
        }
      })
    }
  }
}
</script>

3.接受参数: 用$route

<!-- test-vue-router-to页面 -->
<template>
  <div>
  </div>
</template>
<script>
export default{
  data() {
    return {
      page: '',
      code: ''
    }
  },
  created() {
    this.getRouterData()
  },
  methods: {
    getRouterData() {
      this.page = this.$route.params.page
      this.code = this.$route.params.code
      console.log('page', this.page)
      console.log('code', this.code)
    }

  }
}
</script>

query: 最好也用name来识别,保持与params一致性,路径传参
1.路径配置(跟params一样)
2.参数传递页

<!-- test-vue-router页面 -->
<template>
  <div>
    <a @click="routerTo()">query传参</a>
  </div>
</template>
<script>
export default {
  methods: {
    routerTo() {
      this.$router.push({
        name: `TestVueRouterTo`,
     // 只是把query改了,其他都没变
        query: {
          page: '1', code: '8989'
        }
      })
    }
  }
}
</script>

3.接受参数

<!-- test-vue-router-to页面 -->
<template>
  <div>
  </div>
</template>
<script>
export default{
  data() {
    return {
      page: '',
      code: ''
    }
  },
  created() {
    this.getRouterData()
  },
  methods: {
    getRouterData() {
      // 只是改了query,其他都不变
      this.page = this.$route.query.page
      this.code = this.$route.query.code
      console.log('page', this.page)
      console.log('code', this.code)
    }

  }
}
</script>

相关文章

  • 路由的传参

    路由的传参 下载:npm install vue-router路由的传参:1.查询字符串:/user/regist...

  • 路由的传参 axios

    1,路由的传参:案例: 2,axios下载:npm install vue-router路由的传参:1.查询字符串...

  • vue 路由的传参 vue中的ajax (axios)

    1.路由的传参 下载:npm install vue-router路由的传参:1.查询字符串:/user/regi...

  • 简单遍历vue2.0文档(五)

    11. vue-router 11.1 引用 11.2 配置路由文件 11.3 路由的跳转 11.4 路由的传参 ...

  • vue路由传参的方法

    vue-router路由传参 例一:query传参 //给需要操作的标签添加点击事件 在methods(方法)里...

  • vue笔记9.26

    下载:npm install vue-router路由的传参:1.查询字符串:/user/regist?uname...

  • vue路由器传参

    下载:npm install vue-router路由的传参:1.查询字符串:/user/regist?uname...

  • VUE路由和axios

    下载:npm install vue-router路由的传参:1.查询字符串:/user/regist?uname...

  • Vue-router 相关

    一、vue-router 如何传参 1、用name传递参数 步骤: (1)在路由文件src/router/in...

  • Vue实战第二天

    路由组件传参 动态路由传参 静态路由传参 函数传参htm5 history 模式 设置通用路由,找不到页面跳转自定...

网友评论

      本文标题:vue-router路由传参

      本文链接:https://www.haomeiwen.com/subject/ehfhlhtx.html