vuex的state狀態物件使用方式匯總
這次帶給大家vuex的state狀態物件使用方式彙總,vuex的state狀態物件使用注意事項有哪些,以下就是實戰案例,一起來看一下。
下面給大家貼我的vuex的結構
下面是store資料夾下的state.js和index.js內容
//state.js const state = { headerBgOpacity:0, loginStatus:0, count:66 } export default state //index.js import Vue from 'vue' import Vuex from 'vuex' import state from './state' import actions from './actions' import getters from './getters' import mutations from './mutations' Vue.use(Vuex) export default new Vuex.Store({ state, actions, getters, mutations })
下面開始在test.vue元件當中使用vuex的state狀態物件
#方式一
<template> <p class="test"> {{$store.state.count}} <!--第一种方式--> </p> </template> <script type="text/ecmascript-6"> export default{ name:'test', data(){ return{ } } } </script> <style> </style>
方式二
<template> <p class="test"> {{count}} <!--步骤二--> </p> </template> <script type="text/ecmascript-6"> export default{ name:'test', data(){ return{} }, computed:{ count(){ return this.$store.state.count; //步骤一 } } } </script> <style> </style>
方式三
<template> <p class="test"> {{count}} <!--步骤三--> </p> </template> <script type="text/ecmascript-6"> import {mapState} from 'vuex' //步骤一 export default{ name:'test', data(){ return{} }, computed:mapState({ //步骤二,对象方式 count:state => state.count }) } </script> <style> </style>
方式四
<template> <p class="test"> {{count}} <!--步骤三--> </p> </template> <script type="text/ecmascript-6"> import {mapState} from 'vuex' //步骤一 export default{ name:'test', data(){ return{} }, computed:mapState([ //步骤二,数组方式 "count" ]) } </script> <style> </style>
方式五
<template> <p class="test"> {{count}} <!--步骤三--> </p> </template> <script type="text/ecmascript-6"> import {mapState} from 'vuex' //步骤一 export default{ name:'test', data(){ return{} }, computed:{ ...mapState([ //步骤二,三个点方式 "count" ]) } } </script> <style> </style>
相信看了本文案例你已經掌握了方法,更多精彩請關注php中文網其它相關文章!
推薦閱讀:
#以上是vuex的state狀態物件使用方式匯總的詳細內容。更多資訊請關注PHP中文網其他相關文章!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

熱門話題

Vue2.x是目前最受歡迎的前端框架之一,它提供了Vuex作為管理全域狀態的解決方案。使用Vuex能夠使得狀態管理更加清晰、易於維護,以下將介紹Vuex的最佳實踐,幫助開發者更好地使用Vuex以及提高程式碼品質。 1.使用模組化組織狀態Vuex使用單一狀態樹管理應用程式的全部狀態,將狀態從元件中抽離出來,使得狀態管理更加清晰易懂。在具有較多狀態的應用中,必須使用模組
![在Vue應用中使用vuex時出現「Error: [vuex] do not mutate vuex store state outside mutation handlers.」怎麼解決?](https://img.php.cn/upload/article/000/000/164/168760467048976.jpg?x-oss-process=image/resize,m_fill,h_207,w_330)
在Vue應用程式中,使用vuex是常見的狀態管理方式。然而,在使用vuex時,我們有時可能會遇到這樣的錯誤提示:「Error:[vuex]donotmutatevuexstorestateoutsidemutationhandlers.」這個錯誤提示是什麼意思呢?為什麼會出現這個錯誤提示?如何解決這個錯誤?本文將詳細介紹這個問題。錯誤提示的含

Vuex是做什麼的? Vue官方:狀態管理工具狀態管理是什麼?需要在多個元件中共享的狀態、且是響應式的、一個變,全都改變。例如一些全域要用的狀態資訊:使用者登入狀態、使用者名稱、地理位置資訊、購物車中商品、等等這時候我們就需要這麼一個工具來進行全域的狀態管理,Vuex就是這樣的一個工具。單一頁面的狀態管理View–>Actions—>State視圖層(view)觸發操作(action)變更狀態(state)回應回視圖層(view)vuex(Vue3.
![在Vue應用程式中使用vuex時出現「Error: [vuex] unknown action type: xxx」怎麼解決?](https://img.php.cn/upload/article/000/887/227/168766615217161.jpg?x-oss-process=image/resize,m_fill,h_207,w_330)
在Vue.js專案中,vuex是一個非常有用的狀態管理工具。它可以幫助我們在多個元件之間共享狀態,並提供了一種可靠的方式來管理狀態的變化。但使用vuex時,有時會遇到「Error:[vuex]unknownactiontype:xxx」的錯誤。這篇文章將介紹該錯誤的原因及解決方法。 1.錯誤原因在使用vuex時,我們需要定義一些actions和mu

在Vue應用中使用Vuex是非常常見的操作。然而,偶爾在使用Vuex時會遇到錯誤訊息“TypeError:Cannotreadproperty'xxx'ofundefined”,這個錯誤訊息的意思是無法讀取undefined的屬性“xxx”,導致了程式的錯誤。這個問題其實產生的原因很明顯,就是因為在呼叫Vuex的某個屬性的時候,這個屬性沒有被正確

具體步驟:1、安裝vuex(vue3建議4.0+)pnpmivuex-S2、main.js中設定importstorefrom'@/store'//hx-app的全域設定constapp=createApp(App)app.use(store)3、新建相關的資料夾與文件,這裡配置多個不同vuex內部的js,使用vuex的modules來放置不同的頁面,文件,然後統一使用一個getters.jsindex.js核心文件,這裡使用了import.meta.glob ,而不

Vue中如何使用vuex進行元件通訊? Vue是一款流行的JavaScript框架,它採用組件化的開發模式,使得我們能夠更方便地建立複雜的應用程式。在Vue的組件化開發過程中,常會遇到需要不同組件之間進行通訊的情況。 Vuex是Vue官方推薦的狀態管理工具,提供了一個集中式的儲存管理器,解決了元件之間通訊的問題。本文將介紹如何在Vue中使用Vuex進行組件通信
