Vue狀態管理

2021-10-16 12:27:46 字數 1304 閱讀 8831

一、vuex是什麼

二、使用(單向資料流)

// state

const state =

// 使用

this

.$store.state.uid

this

.$store.state.nickname

getters

const getters =

mutations(不推薦使用)

// mutations

const mutations =

,set_nickname

:(state, nickname)

=>

}// 呼叫

this

.$store.

commit

('updateuid'

, uid)

```

actions(推薦)

const actions =

, uid)

,setnickname

(, nickname)

}

作用多元件間的資料共享(共享狀態)

使用vuex的核心管理物件模組:store

import vue from

'vue'

import vuex from

'vuex'

vue.use(vuex)

export

default

newvuex.store

()

狀態管理建立了之後需要全域性對映使用
// main.js

import store from

'./store'

newvue

()

store物件

store應用

import

from

'vuex'

computed:

}...

maogetters([

'***'])

//mapgetters的返回值:}}

methods:

vuex原理圖

vue狀態管理vuex

const store new vuex.store mutations changeasync function state,a getters actions 1000 解讀 獲取 this.sto re.s tate nam e th is.store.state.name this.stor...

vue狀態管理vuex

vuex就是提供乙個倉庫,store倉庫裡面放了很多物件。其中state就是資料來源存放地,對應於與一般vue物件裡面的data 後面講到的actions和mutations對應於methods 在使用vuex的時候通常會建立store例項new vuex.store 有很多子模組的時候還會使用到m...

VUE狀態管理 store

管理全域性變數,便於元件之間傳值 1 在store資料夾中新建index.js檔案 import vue from vue 引入vue import vuex from vuex 引入vue 使用vuex vue.use vuex 建立vuex例項 const store new vuex.stor...