0

I'm trying to make a dynamic menu at vue, but, while I trying to apply a "active" css class to the menu item, not work, I see the menu variable "menuFomartado" is not updating by VUE, what I thinking wrong?

This code only works while loading page again..

<script>
export default {
  data() {
    return {
      menuFomartado: [],
      menu: [
        {
          icon: "fa fa-chart-area",
          pageName: "dashboard",
          title: "Dashboard",
          role: "admin"
        },
        {
          icon: "fa fa-user-circle",
          pageName: "clientes",
          title: "Clientes",
          role: "admin"
        },
    
      ]
    };
  },
  computed: {
    sideMenu() {
      return this.nestedMenu(this.menu);
    }
  },
  watch: {
    $route() {
      this.menuFomartado = this.sideMenu;
    }
  },
  mounted() {
    this.menuFomartado = this.nestedMenu(this.sideMenu);
  },
  methods: {
    nestedMenu(menu) {
      menu.forEach((item, key) => {
        if (typeof item !== "string" && item.pageName != "") {
          menu[key].active = item.pageName == this.$route.name;
        }
      });

      return menu;
    }
  }
};
</script>

1

1 Answer 1

1

Based in @pierre-said from post Vuejs and Vue.set(), update array

I just changed the line

menu[key].active = item.pageName == this.$route.name;

To

this.$set(menu[key], "active", item.pageName == this.$route.name);

And works fine

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.