- window.open打开新窗口,通过post传递参数
- 新窗口通过opener.document.formName.poNum.value获取参数
- opener.document代表父类
parent.tsx
let num = 0;
const getWriteDownDetail = (num: Number) => {
const url = `/son/${num}`;
//首先创建一个form表单
var tempForm = document.createElement("form");
tempForm.id = `myform${num}`;
//制定发送请求的方式为post
tempForm.method = "post";
tempForm.name = `myform${num}`;
//此为window.open的url,通过表单的action来实现
tempForm.action = url;
//利用表单的target属性来绑定window.open的一些参数(如设置窗体属性的参数等)
tempForm.target = "_blank";
//创建input标签,用来设置参数
var hideInput = document.createElement("input");
hideInput.type = "hidden";
hideInput.name = "poNum";
hideInput.value = `yangyang${num}`;
//将input表单放到form表单里
tempForm.appendChild(hideInput);
//将此form表单添加到页面主体body中
document.body.appendChild(tempForm);
//手动触发,提交表单
tempForm?.submit();
//从body中移除form表单
// document.body.removeChild(tempForm);
}
getWriteDownDetail(1)
son.jsx
const { pathname } = history.location;
const num = pathname.replace('/son/', '');
const formName = `myform${num}`;
// 通过form传递过来的参数
const res = opener.document[formName].poNum.value;