vue.js出现问题。我是新手,看不到代码中的错误。
我的main.js
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
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组件
<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警告]:属性或方法“游戏”未在实例上定义,但在渲染期间被引用。
我做了一些重新研究,但找不到任何帮助我的方法。
答案 0 :(得分:0)
该错误表明您的模板引用了一个未定义的变量games
。如果您看一下代码,就会发现告诉Firebase使用gamesRef
来引用游戏数据。我相信,如果您只是将模板更改为使用gamesRef
而不是games
,那将是很好的选择。
答案 1 :(得分:0)
问题已解决,我在data()属性中缺少初始化:) 添加游戏:[],只是在data()的返回中。
非常感谢您的帮助:)