简体   繁体   English

Vue 3 Composition API - 当父项中的道具发生更改时,计算的子组件未更新

[英]Vue 3 Composition API - Computed child component not updated when props has changed in parent

I'm using Nuxt 3 and Vue 3 in my stack.我在堆栈中使用 Nuxt 3 和 Vue 3。 I want my component changed based my parent state value.我希望根据我的父状态值更改我的组件。 but the problem is only my state value changed then the computed in child component is not updating.但问题只是我的状态值改变了,然后子组件中的计算没有更新。 I already try to pass my Props as reactivity in other variable and to move class binding directly in html :class="..." but still not updating.我已经尝试将我的道具作为反应性传递给其他变量,并直接在 html :class="..." 中移动类绑定,但仍然没有更新。

This is my example code:这是我的示例代码:

index.vue - parent component index.vue - 父组件

<script lang="ts" setup>
import FeaturedIcon from "~/components/icons/FeaturedIcon.vue";

const iconReactive = reactive({
  theme: "light",
  size: "md",
  type: "primary",
});
</script>

<template>
  <div>
    <FeaturedIcon
      :theme="iconReactive.theme"
      :size="iconReactive.size"
      :type="iconReactive.type"
    />

    <button @click="iconReactive.theme = 'dark'">Click Me</button>
  </div>
</template>

FeaturedIcon.vue精选图标.vue

<script lang="ts" setup>  
// Props  
import { computed } from "#imports";  
  
interface Props {  
  theme: FeaturedThemeProps;  
  size: FeaturedSizeProps;  
  type: FeaturedTypeProps;  
}  
  
const props = defineProps<Props>();  
  
// * Class state for dynamic styling based on props used  
const defaultClass = {  
  size: {  
  circle: {  
  xs: "h-[24px] w-[24px]",  
      sm: "h-[32px] w-[32px]",  
      md: "h-[40px] w-[40px]",  
      lg: "h-[48px] w-[48px]",  
      xl: "h-[56px] w-[56px]",  
    },  
    icon: {  
  xs: "text-xs",  
      sm: "text-md",  
      md: "text-xl",  
      lg: "text-display-xs",  
      xl: "text-[28px]",  
    },  
  },  
  color: {  
  materialIcon: {  
  primary: "bolt",  
      gray: "bolt",  
      error: "error_outline",  
      warning: "warning_amber",  
      success: "check_circle",  
    },  
  },  
};  
  
const featuredIconClassConfig = {  
  light: {  
  size: {  
  circle: defaultClass.size.circle,  
      icon: defaultClass.size.icon,  
      border: {},  
    },  
    color: {  
  circle: {  
  primary: "bg-primary-100",  
        gray: "bg-gray-100",  
        error: "bg-error-100",  
        warning: "bg-warning-100",  
        success: "bg-success-100",  
      },  
      icon: {  
  primary: "text-primary-700",  
        gray: "text-gray-700",  
        error: "text-error-700",  
        warning: "text-warning-700",  
        success: "text-success-700",  
      },  
      materialIcon: defaultClass.color.materialIcon,  
    },  
  },  
  lightOutline: {  
  size: {  
  circle: defaultClass.size.circle,  
      icon: defaultClass.size.icon,  
      border: {  
  xs: "border-[2px]",  
        sm: "border-[4px]",  
        md: "border-[6px]",  
        lg: "border-[8px]",  
        xl: "border-[10px]",  
      },  
    },  
    color: {  
  circle: {  
  primary: "bg-primary-100 border-primary-50",  
        gray: "bg-gray-100 border-gray-50",  
        error: "bg-error-100 border-error-50",  
        warning: "bg-warning-100 border-warning-50",  
        success: "bg-success-100 border-success-50",  
      },  
      icon: {  
  primary: "text-primary-700",  
        gray: "text-gray-700",  
        error: "text-error-700",  
        warning: "text-warning-700",  
        success: "text-success-700",  
      },  
      materialIcon: defaultClass.color.materialIcon,  
    },  
  },  
  dark: {  
  size: {  
  circle: defaultClass.size.circle,  
      icon: defaultClass.size.icon,  
      border: {  
  xs: "border-[2px]",  
        sm: "border-[4px]",  
        md: "border-[6px]",  
        lg: "border-[8px]",  
        xl: "border-[10px]",  
      },  
    },  
    color: {  
  circle: {  
  primary: "bg-primary-600 border-primary-700",  
        gray: "bg-gray-600 border-gray-700",  
        error: "bg-error-600 border-error-700",  
        warning: "bg-warning-600 border-warning-700",  
        success: "bg-success-600 border-success-700",  
      },  
      icon: {  
        primary: "text-white",  
        gray: "text-white",  
        error: "text-white",  
        warning: "text-white",  
        success: "text-white",  
      },  
      materialIcon: defaultClass.color.materialIcon,  
    },  
  },  
};  
  
const themeSelected = featuredIconClassConfig[props.theme];  
  
const circleDynamicClass = computed(() => {  
  return `${themeSelected.size.circle[props.size]}  
  ${themeSelected.size.border[props.size]}  
  ${themeSelected.color.circle[props.type]}`;  
});  
  
const spanDynamicClass = computed(() => {  
  return `${themeSelected.size.icon[props.size]}  
  ${themeSelected.color.icon[props.type]}`;  
});  
  
const materialIconContent = computed(() => {  
  return `${themeSelected.color.materialIcon[props.type]}`;  
});  
</script>  
  
<template>  
  {{ circleDynamicClass }}  
  <div  
  :class="`flex justify-center items-center rounded-full ${circleDynamicClass}`"  
  >  
 <span :class="`material-icons-outlined inline-block ${spanDynamicClass}`">  
  {{ materialIconContent }}  
    </span>  
 </div>
 </template>
Here before after i change my props value在我更改道具值之前

在此处输入图像描述

在此处输入图像描述

I only need to move themeSelected in function.我只需要在函数中移动themeSelected

const circleDynamicClass = computed(() => {
  const themeSelected = featuredIconClassConfig[props.theme];
  // Do etch
});

Its not remounted when i put it in as outside variable.当我将它作为外部变量放入时,它没有重新安装。 Thanks for the help.谢谢您的帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 道具值没有在vue中将更新的值从父组件发送到子组件 - props value not sending the updated value from parent to child component in vue 当道具从父项更改时,我的子项复选框未更新吗? - My child component checkbox is not updating when props changed from the parent? Vue父组件道具未传递给子组件 - Vue parent component props are not passing to child component Vue组合API调用子组件方法 - Vue composition API calling child component method VueJS-道具不会通过父项的更改在子项中更新 - VueJS - Props are not updated in child component by changes in parent 一旦在子组件中更新了 prop,如何调用函数。 Vue3 组合 API - How to call a function, once a prop is updated in a child component. Vue3 composition API Vue 3:如何使用组合 api 正确更新组件道具值? - Vue 3: How to update component props value correctly using composition api? Vue 3 组合 API:使用 props 作为组件数据的初始值 - Vue 3 Composition API: using props as initial value for component data 通过父组件的setState更改道具时,子组件中的数据列表选项不会更新 - Datalist options in child component won't update when props are changed through parent component's setState 父组件的 state 更新后,子组件中的道具未更新 - props not updating in child component after parent component's state is updated
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM