1

I have this default vue navbar which I want to make it more dynamic by using v-for.

Before changes, this is the code:

<template>
    <div>
        <router-link to="/">Home</router-link> |
        <router-link to="/about">About</router-link>
    </div>
</template>    

After changes, this is the code:

<template>
    <div>
        <div v-for="navbar in navbars" :key="navbar.id">
            <router-link :to="navbar.router">{{ navbar.names }}</router-link>
        </div>
    </div>
</template>

<script>
export default {
  name: "Header",
  navbars: [
    { names: "Home", router: "/" },
    { names: "About", router: "/About" }
  ]
};
</script>

But after converted it, the navbar doesn't show up. Anything I miss out?

1 Answer 1

1

You need to define navbars as a data of the component using data

<template>
    <div>
        <div v-for="navbar in navbars" :key="navbar.id">
            <router-link :to="navbar.router">{{ navbar.names }}</router-link>
        </div>
    </div>
</template>

<script>
export default {
  name: "Header",
  data: function () {
    return {
      navbars: [
        { names: "Home", router: "/" },
        { names: "About", router: "/About" }
      ]
    }
  }
};
</script>
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.