我和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 warn:属性或方法“游戏”未在实例上定义,但在呈现期间被引用。
我已经做了一些研究,但找不到任何帮助。
发布于 2019-12-09 00:29:40
该错误指出您的模板引用了一个未定义的变量games。如果你看一下你的代码,你会发现你告诉firebase使用gamesRef来引用游戏数据。我相信,如果您只需将您的模板更改为使用gamesRef而不是games,您就可以使用它了。
发布于 2019-12-09 02:05:54
问题解决了,我在data()属性中遗漏了一个初始化:)只需要在data()的返回值中添加games:[]即可。
感谢所有人的帮助:)
https://stackoverflow.com/questions/59236911
复制相似问题