0

I'm trying to create buttons dynamically whose names are fetched from keys of an Object. And I need to pass the corresponding value to the method to get my code work. How do you do both of these tasks?


<template>
  <v-app>
    <router-view></router-view>
    <v-app-bar app dense id="abc" dark>
      <v-icon>note_add</v-icon>
      <v-toolbar-title>Title</v-toolbar-title>
      <v-spacer></v-spacer>
      <v-btn
        v-for="(value, key) in button_redirect_to"
        :key="key"
        @click="render_comp()"
      >{{ key }}</v-btn>
      <v-btn depressed fab small>
        <v-icon>notifications</v-icon>
      </v-btn>
    </v-app-bar>
  </v-app>
</template>

<script>
export default {
  name: "Navbar",
  data() {
    return {
      button_redirect_to: {
        Projects: "/projects",
        Requests: "/requests",
        Reports: "/reports",
        Resources: "/resources",
        temp: "/temp"
      },
      pers_actions: ["Profile", "LogOut"]
    };
  },
  methods: {
    render_comp() {
      this.$router.push();
    }
  }
};
</script>




2 Answers 2

1

In your for-loop, value is the route and key is the button label. Just pass the route (value) as an argument:

@click="render_comp(value)"
methods: {
  render_comp(to) {
    this.$router.push(to);
  }
}
Sign up to request clarification or add additional context in comments.

Comments

0

Pass the value variable from the for loop (which in the first case will be "/projects") as an argument of @click="render_comp(value)" handler so it can be used inside the function.

<v-btn
    v-for="(value, key) in button_redirect_to"
    :key="key"
    @click="render_comp(value)"
>
    {{ key }}
</v-btn>

render_comp will then have access to it so it can be passed into this.$router.push(), making the function this.$router.push("/projects").

methods: {
    render_comp(value) {
        this.$router.push(value);
    }
}

Hope this helps.

2 Comments

Yes, no problem. You're passing the value variable from the for loop (which in the first case will be "projects") as a parameter of your render_comp function so you can use it inside the function. render_comp will then have access to it so it can be passed into this.$router.push().
Please edit your answer to contain such information - don't use the comment section for this

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.