javascript - Vue 3 不更新内联样式

标签 javascript html vue.js vuejs3

我为 vue 3 设置了一些演示代码,用于将内联样式更新为计算对象,但它没有更新。

<!DOCTYPE html>
<html>
    <head>
        <title>Demo</title>
        <link rel="stylesheet" href="main.css">
        <script src="https://cdn.jsdelivr.net/npm/<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="afd9dacaef9c819d819d99" rel="noreferrer noopener nofollow">[email protected]</a>/dist/vue.global.prod.min.js"></script>
    </head>

    <body>
        <div id="map">
            <div id="infantry" class="unit" :style="style">
                Position: {{ x }} {{ y }}
            </div>  
        </div>
    
        <script>
            const Infantry = {
                data() {
                    return {
                        x: 0,
                        y: 0
                    }
                },
                mounted() {
                    setInterval(() => {
                        this.x++;
                        this.y++;
                    }, 1000);
                },
                computed : {
                    style() {
                        return {
                            top : this.x
                        };
                    }
                }
            }

            Vue.createApp(Infantry).mount('#infantry');
        </script>
    </body>
</html> 

这部分不起作用

:style="style"

我检查了 dom,它没有设置使用 top 的样式。有谁知道出了什么问题吗?

最佳答案

问题是您绑定(bind)了挂载 vue 应用程序的元素的属性,然后将 px 添加到返回的顶部值:

.unit {
  position: absolute
}
<!DOCTYPE html>
<html>

<head>
  <title>Demo</title>
  <link rel="stylesheet" href="main.css">
  <script src="https://cdn.jsdelivr.net/npm/<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="ccbab9a98cffe2fee2fefa" rel="noreferrer noopener nofollow">[email protected]</a>/dist/vue.global.prod.min.js"></script>
</head>

<body>
  <div id="map">
    <div class="unit" :style="style">
      Position: {{ x }} {{ y }}
    </div>
  </div>

  <script>
    const Infantry = {
      data() {
        return {
          x: 0,
          y: 0
        }
      },
      mounted() {
        setInterval(() => {
          this.x++;
          this.y++;
        }, 1000);
      },
      computed: {
        style() {
          return {
            top: this.x + "px"
          };
        }
      }
    }

    Vue.createApp(Infantry).mount('#map');
  </script>
</body>

</html>

关于javascript - Vue 3 不更新内联样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70536422/

相关文章:

vue.js - "Unknown custom element: <router-link>"升级到Vue 2

javascript - 对数组进行排序并将信息传递到 View 的可靠方法

javascript - 如何隐藏/显示在 ASP.NET 中动态创建的 HTML 元素

javascript - 使用 Vuefire 检索 Vuejs 中的 Firebase 数据

javascript - 使动态创建的下拉菜单与初始下拉菜单中的 Vue 模型中的值匹配

javascript - 更改输入微调器编号并随之更改输入值

php - 如何在 PHP 中实现事件驱动代码?

javascript - polymer + Firebase (Polymerfire) : Cannot read property 'push' of undefined': 'this.$.query.ref.push(...)'

javascript - 将对象插入数组 CSS 的问题 | JS | HTML

html - 如何将动画添加到输入占位符