【问题标题】:Why this returns an empty array?为什么这会返回一个空数组?
【发布时间】:2018-08-08 13:34:35
【问题描述】:

我有许多组件的父组件,我通过单击元素在它们之间切换。这些文件是 Vue 文件。

我想要的是在使用组件时让它们处于活动状态。

例如:

当我从导航栏中单击主页时,组件会切换到主页的组件(已经完成)并使其具有边框,以便将其标记为活动组件(有人说是活动选项卡)

我想通过以下步骤做到这一点:

首先)我得到所有元素的集合

第二)我通过使用过滤器保留具有 class= 当前数据(这是我用来在它们之间切换的数据)的元素。应该只有一项是活动的。

第三)将 CSS 样式应用于该元素...等

这是父组件的代码(顺便说一句,我削减了 CSS 样式以使其更短):

<template>
  <div id="grid">
    <nav id="navbar">

      <ul id="nav">
        <a href="#" class="Home" @click="current = 'Home'" ><li>{{navbar.Home}}</li></a>        
        <a href="#" class="Reservation" @click="current = 'Reservation'" ><li>{{navbar.Reservation}}</li></a>
        <a href="#" class="About-us" @click="current = 'About-us'" ><li>{{navbar.About}}</li></a>
        <a href="#" class="Contact" @click="current = 'Contact'" ><li>{{navbar.Contact}}</li></a>
      </ul>

      <div class="button"> <!-- Make some animation of this button becomes an extendable window of singing up. Don't forget  -->
        <a href="#">Sign Up
        </a>
      </div>
      <img src="https://i.pinimg.com/564x/8b/fa/5d/8bfa5d6a52a03e83b995fec69a4d8c2c.jpg" alt="" id="logo">
    </nav>      

    <main id="content"> 
      <keep-alive>
        <transition name="component-fade" mode="out-in">
          <component v-bind:is="current"></component>    
        </transition>      
      </keep-alive>        
    </main>      

    <footer>
      <p>Copyright © All Rights Reserved</p>
    </footer>
  </div>
</template>

<script>
import Home from "./components/Home.vue";
import Aboutus from "./components/About us.vue";
import Contact from "./components/Contact.vue";
import Reservation from "./components/Reservation.vue";
import Signup from "./components/Signup.vue";

export default {
  components: {
    Home: Home,
    "About-us": Aboutus,
    Contact: Contact,
    Reservation: Reservation,
    Signup: Signup
  },
  data() {
    return {
      navbar: {
        Home: "Home",
        Reservation: "Reservation",
        About: "About us",
        Contact: "Contact"
      },
      current: "Home"
    };
  },
  methods: {}
};
let nodlist = document.getElementsByTagName("a");
console.log(nodlist);
let active = Array.from(nodlist).filter(
  element => element.className == this.data.current
);
console.log(active);
active.cssText = "border-bottom: 5px solid #5fb0e4;";
</script>

问题是数组返回空并且样式不适用。有什么帮助吗?

【问题讨论】:

  • 你确定一切都在页面加载时初始化了吗?尝试调用mounted() 生命周期钩子中的函数。 vuejs.org/v2/guide/instance.html
  • filter函数内添加console.log(element.className),看看值是多少。
  • @Devilscomrade 我在挂载中尝试过,但没有任何效果。另外我确定一切都已初始化,因为 console.log(nodlist) 返回一个实际元素数组
  • @Programmer 它没有显示任何东西。实际上,console.log(element) 什么也没显示。
  • 如果把filter中的条件改成element.className != this.data.current,得到所有元素了吗?

标签: javascript html css vue.js


【解决方案1】:

同样的问题。使用 Vue 模板和脚本,您正在编写虚拟 DOM。 Vue 只是无法按您期望的方式工作。试试这个:

...
export default {
  ...
  data() {
    return {
      navbar: {
        Home: "Home",
        Reservation: "Reservation",
        About: "About us",
        Contact: "Contact"
      },
      current: "Home"
    };
  },
  methods: {},
  // this method, lifecycle hook, is executed
  // after the Browser DOM is patched with
  // Virtual DOM you composed from template
  mounted () {
    let nodlist = document.getElementsByTagName("a");
    console.log(nodlist);
    let active = Array.from(nodlist).filter(
      element => element.className == this.data.current
    );
    console.log(active);
    active.cssText = "border-bottom: 5px solid #5fb0e4;";
  }
};

现在您的数组将不为空。但是你实现了……什么都没有。 Vue 不知道您更改了浏览器 DOM,并且您的更改将在下一次 DOM 修补时丢失。对不起兄弟,你只是不明白 Vue 的想法。你必须了解更多关于 Vue 的原则。

【讨论】:

  • 这和虚拟DOM有什么关系?它不起作用的原因是因为它返回一个空数组,这意味着没有找到元素,我后来找到了一个修复它但它不起作用,因为每次我按下某个东西时都会保留样式所以它不会不像我想要的那样工作。 Vue 仍然是 Javascript 和 DOM。两者没有什么不同。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-26
  • 1970-01-01
  • 2022-12-03
  • 1970-01-01
  • 2021-11-12
  • 1970-01-01
相关资源
最近更新 更多