• computed计算方法不被调用的原因;只有在使用时才会被调用


    目录

    一、问题

    二、解决方法

    三、总结


    一、问题

    1.需求:根据组件外部一个变量的值来确定 组件内部的操作。组件外部可以更改filetime的值,filetime有值时这个界面可以操作,否则不可以操作。

    我一想 用computed就可以了呀,动态计算一下filetime不就可以了。于是写了下面的代码。然而我发现: 外面的filetime变了,computed函数竟然不执行!!!

    还以为写错了,又加了一个watch,打印看到filetime确实变化了,那么computed为什么不执行呢?

    1)代码如下:

    1. <script >
    2. import { defineComponent, watch, computed } from "vue";
    3. // 患者信息
    4. import { usePatientStoreWithOut } from "@/store";
    5. const patientInfo = usePatientStoreWithOut().currentOperaMsg;
    6. export default defineComponent({
    7. setup() {
    8. // 监听 filetime变化
    9. watch(
    10. () => patientInfo.filetime,
    11. (newval, oldval) => {
    12. console.log("watch filetime", patientInfo.filetime);
    13. workDisabled.save = !!newval;
    14. }
    15. );
    16. // filetime变化时计算 patientInfo_filetime
    17. const patientInfo_filetime = computed(() => {
    18. console.log(
    19. "computed filetime",
    20. patientInfo.filetime,
    21. patientInfo_filetime
    22. );
    23. return patientInfo.filetime;
    24. });
    25. },
    26. });
    27. script>

    2)结果:filetime变了,computed里面没有执行

    二、解决方法

    1.看了半天,不知道原因。不是说 computed就是:根据响应式数据来动态把数据处理成想要的值吗?patientInfo是响应式的,也变化了,怎么就不执行computed呢?

    决定把patientInfo_filetime在页面上显示一下,看看到底有没有变化。

    1)代码

    1. <script >
    2. import { defineComponent, watch, computed } from "vue";
    3. // 患者信息
    4. import { usePatientStoreWithOut } from "@/store";
    5. const patientInfo = usePatientStoreWithOut().currentOperaMsg;
    6. export default defineComponent({
    7. setup() {
    8. // 监听 filetime变化
    9. watch(
    10. () => patientInfo.filetime,
    11. (newval, oldval) => {
    12. console.log("watch filetime", patientInfo.filetime);
    13. workDisabled.save = !!newval;
    14. }
    15. );
    16. // filetime变化时计算 patientInfo_filetime
    17. const patientInfo_filetime = computed(() => {
    18. console.log(
    19. "computed filetime",
    20. patientInfo.filetime,
    21. patientInfo_filetime
    22. );
    23. return patientInfo.filetime;
    24. });
    25. },
    26. });
    27. script>

    2)结果:太神奇了,patientInfo_filetime确实变化了,computed函数竟然也执行了!!!

        真是不可思议!!!

    2.经测试发现:computed是不会主动触发的,即使computed依赖的响应式数据变化了。

    只有在中或 函数中 使用 computed属性,computed属性的计算方法才会执行。

    如果你不使用计算属性,即使真的变化了,也不会计算!!!

    3.解决方法

     1)watch监听到filetime时进行逻辑处理

        2)用computed不可行,因为我外部filetime的变化,并不会触发这个页面的函数,我不能在函数中使用computed属性;在页面的template中也确实不需要使用 computed计算出来的属性 

    三、总结

    1.computed方法不被调用的原因:

      1)computed计算方法写的有问题:检查一下代码是否正确

      2)watch监听一下,如果有变化;但是computed没有执行,则很有可能是  没有使用computed计算属性。

    2.watch和computed

      1)发现他们的一个区别是:watch监听到变化就执行computed只有在你使用 computed计算属性时computed方法才会被调用。要根据不同场景判断到底要使用哪个!!!

     2)一般在 中有用到computed计算属性时,才使用computed;其余情况使用watch监听。

    /*

    希望对你有帮助!

    如有错误,欢迎指正,非常感谢!

    */

  • 相关阅读:
    [UNR #6]稳健型选手
    会议项目之审批
    解放计算力:使用并行处理提升python for循环速度
    PPT文件转换成PDF格式
    Java八股文总结(二)
    我对世界最聪明的AI写作家GPT3提问:AI写作的大规模使用会有什么风险?
    Java程序设计——类加载(Java高级应用)
    51Sim核心参编!《中国智能网联汽车自动驾驶仿真测试白皮书》发布(附资源)
    C++-IO相关
    64位整数高低位的数据获取与赋值操作探讨
  • 原文地址:https://blog.csdn.net/qq_45327886/article/details/127960015