V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
nu1l
V2EX  ›  Vue.js

请教一个 Vue 动态组件问题

  •  
  •   nu1l · 2023-05-08 16:03:41 +08:00 · 1486 次点击
    这是一个创建于 563 天前的主题,其中的信息可能已经有所发展或是发生改变。

    Vue 项目中,使用 v-for="data in 后台返回的数据"<component :is="data.xxx" :prop1="data.prop1"> 方式动态渲染组件。 更新数据后,is 属性值未改变的组件没有刷新

    案例代码

    目前想到的解决方案是:刷新数据之前,清空之前数据的所有属性,之后在 nextTick() 中重新赋值。 但是这种方式可能导致滚动条位置变动

    想请教下大佬们有没有什么优雅的解决方案?

    7 条回复    2023-05-08 17:27:26 +08:00
    justyeh
        1
    justyeh  
       2023-05-08 16:16:38 +08:00   ❤️ 1
    props 直接使用,不要再 ref 一遍
    justyeh
        2
    justyeh  
       2023-05-08 16:17:13 +08:00   ❤️ 1
    https://play.vuejs.org/#__DEV__eNrtV1tPG0cU/isjv8QgZy1Q+2IZ0kjtQyulqZRKfcA8ON4x2QK7q901DbIsuSmBmhgHNSlQc2lJ0pCqXAsJxubyY7qzaz/lL/TMzF7tUFxQW1WqkbDPmXOb75w5cyYfuamqwkQORxKRpJ7RJNVAOjZy6mBKRkgaVxXNQHmk4WwMKfItJScbWIwhGd83Ppcyo6iAspoyjq6BhWsBjS8UbRRrzpoQ5yR1ExS6Y+RELBuelEM7YlQwo8i6gcS0kUYDNIZovtBD+eG1PljMUxZCI5qSUxMuhZCI9UwCpSJk66X99MdUJOYu5HSs6Qk05NLI16EfY1LFVI8HHtBjRsFnwAf/yOlxpkGOfzJrJdBgQfqf9Ags978X48EH+KKoYT0cihMQykjGJLNZXjTrm9SmbmgYG5Rn1st2ZScVQYV2T52a/e9SBSYqhDWHQ3QhsOl3guPkq32vHjwpw/9z4bFWK2R5OaxC1zk873ewu0DHrM1arw/DW2w1pqzXLxk6Fym2gcM1LwbHJYb5D0azf+Ha7O+iNg9/Nuv1v682ffDtyiOz/v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ogSnQocIrGtgPmzHyeXepAA8Id8jgXE515uyv5eAfA9cpmY+7wNh/h3ZxHthY/v95+HfOQ+EPl+vLSg==
    justyeh
        3
    justyeh  
       2023-05-08 16:23:56 +08:00   ❤️ 1
    wangtian2020
        4
    wangtian2020  
       2023-05-08 16:25:37 +08:00   ❤️ 1
    https://play.vuejs.org/#__DEV__eNrtV1tPG0cU/isjv8QgZy1Q+2IZ0kjtQyulqZRKfcA8ON4x2QK7q901DbIsuSmBmhgHNSlQc2lJ0pCqXAsJxubyY7qzaz/lL/TMzF7tUFNQW1WqkbDPmXOb75w5cyYfuamqwkQORxKRpJ7RJNVAOjZy6mBKRkgaVxXNQHmk4WwMKfItJScbWIwhGd83Ppcyo6iAspoyjq6BhWsBjS8UbRRrzpoQ5yR1ExS6Y+RELBuelEM7YlQwo8i6gcS0kUYDNIZovtBD+eG1PljMUxZCI5qSUxMuhZCI9UwCpSJk66X99MdUJOYu5HSs6Qk05NLI16EfY1LFVI8HHtBjRsFnwAf/yOlxpkGOfzJrJdBgQfqf9Ags978X48EH+KKoYT0cihMQykjGJLNZXjTrm9SmbmgYG5Rn1st2ZScVQYV2T52a/e9SBSYqhDWHQ3QhsOl3guPkq32vHjwpw/9z4bFWK2R5OaxC1zk873ewL4COWZu1Xh+Gt9hqTFmvXzJ0uim2gcM1u4PjEsP8B6PZv3Bt9l+gNg9/Nuv1v682ffDtyiOz/vSc2uyD2rxEaR6dkcO1MISc19cFfS7Vhr7HvGJpXuLcbv1AHm6dh02oMC+KjXW4R1b2wxvkvG7YcKk2bDzmVSrT6+HRaA8aGHTRoNAIE+mxHIZ6/eTO7U8FNa3pOMp+QgiSPCJlJ6Os2/awFuw34nhvLzPSi8juNG/Wze1T+2TbrNX5GeDLcf9k5FTg4w95X79sIP08EBoEbRVx8F9pWJWStf+qNTNL5ivW1nNSq3GZ23e/xBlDGMWTOlPmPnqErKJ9lM7ci47SGEQ8hg0ciGFodNjx4V54YdguFy+HjiMSSk5XJPs6kIQixMYVgXQy+p8B0ov3XCA5Nl9J4gg2bqVVvwc7V5Zz9JwuwTWTcT7+wOADhIHH1TGoUTYGJe/1s2+EriP7wRGZaTgVjKxyqfXdNkD09rhsb26ataK1um59v2vNbVu/rVsrpeazX63lA2th9/fiA7IMo0Gdk6Q0B1p2Y8psvLH3GmTtkb1cI/Nl7iZ5V/MdBmRaxWrzbIZjxa4Tgd0VQ8MCbXyIfPvCWtjqNMFd8rDAMcRq1R7av9TJzlFzb92uTlmlIsRK9tas4gZ5vGPWDqzFN82N52R6v7W43Xq2BCrmybS1tENOnnA7zdNvyIsqiFFrjSqZfQU7N0/maLUeV8njJVJeCEeSjDswJkVpwomNbwuacp7u6Qbf1A2BXo0Fls1k3BX2zAADTVyHehtIRaJ0/zEkySK+3wNfHdBAt0xArYIoJQXa7VMRx3kyo8AgKtMRFCUkHWS8ihli0hTUYWpBkrOKa4J6ABPJuKftmvP26YecMwxFRh9kxqDiqQGv73lBcBbX4+K0AI0OVe+ge5qM06aYjHuFG4m5A5o73gdqGhz4SUjqaloe5COyebZufb0DZ4GynGVfEj5k4wmZn6MZA7izCkMU8Vzx9aOD1unU2+OqKwAXqLfuQwO/b/Ir1EOXS3MmA9gRCIEa2CAQ5z1YXNPuy8Kh2x8geRFnJRl/pimqHqPvi/bXjN9LVCoDfSSgEXV6Co074fTF8M3IVem683xhVgTKABmv4UCmAgH+WabIyi5ZLfpp8c4BwEar30HvY3DgFz4wBTpYeIUDgbE5I593lwrwiHAHDc7lVGfe/loe/hmAA7vuhrP/EL3AgWBz+f8H4l84EIU/ANuJzJY=

    原因,你 :key="user.name" 访问的啥自己看看吧
    wangtian2020
        5
    wangtian2020  
       2023-05-08 16:26:24 +08:00   ❤️ 1
    @wangtian2020 我复制错网址了

    https://play.vuejs.org/#__DEV__eNrtV1tPG0cU/isjv9ggZy3c9sUypJHah1ZKUymV+oB5cLxjsgV2V7trGmRZclMCNTEOalKg5tKSpCFVuRYSjM3lx3RnbT/lL/TMzN58CQSKWl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WYvb5Pgx99M4+Z48L4Ma9VYrk5mXAIF5PEv5elQmjxZJcb49oHjERjQuSuMDfGdQhrNsX9f5xq4L9KJEuVw8QpWYtucARGj8GjCuPxEIUQjCSJJFfK8HHh3oJAIoBmwFVToUJDERsHGJpxToVWXapcYkHRRc0gwyVQrqEDWX5LTi2FP34CEecY0db74NejFnDEOR0cepUaA8deEWO/DBB/EIV+pu4J5t0GfvPvV4xEfUQDjga7Fpu++bTBgMaztQXU3KA7xnNk/XrO+2gf1UZE97mvAj64/J3CxLD0VBoNcjpMWbP9xvnky+OSo7CnBFuvMeEPB+g9+SLppcmwsZoLZCC4QtW3zrB4zj2vnWsMftnyRZEaclGX+pKaoept8b7d83XvlQqQ6UDp9FyC4jNO6YXQadq89vSuftzxnmRaAC0HFLDGTKF+BZmSLLO2Ql76XFJT3ARqluo/cZLOCxHIQCbSFcmkNgrKPIZp2pXM5rKriUjzrzdrE8/DsA+3Z9Hs7ep+k7HAjWkr8/EP/Bgcj9DdbDvy8=
    mgzu
        6
    mgzu  
       2023-05-08 16:42:05 +08:00   ❤️ 1
    可以看 vue devtool 排查,vue devtool 里 props 值跟着变了,info 还没变。
    1. 直接用 props.info
    2. const info = ref(props.info) 改为 const info = computed(() => props.info)
    nu1l
        7
    nu1l  
    OP
       2023-05-08 17:27:26 +08:00
    @justyeh #1
    @wangtian2020 #4
    @mgzu #6

    谢谢大佬们,问题解决了。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   3049 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 18ms · UTC 12:59 · PVG 20:59 · LAX 04:59 · JFK 07:59
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.