简体   繁体   English

将对象数组传递给vue.js中的组件

[英]passing array of objects to a component in vue.js

I am having problem to pass an array of objects to component in Vue.js 2.2. 我在将对象数组传递给Vue.js 2.2中的组件时遇到问题。

Here is my component 这是我的组件

<vue-grid :fields = "[
  {name: 'Person Name', isSortable: true}, 
  {name: 'Country', isSortable: true}]"
></vue-grid>

It doesn't work as it renders the curly braces in the browser. 它无法在浏览器中呈现花括号,因此无法使用。
I've tried without the quotation " in the object and without the colon : in front of fields property. None of these work either. However, if I just pass a simple string that works. I don't know why object is not working. 我尝试过在对象中不加引号" ,在fields属性前也没有冒号: 。这些都不起作用。但是,如果我只是传递一个简单的有效字符串,我不知道为什么对象不起作用。
I have found a similar question but answer was given for php. 我发现了一个类似的问题,但针对php给出了答案。 I need the solution just for JavaScript. 我只需要JavaScript的解决方案。 I want to hard code the object array in the component. 我想对组件中的对象数组进行硬编码。

You are passing it correctly. 您正确传递了它。 You must have something else happening behind the scenes. 您必须在幕后发生其他事情。 Ensure your template has a wrapping element. 确保您的模板具有包装元素。 See this fiddle 看到这个小提琴

<div id="vue-app">
    <h2>
        Vue App
    </h2>
    <vue-grid :fields = "[
        {name: 'Person Name', isSortable: true}, 
        {name: 'Country', isSortable: true}]"
    ></vue-grid>
</div>
<script id="vue-grid-template" type="text/x-template">
    <div>
        <h3>Grid</h3>
        <div class="grid">
            Fields are:
            <ul>
                <li v-for="field in fields">
                    {{field.name}} - {{field.isSortable}}
                </li>
            </ul>
        </div>
    </div>
</script>

<script>
    Vue.component('vue-grid', {
        props: ['fields'],
        template: '#vue-grid-template'
    });

    new Vue({
        el: '#vue-app'
    });
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM