1

I want to pass a php array to a vuejs component.

Here is how I am doing it.

<?php
  $number = ['one', 'two', 'three'];
?>

<!-- My vue js component -->

<orderform :orderd="formdata"></orderform>

<script>
  const app = new Vue({
      el: '#orderform',
      data(){
        return {
            formdata: <?php $number ?>
        }
      }
  });
</script>

Below is the error I am getting in the console

Uncaught SyntaxError: Unexpected token }

When I use it like below. It works fine with no errors.

<?php
  $number = ['one', 'two', 'three'];
?>

<!-- My vue js component -->

<orderform :orderd="formdata"></orderform>

<script>
  const app = new Vue({
      el: '#orderform',
      data(){
        return {
            formdata: ['one', 'two', 'three']
        }
      }
  });
</script>

I will appreciate for any help. Thanks

2
  • @kikuyu1 check answer of Jacob Goh. It will solve your problem Commented Mar 28, 2018 at 4:31
  • @B.Desai Thank you. my problem is solved Commented Mar 28, 2018 at 4:33

1 Answer 1

6
<script>
  const app = new Vue({
      el: '#orderform',
      data(){
        return {
            formdata: <?php print json_encode($number) ?>
        }
      }
  });
</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.