Skip to content

Commit ea7d09a

Browse files
committed
fix: fixed can not read property of null
1 parent f17735f commit ea7d09a

File tree

1 file changed

+24
-18
lines changed

1 file changed

+24
-18
lines changed

src/components/vue-iframe.vue

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -50,21 +50,24 @@
5050
}
5151
},
5252
setIframeUrl () {
53-
const iframeDoc = this.iframeEl.contentWindow.document;
54-
iframeDoc.open()
55-
.write(
56-
`
57-
<body onload="window.location.href='${this.src}'; parent.postMessage('${this.iframeLoadedMessage}', '*')"></body>
58-
<script>
59-
window.document.onreadystatechange = function () {
60-
if (window.document.readyState === 'complete') {
61-
parent.postMessage('${this.iframeOnReadyStateChangeMessage}', '*')
62-
}
63-
};
64-
<\/script>
65-
`
66-
);
67-
iframeDoc.close(); //iframe onload event happens
53+
if(!this.iframeEl.contentWindow) {
54+
this.initIframe()
55+
}
56+
this.$nextTick(() => {
57+
const iframeDoc = this.iframeEl.contentWindow.document;
58+
iframeDoc.open()
59+
.write(`
60+
<body onload="window.location.href='${this.src}'; parent.postMessage('${this.iframeLoadedMessage}', '*')"></body>
61+
<script>
62+
window.document.onreadystatechange = function () {
63+
if (window.document.readyState === 'complete') {
64+
parent.postMessage('${this.iframeOnReadyStateChangeMessage}', '*')
65+
}
66+
};
67+
<\/script>
68+
`);
69+
iframeDoc.close(); //iframe onload event happens
70+
})
6871
},
6972
reinitIframe: debounce(function () {
7073
this.removeIframe();
@@ -105,13 +108,16 @@
105108
}, false);
106109
}
107110
},
108-
mounted () {
111+
created () {
109112
this.listenForEvents();
110113
this.initIframe();
111114
},
112115
watch: {
113-
src () {
114-
this.reinitIframe();
116+
src: {
117+
immediate: true,
118+
handler() {
119+
this.reinitIframe();
120+
}
115121
}
116122
},
117123
render(createElement) {

0 commit comments

Comments
 (0)