zoukankan      html  css  js  c++  java
  • Vue的路由

    路由

    参数传递

    第一种

    router
    {path: '/user/profile/:id', name:'UserProfile', component: UserProfile}
    
    a传递
    <router-link :to="{name: 'UserProfile', params: {id: 1}}">个人信息</router-link>
    或
    <router-link to="/user/info/2">
    
    b接受
    {{$route.params.id }}
    

    第二种

    router
    {path: '/user/profile/:id', name:'UserProfile', component: UserProfile, props: true}
    
    a传递
    <router-link :to="{name: 'UserProfile', params: {id: 1}}">个人信息</router-link>
    或
    <router-link to="/user/info/2">
    
    b接受
    为目标组件增加 props 属性,代码如下:
      export default {
        props: ['id'],
        name: "UserProfile"
      }
    
    模板中使用
    {{ id }}
    

    第三种 编程式导航

    a->b
    rounter
    b:
        {
          // 路由路径
          path: '/main/:username',
          xxx
        }
    
    b接受:
    {{$route.params.username}}
    
    a传递:
    // 使用 vue-router 路由到指定页面,该方式称之为编程式导航
    this.$router.push({name:'Main',params:{username:this.form.username}});
    

    组件重定向

    配置重定向

    修改路由配置
        {
          path: '/main',
          name: 'Main',
          component: Main
        },
        {
          path: '/goHome',
          redirect: '/main'
        }
    
    说明:这里定义了两个路径,一个是 /main ,一个是 /goHome,其中 /goHome 重定向到了 /main 路径,由此可以看出重定向不需要定义组件;

    重定向到组件

    设置对应路径即可
    <router-link to="/goHome">回到首页</router-link>
    

    带参数的重定向

    修改路由配置
    {
      // 首页
      path: '/main/:username',
      name: 'Main',
      component: Main
    },
    {
      path: '/goHome/:username',
      redirect: '/main/:username'
    }
    
    重定向到组件
    <router-link to="/goHome/Lusifer">回到首页</router-link>
    
  • 相关阅读:
    Vue如何下载文件?
    vue用template还是JSX?
    2020年的第一件事,我取关了97个公众号
    月经贴 | 2019.12
    CSS——盒子模型(含详解)
    CSS——字体、文本、背景属性设置
    CSS——选择器(三大特性)
    CSS——选择器(本篇介绍八类多种)
    CSS——简介
    WEB前端——body内常用标签(form标签)
  • 原文地址:https://www.cnblogs.com/faramita/p/11305948.html
Copyright © 2011-2022 走看看