首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >[Vue warn]:未在实例上定义属性或方法“游戏”,但在呈现过程中引用了该属性或方法

[Vue warn]:未在实例上定义属性或方法“游戏”,但在呈现过程中引用了该属性或方法
EN

Stack Overflow用户
提问于 2019-12-08 23:28:31
回答 2查看 1.1K关注 0票数 2

我和vue.js有点问题。我对此还不熟悉,看不出代码中有什么错误。

我的main.js

代码语言:javascript
复制
import Vue from 'vue';
import App from './App.vue';
import BootstrapVue from 'bootstrap-vue';
import 'bootstrap/dist/css/bootstrap.css';
import 'bootstrap-vue/dist/bootstrap-vue.css';
import { rtdbPlugin } from 'vuefire';


Vue.use(BootstrapVue);
Vue.use(rtdbPlugin);
Vue.config.productionTip = false;


new Vue({
    render: h => h(App)
}).$mount('#app');

我的firebase.js

代码语言:javascript
复制
import firebase from 'firebase/app';
import 'firebase/database';

const app = firebase.initializeApp({ ... });

export const db = app.database();
export const gamesRef = db.ref('Games');

我的App.vue组件

代码语言:javascript
复制
<template>
    <b-container>
        <div class="page-header">
            <h2 class="text-center">Game Manager</h2>
            <br/>
        </div>
        <b-row>
            <b-col lg="4">
                <b-form v-on:submit.prevent="onSubmit()">
                    <b-form-group label="Titolo" label-for="titolo">
                        <b-form-input type="text" id="titolo" name="titolo" v-model="newGame.Titolo" v-on:change="onChange()"></b-form-input>
                    </b-form-group>
                    <b-form-group label="Software House" label-for="softwarehouse">
                        <b-form-input type="text" id="softwarehouse" name="softwarehouse" v-model="newGame.SoftwareHouse" v-on:change="onChange()"></b-form-input>
                    </b-form-group>
                    <b-form-group label="Tipo" label-for="tipo">
                        <b-form-select id="tipo" name="tipo" v-model="newGame.Tipo" :options="gameTypes"></b-form-select>
                    </b-form-group>
                    <b-form-group label="Piattaforma" label-for="piattaforma">
                        <b-form-select id="piattaforma" name="piattaforma" v-model="newGame.Piattaforma" :options="gamePlatforms"></b-form-select>
                    </b-form-group>

                    <b-btn type="submit" variant="primary" :disabled="submitDisabled">Aggiungi</b-btn>
                </b-form>
                <br/>
            </b-col>
            <b-col lg="8">
                <b-table :items="games" :fields="fields">
                    <template slot="Tipo" slot-scope="data">{{getGameType(data.item.Tipo)}}</template>
                    <template slot="Piattaforma" slot-scope="data">{{getPiattaforma(data.item.Piattaforma)}}</template>
                    <template slot=" " slot-scope="data">
                        <b-btn size="sm" variant="warning">X</b-btn>
                        <b-btn size="sm" variant="secondary">M</b-btn>
                    </template>
                </b-table>
            </b-col>
        </b-row>
    </b-container>
</template>

<script>
    import { gamesRef } from './firebase';
    import { gameTypeEnum, piattaformaEnum } from './models/game';

    export default {
        firebase: {
            games: gamesRef
        },
        data() {
            return {
                gameTypes: gameTypeEnum.properties,
                gamePlatforms: piattaformaEnum.properties,
                fields: ['Titolo', 'SoftwareHouse', 'Tipo', 'Piattaforma', ' '],
                newGame: {
                    Titolo: '',
                    SoftwareHouse: '',
                    Tipo: gameTypeEnum.FPS,
                    Piattaforma: piattaformaEnum.PC
                },
                submitDisabled: true
            };
        },
        methods: {
            getPiattaforma(value) {
                return this.gamePlatforms[value].text;
            },
            getGameType(value) {
                return this.gameTypes[value].text;
            },
            onSubmit() {
                gamesRef.push(this.newGame);
                this.newGame.Titolo = '';
                this.newGame.SoftwareHouse = '';
                this.submitDisabled = true;
            },
            onChange() {
                this.submitDisabled = this.SoftwareHouse === '' || this.Titolo === '';
            },
            onDelete(game) {
                gamesRef.child(game['.key']).remove();
            }

        }
    };
</script>

<style lang="scss">
    .page-header{
        background-color: #226622;
        color: #ffffff;
    }
    .page-header h2{
        padding-top: 8px;
    }
</style>

该代码来自学习Vue.js的教程,编译和启动后一切正常,但没有从FireBase DB读取数据。相反,此错误出现在控制台中: Vue warn:属性或方法“游戏”未在实例上定义,但在呈现期间被引用。

我已经做了一些研究,但找不到任何帮助。

EN

回答 2

Stack Overflow用户

发布于 2019-12-09 00:29:40

该错误指出您的模板引用了一个未定义的变量games。如果你看一下你的代码,你会发现你告诉firebase使用gamesRef来引用游戏数据。我相信,如果您只需将您的模板更改为使用gamesRef而不是games,您就可以使用它了。

票数 0
EN

Stack Overflow用户

发布于 2019-12-09 02:05:54

问题解决了,我在data()属性中遗漏了一个初始化:)只需要在data()的返回值中添加games:[]即可。

感谢所有人的帮助:)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59236911

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档