0

I am passing an array to a component in Vue.js but it is not passing properly. Strings pass fine. My code is below:

Vue code

<template>
                <div class="panel panel-default">
                    <div class="panel-heading">{{c}}</div>

                    <div class="panel-body">

                    </div>
                </div>

</template>

<script>
import axios from 'axios';
    export default {

        mounted() {
            console.log('Component ready.');
        },

        props: ['f','c'],

        data : function() {
            return {

            }
        },

And the HTML/PHP

<div class="container">
    <div class="row">
        <div class="col-md-12">
          <?php $a = ['Pasta', 'Chicken', 'Rice']; ?>
            <credits f= $a c="One"></credits>
        </div>
    </div>
</div>

In this case "c" works fine and "f" doesn't.

How can I do this properly?

1 Answer 1

3

Maybe try to encode the value using json_encode()like this:

<credits f="<?= json_encode($a)?>" c="One"></credits>

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

2 Comments

Just to make it clear to anyone else with this problem, the JSON then needs to be parsed in the component.
I don't know which backend you are using (plain PHP?) but in Laravel you're able to just pass a prop using the blade echoing like this: <credits f="{{ $a }}" c="One></credits> That way you don't have to parse anything.

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.