如何将Codepen转换为Vue.js?

问题描述 投票:0回答:1

我无法将this pen移动到Vue.js

这就是我的代码对于Vue应用程序的样子 - 我理解HTML和CSS应该去哪里。我应该将Javascript添加到单个组件,还是将其添加到App.vue文件中?

我想要做的是在我可以路由到的视图中测试此代码。

这是笔中的Javascript:

    var app = new Vue({
    el: '#app',
    mounted() {
        let vm = this
        axios
            .get(
                'https://sheets.googleapis.com/v4/spreadsheets/15qFEW97T-9Im9dx5G4KJqRorPDy74wwAvqaQm5Phz4w/values/A2%3AC12?key=AIzaSyBP4OwJmDCdX0rdOdgIa4q79g0XrMGcOSk'
            )
            .then(function (response) {
                let specials = response.data.values
                for (let index = 0; index < specials.length; index++) {
                    const element = specials[index]
                    let mitem = {
                        name: element[0],
                        description: element[1],
                        price: element[2]
                    }
                    if (vm.isEven(index)) {
                        vm.menuItems_L = vm.menuItems_L.concat(mitem)
                    } else {
                        vm.menuItems_R = vm.menuItems_R.concat(mitem)
                    }
                }
                console.log(response)
            })
    },
    data: {
        menuItems_L: [],
        menuItems_R: [],
        menuStyle: {
            background: '#ffe6d1',
            color: '#000'
        },
        dotStyle: {
            backgroundImage: 'radial-gradient(' + this.color + ' 1px, transparent 0px)'
        }
    },
    computed: {},
    methods: {
        isEven: function (n) {
            return n % 2 == 0
        }
    }
});

这就是我的代码在组件中的样子(通过研究/猜测的变化),HTML在它上面的标签中:

    <script>
    import axios from 'axios';

    export default {

    mounted() {
    let vm = this
    Vue.axios.get(
            'https://sheets.googleapis.com/v4/spreadsheets/15qFEW97T-9Im9dx5G4KJqRorPDy74wwAvqaQm5Phz4w/values/A2%3AC12?key=AIzaSyBP4OwJmDCdX0rdOdgIa4q79g0XrMGcOSk'
        )
        .then(function (response) {
            let specials = response.data.values
            for (let index = 0; index < specials.length; index++) {
                const element = specials[index]
                let mitem = {
                    name: element[0],
                    description: element[1],
                    price: element[2]
                }
                if (vm.isEven(index)) {
                    vm.menuItems_L = vm.menuItems_L.concat(mitem)
                } else {
                    vm.menuItems_R = vm.menuItems_R.concat(mitem)
                }
            }
            console.log(response)
        })
    },

    data: {
        menuItems_L: [],
        menuItems_R: [],
        menuStyle: {
            background: '#ffe6d1',
            color: '#000'
        },
        dotStyle: {
            backgroundImage: 'radial-gradient(#000, 1px, transparent 0px)'
        }
    },

    computed: {},

    methods: {
        isEven: function (n) {
            return n % 2 == 0
        },

        setColor: function (c) {
            c = menuStyle.color
            let colorStyle = 'radial-gradient(' + c + ' 1px, transparent 0px)'
            return colorStyle
        }
    }
}
</script>
javascript vue.js codepen
1个回答
0
投票

你只需要删除第一个HTML模板上的链接,并使<div id='#app'>是整个页面的根,如Vue Docs所述

Vue将显示错误,解释每个组件必须具有单个根元素。您可以通过将模板包装在父元素中来修复此错误

同时将data:{}更改为data(){return{}} ...因为正如Vue Docs所述,数据必须是函数

组件的数据选项必须是一个函数,以便每个实例都可以维护返回的数据对象的独立副本:

import axios from 'axios';

export default {
    el: '#app',
    data() {
      return {
        menuItems_L: [],
        menuItems_R: [],
        menuStyle: {
            background: '#ffe6d1',
            color: '#000'
        },
        dotStyle: {
            backgroundImage: 'radial-gradient(' + this.color + ' 1px, transparent 0px)'
        }
      }
    },
    mounted() {
        let vm = this
        axios
            .get(
                'https://sheets.googleapis.com/v4/spreadsheets/15qFEW97T-9Im9dx5G4KJqRorPDy74wwAvqaQm5Phz4w/values/A2%3AC12?key=AIzaSyBP4OwJmDCdX0rdOdgIa4q79g0XrMGcOSk'
            )
            .then(function (response) {
                let specials = response.data.values
                for (let index = 0; index < specials.length; index++) {
                    const element = specials[index]
                    let mitem = {
                        name: element[0],
                        description: element[1],
                        price: element[2]
                    }
                    if (vm.isEven(index)) {
                        vm.menuItems_L = vm.menuItems_L.concat(mitem)
                    } else {
                        vm.menuItems_R = vm.menuItems_R.concat(mitem)
                    }
                }
                console.log(response)
            })
    },
    methods: {
        isEven: function (n) {
            return n % 2 == 0
        }
    }
}
<template>
<div id="app">
        <section id="specialssection" class="specials-container" v-if="menuItems_L" :style="menuStyle">
            <div id="special_component" :style="menuStyle">
                <h1>Daily Specials</h1>
                <div class="specials-table-container">
                    <table>
                        <tbody v-for="item in menuItems_L" :key="`specialmenu-${item.name}`">
                            <tr class="nameandprice">
                                <td :style="dotStyle">
                                    <span :style="menuStyle">{{item.name}}</span>
                                </td>
                                <td :style="dotStyle">
                                    <span :style="menuStyle">${{item.price}}</span>
                                </td>
                            </tr>
                            <tr class="description">
                                <td colspan="2">{{item.description}}</td>
                            </tr>
                        </tbody>
                    </table>
                    <table>
                        <tbody v-for="item in menuItems_R" :key="`specialmenu-${item.name}`">
                            <tr class="nameandprice">
                                <td :style="dotStyle">
                                    <span :style="menuStyle">{{item.name}}</span>
                                </td>
                                <td :style="dotStyle">
                                    <span :style="menuStyle">${{item.price}}</span>
                                </td>
                            </tr>
                            <tr class="description">
                                <td colspan="2">{{item.description}}</td>
                            </tr>
                        </tbody>
                    </table>
                </div>
            </div>
        </section>
    </div>
</template>
© www.soinside.com 2019 - 2024. All rights reserved.