• Vue中path和component属性


    Vue Router中,path 和 component 是路由配置对象中最重要的两个属性。它们共同定义了路由的匹配规则和当路由被匹配时应该渲染哪个组件。

    path 属性

    作用:path 属性指定了路由的匹配规则,即当用户访问某个URL时,Vue Router会检查这个URL是否与某个路由的path属性相匹配。

    值:path 属性的值通常是一个字符串,表示URL的路径部分。它可以是静态的,也可以是包含动态部分的(通过:来指定动态段)。

    1. {
    2. path: '/user/:id', // 动态路径,包含一个名为id的动态段
    3. // 其他配置...
    4. }

    component属性

    作用:component 属性指定了当路由被匹配时应该渲染哪个Vue组件。

    值:component 属性的值通常是一个Vue组件的构造函数或者是一个通过import导入的组件对象。

    1. {
    2. path: '/user/:id',
    3. component: UserProfile // 假设UserProfile是一个Vue组件
    4. // 其他配置...
    5. }

    完整事例:

    1. import Vue from 'vue';
    2. import Router from 'vue-router';
    3. import Home from '@/components/Home.vue';
    4. import UserProfile from '@/components/UserProfile.vue';
    5. Vue.use(Router);
    6. export default new Router({
    7. routes: [
    8. {
    9. path: '/',
    10. name: 'Home',
    11. component: Home
    12. },
    13. {
    14. path: '/user/:id',
    15. name: 'UserProfile',
    16. component: UserProfile
    17. }
    18. ]
    19. });

    在这个示例中,我们定义了两个路由:一个是根路径/,当用户访问这个路径时,会渲染Home组件;另一个是/user/:id,这是一个动态路径,当用户访问这个路径时(例如/user/123),会渲染UserProfile组件,并且可以通过this.$route.params.idUserProfile组件中访问到动态段id的值。

  • 相关阅读:
    创作纪念日-我的第1024天
    redis主从复制原理
    margin的特性和巧妙用法
    Ultra-Light-Fast-Generic-Face-Detector-1MB-master人脸检测算法的复现过程记录
    leetcode每日一题-周复盘
    NPDP在国内的含金量
    Yum安装JDK11
    Nginx的优化和防盗链
    ChatGPT带你飞,送3本《巧用ChatGPT快速搞定数据分析》
    人体的神经元有多少个,人体的神经元有多少支
  • 原文地址:https://blog.csdn.net/m0_75068951/article/details/143403674