Join the Stack Overflow Community
Stack Overflow is a community of 6.6 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

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?

share|improve this question
up vote 1 down vote accepted

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

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

share|improve this answer
    
Just to make it clear to anyone else with this problem, the JSON then needs to be parsed in the component. – James Parsons Jan 10 at 23:43
    
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. – Matthias Weiß Jan 11 at 9:08

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.