vue 应用:class的元素, 在js中注入classList.add方法被覆盖

请教:
vue 应用:class的元素, 在js中注入classList.add方法的类名被:class覆盖

阅读 4.8k
2 个回答
<template>
  <div
    v-test
    :class="{ test: test > 0.5 }"
    style="width: 100px; height: 100px; background-color: red"
  ></div>
</template>
<script>
export default {
  name: "App",
  mounted() {
    setInterval(() => (this.test = Math.random()), 1000);
  },
  data() {
    return { test: Math.random() };
  },
  directives: {
    test: {
      inserted: function (el) {
        console.log("inserted el", el);
        el.classList.add("test1");
      },
      // 当被绑定的元素插入到 DOM 中时……
      componentUpdated: function (el) {
        console.log("componentUpdated el", el);
        // 聚焦元素
        // el.focus();
        el.classList.add("test1");
      },
    },
  },
};
</script>

hook componentUpdated

不要把数据绑定和操作dom混着用

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题