如何将 Codepen 转换为 Vue.js?

How to Convert Codepen to Vue.js?

我无法将 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>

您只需删除第一个 HTML 模板上的 link,并使 <div id='#app'> 成为整个页面的根,如 [=17= 中所述]

Vue will show an error, explaining that every component must have a single root element. You can fix this error by wrapping the template in a parent element

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

a component’s data option must be a function, so that each instance can maintain an independent copy of the returned data object:

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>