npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@jslq/vue-iframe-messager

v1.0.4

Published

[vue]iframe跨域通信插件

Downloads

22

Readme

Vue-iframe-messager

基于vue(vuex)实现的可跨域iframe通信的插件,有问题联系QQ: 917498254,或者提issue

🚀 下载

npm install @jslq/vue-iframe-messager --save

在iframe中使用

import Vue from 'vue'
import store from './store'
import VueIframeMessager from '@jslq/vue-iframe-messager'

Vue.use(new VueIframeMessager({
    role: 'iframe',
    vuex: {
        store,
        actionPrefix: "MESSAGER_",  //默认值就是这个
        mutationPrefix: "MESSAGER_" //没有默认值,需手动传入
    },
    options: {
        window: window,
        origin: process.env.VUE_APP_PARENT_ORIGIN  //在vue-cli3中可以存父窗口的origin,同域可以不传
    }
}))

在父窗口中使用

import Vue from 'vue'
import store from './store'
import VueIframeMessager from '@jslq/vue-iframe-messager'

Vue.use(new VueIframeMessager({
    role: 'parent',
    vuex: {
        store,
        actionPrefix: "MESSAGER_",  //默认值就是这个
        mutationPrefix: "MESSAGER_" //没有默认值,需手动传入
    },
    options: {
        iframeId: 'my-iframe'
        window: window,
        origin: process.env.VUE_APP_IFRAME_ORIGIN  //在vue-cli3中可以存父窗口的origin,同域可以不传
    }
}))

如何在组件中注册事件

//a.vue in parent
export default {
    ...
    data() {
        return {
            got
        }
    },
    messager: { //如果alias 赋值则使用那个值,parent和iframe的alias保持一直
        //会自动注册type===selected的事件
        selected(data) {
            this.got = data //'haha'
        }
    }
    ...
}

//b.vue in iframe
export default {
    ...
    method: {
        handlerClick() {
            this.$messager.send('selected', 'haha')
        }
    }
    ...
}

参数|类型|默认值|是否必传|描述 -----|-----|-----|-----|----- role|string|null|是|看上面 alias|string|messager|不是|如果用到vue插件也是以messager作为名字,为了防止冲突,可以起别名,不过一般不会有人用这个名字 vuex.store|Vuex|null|不是|Vuex store 实例 vuex.actionPrefix|String|null|不是|前缀 vuex.mutationPrefix|String |null|不是|前缀 options.iframeId|string|null|是|role是'parent'时必传 options.origin|string|null|是|role是'iframe'并且和父窗口跨域时必传 options.window|DOMElement|null|是|window对象

在vuex中使用

import Vue from 'vue'
import Vuex from 'vuex'
const prefix = 'MESSAGER_'

Vue.use(Vuex)

export default new Vuex.Store({
    state: {},
    mutations: {
        `MESSAGER_${EVENT_NAME}`() {
            // do something
        }
    },
    actions: {
        `MESSAGER_${EVENT_NAME}`() {
            // do something
        }
    }
})

常见问题

父窗口mounted中调用this.$messager.send没有生效

虽然设置了origin,但是还是会有跨域问题,原因未知,不过可以用setTimeout延迟执行

提示跨域

报错信息 Failed to execute 'postMessage' on 'DOMWindow',设置下options.origin 即可