簡體   English   中英

在 Vue.js 中使用 $state 的最佳方式是什么?

[英]What is the best way to use $state in Vue.js?

我曾經做過AngularJS ,現在我想嘗試使用Vue.js

但我有一個簡單的問題:使用和訪問狀態的最佳方式是什么? 我在 Google 上看到了很多解決方案,但我找不到哪個最適合初學者。

我曾經使用過這樣的狀態:

$state.go('homework', { homework: homework.homework});

你能給我一個Vue中這段代碼的例子嗎? 基本上,去做作業,給他做作業。

Vue.js ,它與管理庫Vuex一起使用。 您可以在此處找到Vuex中的狀態管理文檔,也可以在此處找到Vue.js的文檔。

Vuex文檔中的示例:

const store = new Vuex.Store({
  state: {
    count: 0
  },
  mutations: {
    increment (state) {
      state.count++
    }
  }
})

在您的應用程序中使用您的Vuex商店:

new Vue({
  el: '#app',
  store
})

在組件中訪問您的狀態:

this.$store.state.count

根據示例更改狀態( state.count++)

this.$store.commit('increment')

編輯:

用你的問題的例子來完成答案。 宣布

const store = new Vuex.Store({
  state: {
    homework: "Example"
  },
  mutations: {
    setNewHomework (state, newHomework) {
      state.homework = newHomework
    }
  }
})

homework設置新狀態:

this.$store.commit('setNewHomework', 'New Homework')

使用$state.go我相信您正在使用ui-router 在這種情況下,您應該查看Vue Router 它的工作原理非常相似。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM