0

I know this is a very silly question but im new to vuejs and i would like to know how to convert this simple php code to vuejs

<div>
<?php
 foreach( $array as $key => $value){
  if($key == 0){
    echo '<div>'.$key.'</div>';
  }else{
   echo '<div>'.$value.'</div>';
  }
}
?>
<div>

it should go something like this :

<div>
 <div v-for="(item, key) in items" v-if="key == 0">{{key}}</div>
 <div v-else>{{item}}</div>
</div>
1
  • The condition in the for loop would break your else condition, try <div v-for="(item, key) in items"><div v-if="key == 0">{{key}}</div> <div v-else>{{item}}</div></div> Commented Jul 7, 2017 at 14:37

1 Answer 1

1

Try this:

<template v-for="(item, index) in items">
  <div v-if="index === 0">{{index}}</div>
  <div v-else>{{item}}</div>
</template>

JSFiddle

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

1 Comment

Just fyi, this uses Vue version 1. In Vue version 2 (which I think the asker is using), the syntax would need to be v-for="(item, index) in items".

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.