【问题标题】:How to assign json value to variable in Vue.js?如何将 json 值分配给 Vue.js 中的变量?
【发布时间】:2017-01-20 21:17:34
【问题描述】:

我使用 Firebase 来存储数据库,并且有一个对象 currentWeek,其值与当前周不同。

我想将此对象的值存储在一个变量中以调用它。

目前我正在使用它来获取当前周,但我想从数据库中获取当前周:

    <template>
  <div id="app">
      <div v-if="ligler[1]">
      <h3>{{ligler[1].weeks[currentWeek].day}}</h3>
  </div>
</template>

<script>
import Firebase from 'firebase'

let config = {
    apiKey: "xxxx",
    authDomain: "xxx.firebaseapp.com",
    databaseURL: "https://xxx.firebaseio.com"
}

let app = Firebase.initializeApp(config);
let db = app.database();

let ligRef = db.ref('ligler');

export default {
  name: 'app',
  firebase: {
    ligler: ligRef
  },
  data() {
      return {
        currentWeek: 18
      }
  }
}
</script>

我试过了,但它不起作用:

data() {
      return {
        currentWeek: ligler[1].currentWeek
      }
  }

数据库如下:

如何将 currentWeek 的值赋给 vue.js 中的变量?在代码中使用[currentWeek]:

<h3>{{ligler[1].weeks[currentWeek].day}}</h3>

【问题讨论】:

  • 试试vuefire插件
  • 我会尝试的,但我现在有一个解决方案。我在 [] 之间添加对象值,它可以工作!

标签: javascript json firebase-realtime-database vue.js vuejs2


【解决方案1】:

我有一个解决方案,我在 [] 之间添加对象值就可以了!!!

ligler[1].weeks[ligler[1].currentWeek].day

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-02-04
    • 1970-01-01
    • 2019-07-18
    • 1970-01-01
    • 1970-01-01
    • 2022-01-05
    • 2015-06-19
    • 2020-05-19
    相关资源
    最近更新 更多