繁体   English   中英

将数据从Vue函数移动到外部JSON文件

[英]Move data from Vue function to an external JSON file

我希望获得Vue项目的数据,存储在外部JSON文件中,而不是存储在Vue函数本身中。

我尝试使用下面的代码从外部文件中获取数据,但无法使其工作。 也许是因为与“物品”的冲突

const app = new Vue({
    data: {
        taxRate: '',
        to: '',
        from: '',
        items: ''
    },
    created: function () {
        fetch('https://example.netlify.com/invoicedetails.json')
        .then(resp => resp.json())
        .then(items => {
            this.items = items
        })
    }
    });

以下是部分提取数据所需的代码:

new Vue({    
    data: {
    taxRate: .13,
    to: [
        'University of Somewhere',
        '118 Bureaucracy Lane',
        'Cityville, CA 90210',
    ],
    from: [
        'Business Time Inc.',
        '60 Paycheck Drive',
        'Townsland, ON A1B 2CD',
        'Canada',
    ],
    items: [
        [1, `Amazing product you can't live without`, 549],
        [3, `Unnecessary extra item`, 49],
    ]
    },
    template: `
    <div class="min-h-screen bg-gray-700 p-8 text-gray-900">
        <div class="max-w-4xl mx-auto">
        <div ref="quote" class="bg-white px-12 py-20">
            <h1 class="uppercase font-bold text-gray-600 text-3xl border-b pb-8">Quote</h1>
            <div class="mt-8 flex -mx-6">
            <div class="w-1/2 px-6">
                <div class="text-gray-700 font-bold text-sm mb-2">To:</div>
                <div v-for="line in to">{{ line }}</div>
            </div>
            <div class="w-1/2 px-6">
                <div class="text-gray-700 font-bold text-sm mb-2">From:</div>
                <div v-for="line in from">{{ line }}</div>
            </div>
            </div>
            <table class="w-full mt-8">
            <thead>
                <tr>
                <th class="px-4 py-4 border-b text-right">Quantity</th>
                <th class="px-4 py-4 border-b text-left">Description</th>
                <th class="px-4 py-4 border-b text-right">Price</th>
                <th class="px-4 py-4 border-b text-right">Total</th>
                </tr>
            </thead>
            <tbody>
                <tr v-for="[quantity, description, price] in items">
                <td class="px-4 py-4 border-b text-right">{{ quantity }}</td>
                <td class="px-4 py-4 border-b text-left">{{ description }}</td>
                <td class="px-4 py-4 border-b text-right">{{ price | price }}</td>
                <td class="px-4 py-4 border-b text-right">{{ price * quantity | price }}</td>
                </tr>
                <tr>
                <td class="border-b"></td>
                <td class="border-b"></td>
                <td class="border-b px-4 py-4 text-right font-bold">Subtotal</td>
                <td class="border-b px-4 py-4 text-right font-bold">{{ totalWithoutTaxes | price }}</td>
                </tr>
                <tr>
                <td class="border-b"></td>
                <td class="border-b"></td>
                <td class="border-b px-4 py-4 text-right font-bold">Taxes</td>
                <td class="border-b px-4 py-4 text-right font-bold">{{ taxes | price }}</td>
                </tr>
                <tr>
                <td class="border-b"></td>
                <td class="border-b"></td>
                <td class="border-b px-4 py-4 text-right font-bold">Total</td>
                <td class="border-b px-4 py-4 text-right font-bold">{{ total | price }}</td>
                </tr>
            </tbody>
            </table>
            <div class="mt-8 text-center text-gray-700">
            All prices in USD.
            </div>
        </div>
        </div>
    </div>
    `,
    el: '#app',
    computed: {
    totalWithoutTaxes() {
        return this.items.reduce((total, [quantity, _, price]) => {
        return total + (quantity * price)
        }, 0)
    },
    taxes() {
        return this.totalWithoutTaxes * (1 + this.taxRate) - this.totalWithoutTaxes
    },
    total() {
        return this.totalWithoutTaxes + this.taxes
    },
    },
    filters: {
    price: function (value) {
        return `$${value.toFixed(2)}`
    }
    }
})

这是CodePen

您可以将数据移动到vuex商店。 你可以在这里阅读更多内容( https://vuex.vuejs.org/guide/

如果您需要使用rest api数据测试您的应用程序,可以使用https://github.com/indexzero/http-server

暂无
暂无

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

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