首页 > 其他分享 >Vue3 Pinia对state的订阅监听($subscribe,$onAction)数据监听

Vue3 Pinia对state的订阅监听($subscribe,$onAction)数据监听

时间:2023-11-16 20:55:06浏览次数:33  
标签:const subscribe currentFrameUrl patch onAction state token 监听 store

<template>
    <div class="main-container" :class="{'show-scroll':targetIsVisible}">
        <div :style="{height:frameHeight+'px'}"  class="main-content" :class="{'show-scroll':targetIsVisible}">
            <!--:scrolling="targetIsVisible ? 'yes':'no'"-->
      <iframe id="content_frame_quality" scrolling="true"   frameborder="no" border="0" height="100%" width="100%" 
          :src="state.frameUrl"
          name="main" @load="adjustIframe">
     </iframe>
         </div>
    </div>
</template>

<script setup>
import { reactive } from "@vue/reactivity";
import "../assets/css/base.css";
import { useWebStore } from '@/store/web'
import { onBeforeRouteLeave, onBeforeRouteUpdate } from "vue-router";
let webStore = useWebStore()

//第二种修改方式:使用$patch改变数据 $patch 可以同时修改多个值
function changeDataByPatch() {
    /*
    $patch也有两种的调用方式
    第一种写法的在修改数组时,假如我只想要把 ipList 的中第2项改成‘192.168.10.222’,
    但是也需要传入整个包括所有元素的数组,这无疑增加了书写成本和风险,
    显然是不合理的,所以一般都推荐使用第二种传入一个函数的写法
    * */
    // 第一种 $patch方法
    // store.$patch({
    //   baseUrl: 'https://www.jd.com/',
    //   ipList: ['192.168.10.777', '192.168.10.222', '192.168.10.888']
    // })
    // 第二种 $patch方法
    webStore.$patch((state) => {
        //state.baseUrl = 'https://www.jd.com/'
        //state.ipList[0] = '192.168.10.222'
    })
}

const targetIsVisible = ref(false);
const frameHeight = ref(1000)
const setHeight = ( height )=>{
    frameHeight.value=height
}

const state = reactive({
    currentFrameUrl: '/Quality/Mobile/Web',
    frameUrl:''
})

const adjustIframe = (e) => {
      setTimeout(() => {
                    let qualityIframe = document.getElementById("content_frame_quality");
                    console.log(qualityIframe);
                    let contentWindowObject = qualityIframe.contentWindow;
                let targetDom = contentWindowObject
                //console.log(targetDom)
      }, 0);
}

onBeforeMount(() => {
    localStorage.setItem("userData", { "username": 'zhangsan', token: 'xmyfsj0821119' })
    const internalInstance = getCurrentInstance()
  const internalData = internalInstance.appContext.config.globalProperties
    const userToken = internalData.$cookies.get('userToken') // 后面的为之前设置的cookies的名字
    state.frameUrl=`https://test.ashgso.com${state.currentFrameUrl}?login_from=davinci&davinci_token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJhZG1pbiIsImlhdCI6MTY3MDM4MTczNCwiZXhwIjoxNjcwMzgxNzk0LCJuYmYiOjE2NzAzODE3MzQsImFjY291bnQiOiJhZG1pbiIsInBhc3N3b3JkIjoiYWI1OGUwN2JhYmViODZlZTk1Y2I5MzFkZGI4MjViODIifQ.7iEs5fpYj10QlLX4fi7FJEy3iuRptQDX6V3A1178o90&checklist_token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJhZG1pbiIsImlhdCI6MTY3MDM4MTczNCwiZXhwIjoxNjcwMzgxNzk0LCJuYmYiOjE2NzAzODE3MzQsImFjY291bnQiOiJhZG1pbiIsInBhc3N3b3JkIjoiYWI1OGUwN2JhYmViODZlZTk1Y2I5MzFkZGI4MjViODIifQ.7iEs5fpYj10QlLX4fi7FJEy3iuRptQDX6V3A1178o90`
})


onBeforeRouteLeave((to, from, next) => {
    
});

//webStore中数据监听
const subscribe = webStore.$subscribe((mutation, store) => {
      /*
      * mutation主要包含三个属性值:
      *   events:当前state改变的具体数据,包括改变前的值和改变后的值等等数据
      *   storeId:是当前store的id
      *   type:用于记录这次数据变化是通过什么途径,主要有三个分别是
      *         “direct” :通过 action 变化的
                ”patch object“ :通过 $patch 传递对象的方式改变的
                “patch function” :通过 $patch 传递函数的方式改变的

            * detached:布尔值,默认是 false,正常情况下,当订阅所在的组件被卸载时,订阅将被停止删除,
              如果设置detached值为 true 时,即使所在组件被卸载,订阅依然在生效
      *
      * */
      //在此处监听store中值的变化,当变化为某个值的时候,去做一些业务操作之类的
            if (store.currentFrameUrl) {
                state.currentFrameUrl = store.currentFrameUrl
                state.frameUrl = `https://test.ashgso.com${state.currentFrameUrl}?login_from=davinci&davinci_token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJhZG1pbiIsImlhdCI6MTY3MDgwOTE3MiwiZXhwIjoxNjcwODA5MjMyLCJuYmYiOjE2NzA4MDkxNzIsImFjY291bnQiOiJhZG1pbiIsInBhc3N3b3JkIjoiYWI1OGUwN2JhYmViODZlZTk1Y2I5MzFkZGI4MjViODIifQ.8d8NfS36GoK_dQunfavgJckgcGvtjXhsAW4aynCKjLk&checklist_token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJhZG1pbiIsImlhdCI6MTY3MDgwOTE3MiwiZXhwIjoxNjcwODA5MjMyLCJuYmYiOjE2NzA4MDkxNzIsImFjY291bnQiOiJhZG1pbiIsInBhc3N3b3JkIjoiYWI1OGUwN2JhYmViODZlZTk1Y2I5MzFkZGI4MjViODIifQ.8d8NfS36GoK_dQunfavgJckgcGvtjXhsAW4aynCKjLk`
            }
    }, { detached: false }
)

//监听actions中的方法
webStore.$onAction(args => {
    args.after(() => {
        //也可以在action执行完数据后后赋值
        if (args.name == 'setCurrentFrameUrl') { 
            //state.currentFrameUrl = args.args[0]
            //state.frameUrl=`https://test.ashgso.com/${state.currentFrameUrl}?login_from=davinci&davinci_token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJhZG1pbiIsImlhdCI6MTY3MDM4MTczNCwiZXhwIjoxNjcwMzgxNzk0LCJuYmYiOjE2NzAzODE3MzQsImFjY291bnQiOiJhZG1pbiIsInBhc3N3b3JkIjoiYWI1OGUwN2JhYmViODZlZTk1Y2I5MzFkZGI4MjViODIifQ.7iEs5fpYj10QlLX4fi7FJEy3iuRptQDX6V3A1178o90&checklist_token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJhZG1pbiIsImlhdCI6MTY3MDM4MTczNCwiZXhwIjoxNjcwMzgxNzk0LCJuYmYiOjE2NzAzODE3MzQsImFjY291bnQiOiJhZG1pbiIsInBhc3N3b3JkIjoiYWI1OGUwN2JhYmViODZlZTk1Y2I5MzFkZGI4MjViODIifQ.7iEs5fpYj10QlLX4fi7FJEy3iuRptQDX6V3A1178o90`
        }
  })
})

onMounted(() => {
    //console.log(webStore.getModuleList)
})
</script>

<style lang="scss" scoped>
  .main-container{
        padding: 0;
        padding-top:30px;
        box-sizing: border-box;
        height:100vh;
    }

    .main-content{
        padding-top:60px;
        overflow:hidden;
    }

    .show-scroll{
        overflow:visible;
    }

    iframe{
    overflow: hidden;
  } 
</style>

 

标签:const,subscribe,currentFrameUrl,patch,onAction,state,token,监听,store
From: https://www.cnblogs.com/xmyfsj/p/17837256.html

相关文章

  • 过期监听实现定时任务的反面教材
    一、场景    在电商、支付等领域,往往会有这样的场景,用户下单后放弃支付了,那这笔订单会在指定的时间段后进行关闭操作。    像某宝、某东都有这样的逻辑,而且时间很准确,误差在1s内,那他们是怎么实现的呢?    一般实现方法有几种: 使用RocketMQ、RabbitM......
  • Object.defineProperty(obj,key,val)不可以监听数组变化,需要做特殊处理,所以Vue3.0使用
    关于Vue双向数据绑定说法错误的是()AVue实现双向数据绑定是采用数据劫持和发布者-订阅者模式BObject.defineProperty(obj,key,val)可以监听数组变化,不需要做特殊处理CVue2.0数据劫持是利用ES5的Object.defineProperty(obj,key,val)方法来劫持每个属性的getter和setterD......
  • Vue怎么监听路由参数的变化?
    有两种方法可以监听路由参数的变化,但是只能用在包含<router-view/>的组件内第一种用侦听器监听watch:{ '$route'(to,from){ //在此处监听 },},第二种在路由守卫种监听beforeRouteUpdate(to,from,next){ //这里监听}......
  • JavaScript--事件监听
     事件绑定 dom属性绑定  <inputtype="button"id="btn"value="点我">  <script>    //document.getElementById('myImgine').src='images/open.jpg';    document.getElementById("btn&......
  • websocket的消息丢失处理,以及前端监听心跳处理方案
    消息丢失处理方案:1、后台通过websocket传输给前端消息,并且后台生成校验此消息的定时任务,设置每5秒重发2、前端接收到消息后将消息通过websocket传输给后台3、后台如接收到前端的消息则删除对应的发送消息定时任务,如未收到消息则继续发送,设置最多发送5次(超过5次默认认为此条消......
  • vue-test ------事件监听
    <template><h3>监听器</h3><p>{{message}}</p><button@click="updateHandle">点击</button></template><script>exportdefault{name:"WatchDemo",data(){return{mess......
  • VUE监听网页关闭和隐藏显示(页签关闭调用某一个接口)
    mounted(){this.id=this.$route.query.id;window.addEventListener("beforeunload",async(e)=>awaitthis.beforeunloadHandler(e));window.addEventListener("unload",(e)=>this.unloadHandle......
  • Oracle ORA-12514:TNS:监听程序当前无法识别连接描述符中请求的服务
    oracle10g配置客户端时,测试连接出现错误(NetConfigurationAssistant--本地Net服务名配置):ORA-12514:TNS:监听程序当前无法识别连接描述符中请求的服务随后打开:D:\oracle\product\10.2.0\db_1\NETWORK\ADMIN\listener.ora 内容如下:#listener.oraNetworkConfigurationFile:D......
  • Nuxt-监听浏览器返回
    区分普通页面/keep-alive缓存页面1、普通页面mounted:{this.setAddListener()},destroyed(){window.removeEventListener('popstate',this.setBack,false)},methods:{//监听浏览器返回操作setAddListener(){consthasRefresh......
  • echarts监听legend的点击事件
    1、echarts监听legend的点击事件myChart.on('legendselectchanged',function(params){console.log(params);});转自:echarts问题--点击legend不全部隐藏,监听最后一个legend点击事件_echarts点击图例不隐藏_哥要掉线了的博客-CSDN博客 ......