Home » vue » How can I bind dynamic fields from server with vuejs

How can I bind dynamic fields from server with vuejs

Posted by: admin November 26, 2021 Leave a comment

Questions:

The fileds post by server, and the data is form server too, how can i bind them with vuejs? what i want to show at last is the data, not the filedName.

<script>
    module.exports = {
        data: function () {
            return {
                filedNameFromServer: ['{{filed1}}', '{{filed2}}','{{filed3}}'],
                dataFromServer: {
                    filed1: 'value1',
                    filed2: 'value2',
                    filed3: 'value3'
                }
            }
        },
        methods: {

        }
    }
</script>
<template>
    <ul>
        <li v-for="filedName in filedNameFromServer">{{filedName}}</li>
    </ul>
    //which I want after compile
    <ul>
        <li>value1</li>
        <li>value2</li>
        <li>value3</li>
    </ul>
</template>

Answers:
<script>
    module.exports = {
        data: function () {
            return {
                filedNameFromServer: ['filed1', 'filed2','filed3'],
                dataFromServer: {
                    filed1: 'value1',
                    filed2: 'value2',
                    filed3: 'value3'
                }
            }
        },
        methods: {

        }
    }
</script>

HTML:

<template>
    <ul>
        <li v-for="filedName in filedNameFromServer">
          {{ dataFromServer[filedName] }}
        </li>
    </ul>
    //which I want after compile
    <ul>
        <li>value1</li>
        <li>value2</li>
        <li>value3</li>
    </ul>
</template>

PS: should filed be field, or is your data actually about files / filing?