Postmate
GirHub地址: https://github.com/dollarshaveclub/postmate.
postmate: https://www.npmjs.com/package/postmate.
npm
$ yarn add postmate # Install via Yarn
$ npm i postmate --save # Install via NPM
使用
1.在父页面和iframe页面添加
import Postmate from "postmate";
2.父页面添加(配置握手)
const handshake = new Postmate({
//iframe容器:父页面中用于存放iframe的地方
container: document.getElementById("divId"),
// iframe指向的页面,
url: "url",
// iframe名称:似乎是在父页面中区分iframe起的名字
name: "name"
});
3.iframe页面 添加供使用的方法
const handshake = new Postmate.Model({
//demoFunction:提供给父页面的方法
//options: 从父页面传入的参数信息
demoFunction:(options) =>{
console.log(options)
}
});
4.父页面调用方法
this.handshake.then(child => {
child.call("demoFunction",{options:"Hello,Postmate!"})
});