最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

vue3使用vuex實(shí)現(xiàn)頁面實(shí)時(shí)更新數(shù)據(jù)實(shí)例教程(setup)

 更新時(shí)間:2022年09月19日 11:35:01   作者:熊抱抱的一粒  
在前端開發(fā)中往往會遇到頁面需要實(shí)時(shí)刷新數(shù)據(jù)的情況,給用戶最新的數(shù)據(jù)展示,這篇文章主要給大家介紹了關(guān)于vue3使用vuex實(shí)現(xiàn)頁面實(shí)時(shí)更新數(shù)據(jù)(setup)的相關(guān)資料,需要的朋友可以參考下

前言

我項(xiàng)目中使用ws獲取數(shù)據(jù),因?yàn)閿?shù)據(jù)是不斷更新的,vue頁面只更新一次就不更新了,然后暫時(shí)只能想到vuex來保存更新狀態(tài),頁面監(jiān)聽數(shù)據(jù)實(shí)現(xiàn)實(shí)時(shí)更新。下面是我測試時(shí)用的數(shù)據(jù),沒有用ws,用的是定時(shí)器模擬定時(shí)發(fā)送數(shù)據(jù)。

1.項(xiàng)目引入vue

npm i vuex

2.main.js引入vuex

import { createApp } from 'vue'
import './style.css'
import App from './App.vue'
//vuex
import store from './store/index.js'
import * as echarts from 'echarts'
const app = createApp(App)
// 全局掛載echarts
createApp(App).config.globalProperties.$echarts = echarts
createApp(App).use(store).mount('#app')

3.新建store文件夾

index.js里寫vuex

import { createStore } from 'vuex'

const store = createStore({
    state: {
        iotData: {},
        count: 0,
    },
    getters: {

        getStateCount: function (state) {
            console.log('想發(fā)火啊');
            return state.iotData;
        }
    },
    mutations: {
        addCount(state, payload) {
            state.count += payload
        },
        setIOTData(state, data) {
            state.iotData = data
            console.log('setIOTData方法', state.iotData);
        },
        updateIOTTagConfig(state, data) {
            //重點(diǎn),要不然頁面數(shù)據(jù)不更新
            state.iotData=null
            state.iotData = data
            console.log(state.iotData, '進(jìn)入mutations');
        },
    },
    actions: {
        asyncAddStoreCount(store, payload) { // 第一個(gè)參數(shù)是vuex固定的參數(shù),不需要手動去傳遞
            store.commit("addCount", payload)
        },
        asyncupdateIOTTagConfig({ commit, state }, data) {
            commit('updateIOTTagConfig', data)
        },

    },
    modules: {

    }
})

export default store

3.在傳輸數(shù)據(jù)的頁面引入vuex (api.js)

(注意,這里我用的是定時(shí)器,另外不要在意這么多方法傳這個(gè)數(shù)組,你也可以就一個(gè)方法里使用vuex,我這個(gè)就是測試寫的)

let timer
import  store  from "../store/index";
export function myStopEcharts() {
    clearTimeout(timer)
}
export function startEcharts() {
 
    timer = setInterval(() => {
        var ydata1 = []
        for (let i = 0; i < 1; i++) {
            ydata1.push({ 'id': Math.round(Math.random() * 20), 'serialNumber': 2001, 'time': 2022 })
        }
        jj(ydata1)
       
    }, 2000)
}
 function jj(ydata1) {
    const messageList = ydata1
    hh(messageList)
}
function hh(messageList) {
    runExit(messageList)

}
function runExit(message) {
    exit(message)
}
 var exitArr = []
function exit(data) {
       exitArr.push(...data)
    if (exitArr.length > 20) {
         exitArr.splice(0,17)
        // console.log(s,exitArr,'pos');
    } 
    store.dispatch('asyncupdateIOTTagConfig',exitArr)
}

4.渲染頁面

<template>
  {{name}} 
  <!-- <h1>vuex中的數(shù)據(jù){{ store.state.count }}</h1>
  <button @click="changeStoreCount">改變vuex數(shù)據(jù)</button>
  <button @click="asyncChangeStoreCount">異步改變vuex數(shù)據(jù)</button> -->
  <echarts></echarts>
</template>
<script>
import { defineComponent, computed,ref, watch, toRaw ,onUnmounted} from "vue";
import echarts from "./echarts.vue";
import { useStore } from "vuex";
import axios from "axios";
export default defineComponent({
  name: "HelloWorld",
  components:{echarts},
  setup() {
    let {state, commit,getters} = useStore();
    //使用計(jì)算屬性動態(tài)拿到vuex的值
    let name = computed(() => {return state.iotData})
    // let UserInfoNoParams = computed(() => getters['getStateCount'])
    console.log(name,state.iotData,'哎');
    // commit("setIOTData", 1);
    // const nextAge = computed({
    //    get() {
    //     return store.iotData
    //   },
    //   // set(value) {
    //   //   console.log(value)  //  輸出新修改的值
    //   //   return age.value = value - 1
    //   // }
    // })
    //監(jiān)聽數(shù)據(jù)
     watch(name, (newVal, oldVal) => {
      console.log(name,"改變的值", toRaw(newVal));
      console.log("舊",oldVal);
    },{immediate:true,deep: true});
    // console.log(nextAge,'nextAge');
    return {name};
    //   const changeStoreCount = () => {
    //     store.commit("addCount", 1)
    //   }
    //   const asyncChangeStoreCount = () => {
    //     setTimeout(() => {
    //  // asyncAddStoreCount是mutations中的方法,2是傳遞過去的數(shù)據(jù)
    //  // 異步改變vuex用dispatch方法,這里用setTimeout模擬異步操作
    //       store.dispatch("asyncAddStoreCount", 2)
    //     }, 1000)
    //   }
    // return { store, changeStoreCount, asyncChangeStoreCount }
  },
});
</script>
<style scoped></style>

代碼可能有點(diǎn)亂,不過能實(shí)現(xiàn)效果。

總結(jié)

到此這篇關(guān)于vue3使用vuex實(shí)現(xiàn)頁面實(shí)時(shí)更新數(shù)據(jù)(setup)的文章就介紹到這了,更多相關(guān)vuex頁面實(shí)時(shí)更新數(shù)據(jù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Vue?動態(tài)路由的實(shí)現(xiàn)詳情

    Vue?動態(tài)路由的實(shí)現(xiàn)詳情

    這篇文章主要介紹了Vue?動態(tài)路由的實(shí)現(xiàn),動態(tài)路由是一個(gè)常用的功能,根據(jù)后臺返回的路由json表,前端動態(tài)顯示可跳轉(zhuǎn)的路由項(xiàng),本文主要實(shí)現(xiàn)的是后臺傳遞路由,前端拿到并生成側(cè)邊欄的一個(gè)形勢,需要的朋友可以參考一下
    2022-06-06
  • 基于Vue.js 2.0實(shí)現(xiàn)百度搜索框效果

    基于Vue.js 2.0實(shí)現(xiàn)百度搜索框效果

    這篇文章主要為大家詳細(xì)介紹了基于Vue.js 2.0實(shí)現(xiàn)百度搜索框效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-09-09
  • vue+animation動畫實(shí)現(xiàn)跑馬燈效果

    vue+animation動畫實(shí)現(xiàn)跑馬燈效果

    這篇文章主要為大家詳細(xì)介紹了vue+animation動畫實(shí)現(xiàn)跑馬燈效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-04-04
  • Electron + vue 打包桌面操作流程詳解

    Electron + vue 打包桌面操作流程詳解

    這篇文章主要介紹了Electron + vue 打包桌面操作流程,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2019-06-06
  • 使用vue-cli webpack 快速搭建項(xiàng)目的代碼

    使用vue-cli webpack 快速搭建項(xiàng)目的代碼

    這篇文章主要介紹了vue-cli webpack 快速搭建項(xiàng)目的教程詳解,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2018-11-11
  • vue element-ui table組件動態(tài)生成表頭和數(shù)據(jù)并修改單元格格式 父子組件通信

    vue element-ui table組件動態(tài)生成表頭和數(shù)據(jù)并修改單元格格式 父子組件通信

    這篇文章主要介紹了vue element-ui table組件動態(tài)生成表頭和數(shù)據(jù)并修改單元格格式 父子組件通信,需要的朋友可以參考下
    2019-08-08
  • vue router-link傳參以及參數(shù)的使用實(shí)例

    vue router-link傳參以及參數(shù)的使用實(shí)例

    下面小編就為大家?guī)硪黄獀ue router-link傳參以及參數(shù)的使用實(shí)例。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-11-11
  • ant design vue動態(tài)循環(huán)生成表單以及自定義校驗(yàn)規(guī)則詳解

    ant design vue動態(tài)循環(huán)生成表單以及自定義校驗(yàn)規(guī)則詳解

    這篇文章主要介紹了ant design vue動態(tài)循環(huán)生成表單以及自定義校驗(yàn)規(guī)則詳解,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-01-01
  • vue實(shí)現(xiàn)橫屏滾動公告效果

    vue實(shí)現(xiàn)橫屏滾動公告效果

    這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)橫屏滾動公告效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-04-04
  • Vue的路由及路由鉤子函數(shù)的實(shí)現(xiàn)

    Vue的路由及路由鉤子函數(shù)的實(shí)現(xiàn)

    這篇文章主要介紹了Vue的路由及路由鉤子函數(shù)的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-07-07

最新評論

习水县| 驻马店市| 锦州市| 苏尼特左旗| 北京市| 天等县| 库尔勒市| 红河县| 南平市| 高密市| 天水市| 长泰县| 阿巴嘎旗| 海兴县| 甘肃省| 门源| 龙门县| 赞皇县| 德阳市| 江阴市| 黑水县| 乌苏市| 大新县| 城市| 商都县| 自贡市| 鄂托克前旗| 晋州市| 汝城县| 阜新| 宁国市| 平南县| 凤阳县| 邵阳县| 清丰县| 保定市| 张家口市| 合肥市| 任丘市| 鹤庆县| 泸水县|