Rumah  >  Artikel  >  hujung hadapan web  >  Vue.js的路由参数

Vue.js的路由参数

php中世界最好的语言
php中世界最好的语言asal
2018-03-13 14:38:081435semak imbas

这次给大家带来Vue.js的路由参数,使用Vue.js的路由参数的注意事项有哪些,下面就是实战案例,一起来看一下。

作用:为页面传递路由参数.

在路由映射表中配置需要传递的参数:
比如:apple页面,我需要传递一个color的参数,可以在path中以:开头设置参数.

path: '/apple/:color',

具体使用:

let router = new VRouter({ // 如果mode设为history, 那么地址就可以不使用哈希(# 哈希)了,就可以直接访问. http://localhost:8080/#/apple ==>> http://localhost:8080/apple
  mode: 'history',  routes: [    //  做一个映射表
    {      path: '/apple/:color',      component: Apple
    },
    {      path: '/banana',      component: Banana
    }
  ]
})

当我在页面跳转时,直接在地址后面拼接参数,下面的red,即为传递的color参数

http://localhost:8080/apple/red

在script标签中获取页面传递的参数,通过this.$route.params全局的对象来获取

<template>
  <div class="hello">
    <h1>{{msg}}</h1>
    <button @click="getParams">get params</button>
  </div></template><script>
  export default {
    data () {      return {        msg: &#39;I am componnet apple&#39;
      }
    },    methods: {
      getParams () {        console.log(this.$route.params)
      }
    }
  }</script>

上面的打印结果为:

{color: "red"}

在template标签中使用界面传递过来的参数,可以使用$route.params.color

<template>
  <div class="hello">
    <h1>{{msg}}</h1>
    <p>{{$route.params.color}}</p>
    <button @click="getParams">get params</button>
  </div></template>

1.png

传递多个参数

路由中设置如下:

path: &#39;/apple/:color/detail/:type&#39;,

传递参数的url:

http://localhost:8080/apple/red/detail/3

打印传递过来的所有参数:

{color: "red", type: "3"}

相信看了本文案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!

推荐阅读:

Vue.js的vue标签属性和条件渲染

Vue.js的列表渲染 v-for 数组 对象 子组件

Atas ialah kandungan terperinci Vue.js的路由参数. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Artikel sebelumnya:Vue.js如何设置路由Artikel seterusnya:Vue.js的嵌套路由(子路由)