Use props to pass parameters between routes

What was used before 通过$route.params.X传递参数, this time using props, the method is divided into two steps.

① Enable parameters to be passed in index.js

  {
    
    
    // 注意参数后面有一个冒号
    path: '/user/:username/:id/',
    component: () => import('../views/UserView.vue'),
    props: true, //传参用
  },

②Receive in specific routes

<template>
    <div>
        <h3>个人中心</h3>
        <p>通过$route.params.X传递参数</p>
        {
   
   { $route.params.username }} --- {
   
   { $route.params.id }}
        <hr>
        <p>通过props传递参数</p>
        {
   
   { username }} === {
   
   { id }}
    </div>
</template>

<script>
export default {
      
      
    created(){
      
      
    
    },
    props: ["username", "id"]
}
</script>

<style>

</style>

Insert image description here

Guess you like

Origin blog.csdn.net/weixin_44239550/article/details/128665778