vue3+TS使用component 组件的实例

目录

一.项目中实际使用

1.代码使用

二.元素

1.使用方式:

2.代码解释


一.项目中实际使用

1.代码使用

之前,我们使用过,在login相关的几个页面使用component 实现登录/注册/找回密码等页面的动态切换,在天转到这个页面时,传递一个参数,根据参数去显示不同的页面,(当然,你也可以选择将这些页面都加入到路由里,然后设置不同的跳转路径也是可以的)

<template>
  <div class="loginCenter">
    <div class="left">
      <ImageShow></ImageShow>
    </div>

    <div class="right">
      <component :is="componentToShow"></component>
    </div>

  </div>
</template>

<script setup lang="ts">
import ImageShow from "./components/imageShow.vue"
import Login from "./components/login.vue"
import Register from "./components/register.vue"
import FindPasswaod from "./components/findPasswaod.vue"
import { useRoute } from "vue-router";
import {  ref, watch } from "vue";

const judgePath = (path: any) => {
  if (path === 'login') {
    return Login
  } else if (path === 'register') {
    return Register
  } else if (path === 'findPassword') {
    return FindPasswaod
  }
}

const route = useRoute()
const componentToShow = ref(judgePath(route.query.path)) // 使用 ref 进行响应式声明
watch(
  () => route.query.path,
  (newPath: any, old: any) => {
    // 当 route.query.path 发生变化时触发
    componentToShow.value = judgePath(newPath)
  }
);


</script>

 

二.<component>元素

1.使用方式:

<component> 是Vue.js提供的一个特殊元素,它允许你根据变量或表达式的值来动态渲染不同的组件。

<component :is="componentToShow"></component>

:is 属性:通过 :is 属性绑定一个变量,这个变量的值将决定要渲染的组件。 

2.代码解释

在上述的代码中,componentToShow 是一个响应式变量,它的值由路由参数 route.query.path 决定。通过这种设置,你可以动态地选择要渲染的组件。

const componentToShow = ref(judgePath(route.query.path)) // 使用 ref 进行响应式声明

watch(
  () => route.query.path,
  (newPath: any, old: any) => {
    // 当 route.query.path 发生变化时触发
    componentToShow.value = judgePath(newPath)
  }
);
  • ref: 使用 ref 创建一个响应式变量,确保当 componentToShow 的值改变时,视图能够及时更新。
  • watch: 监听 route.query.path 的变化,一旦路径发生变化,执行回调函数,将新的组件赋给 componentToShow,触发动态组件的重新加载。

通过这种方式,可以根据路由参数动态地切换和加载不同的组件,使应用更具有灵活性和可扩展性。这对于处理不同页面或用户操作需要显示不同内容的情况非常有用。

相关推荐

  1. vue3+TS使用component 实例

    2024-01-13 12:46:02       31 阅读
  2. Vue 3 Teleport 实现跨层级通信

    2024-01-13 12:46:02       6 阅读
  3. 使用sass开发web-components

    2024-01-13 12:46:02       26 阅读
  4. Vue Camera使用方法

    2024-01-13 12:46:02       42 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-01-13 12:46:02       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-01-13 12:46:02       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-01-13 12:46:02       19 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-01-13 12:46:02       20 阅读

热门阅读

  1. 多线程面试题目(1)

    2024-01-13 12:46:02       35 阅读
  2. K8S的搭建步骤

    2024-01-13 12:46:02       39 阅读
  3. 单片机学习记录(一)

    2024-01-13 12:46:02       32 阅读
  4. Spring整理-Spring Bean的作用域

    2024-01-13 12:46:02       34 阅读
  5. PyTorch核心--tensor 张量 !!

    2024-01-13 12:46:02       31 阅读
  6. AOSP 编译

    2024-01-13 12:46:02       33 阅读
  7. Spring整理-Spring Bean的生命周期

    2024-01-13 12:46:02       35 阅读
  8. Xbox无法登陆解决方式

    2024-01-13 12:46:02       117 阅读
  9. 【Node.js学习 day5——包管理工具】

    2024-01-13 12:46:02       35 阅读
  10. 正则表达式

    2024-01-13 12:46:02       23 阅读
  11. 浅谈Vue中的NextTick。

    2024-01-13 12:46:02       33 阅读
  12. 前端系列:正则表达式RegExp详解

    2024-01-13 12:46:02       30 阅读