1

I am using v-for loop to show pagination links -

<div v-for="n in this.totalPages">
   <router-link :to="'/top/pages/' + n" @click.native="getPaginatedUser">{{ n }}</router-link>
</div>

Each link is getting displayed in new line, like below -

1

2

Is there any way i can display them in single line ? like this -

1 2

Thanks.

3
  • 1
    why don't you do it by simple css ? Like set the display property of that div to inline-block ? Commented Aug 14, 2019 at 15:25
  • @SwadhIn thanks for the suggestion but i don't want ot use css for this. Commented Aug 14, 2019 at 15:32
  • 3
    @Derick You can change <div> tag to <span> because <div> by default causes a line break. Commented Aug 14, 2019 at 15:35

4 Answers 4

6

The reason for each link is displayed in new line is because you are iterating a div element which is a block element. You can use an inline element like span or Vue specific template which won't render any additional markup or you can just iterate the router link

<router-link
  :to="'/top/pages/' + n"
  @click.native="getPaginatedUser"
  :key="n"
  v-for="n in this.totalPages"
>{{ n }}</router-link>

Using template instead of div.

<template v-for="n in this.totalPages">
    <router-link :to="'/top/pages/' + n" @click.native="getPaginatedUser" :key="n">{{ n }}</router-link>
</template>
Sign up to request clarification or add additional context in comments.

Comments

2

You can change either the component div to span, or change the style of the div to:

display: inline-block;

Comments

1

just move the v-for to the router-link component:

<div>
   <router-link v-for="n in this.totalPages" :key="n" :to="'/top/pages/' + n" @click.native="getPaginatedUser">{{ n }}</router-link>
</div>

The router-link component will render an <a> tag by default, which you can then style via css to get the layout you want.

Example:

new Vue({
  el: '#root',
  router: new VueRouter()
});
a + a { margin-left: 0.3rem; }
<script src="https://unpkg.com/vue/dist/vue.js"></script>
<script src="https://unpkg.com/vue-router/dist/vue-router.js"></script>
<div id="root">
  <router-link v-for="id in 10" :key="id" :to="'/foo/' + id">{{id}}</router-link>
</div>

Comments

1

Add a class to template/div and style it with:

display: flex;

and v-for the router link instead of the div. It should put your pagination inline!

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.