Skip to content

Commit 2185fbd

Browse files
committed
fix web component.
1 parent 93b9ee1 commit 2185fbd

File tree

4 files changed

+70
-17
lines changed

4 files changed

+70
-17
lines changed

examples/components/web.vue

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,17 @@
1212
@click.native="refresh"></button>
1313
</div>
1414
<web class="content" ref="webview" :src="src"
15-
@pagestart="startload" @pagefinish="finishload" @error="failload"></web>
15+
@pagestart="startload"
16+
@pagefinish="finishload"
17+
@error="failload"></web>
1618
</div>
1719
</template>
1820

1921
<script>
2022
var webview = weex.requireModule('webview');
2123
const src = 'http://invalid.src/for/test'
2224
const newSrc = 'http://m.taobao.com'
25+
// const newSrc = window.location.href.replace(/\?[^?]*$/, '') // for test.
2326
module.exports = {
2427
data () {
2528
return {

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "weex-vue-render",
33
"description": "Web renderer for weex project written in Vue DSL.",
4-
"version": "1.0.15",
4+
"version": "1.0.16",
55
"license": "Apache-2.0",
66
"main": "dist/index.common.js",
77
"keywords": [

packages/weex-vue-render/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "weex-vue-render",
3-
"version": "1.0.15",
3+
"version": "1.0.16",
44
"description": "Web renderer for weex project written in Vue DSL.",
55
"license": "Apache-2.0",
66
"main": "dist/index.common.js",

src/components/web/index.js

Lines changed: 64 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,53 +23,103 @@ function getWeb (weex) {
2323
const { dispatchNativeEvent } = weex.utils
2424

2525
return {
26+
data () {
27+
return {
28+
currentSrc: ''
29+
}
30+
},
2631
name: 'weex-web',
2732
props: {
2833
src: String
2934
},
35+
watch: {
36+
src (newVal) {
37+
this.currentSrc = newVal
38+
}
39+
},
3040
methods: {
31-
// TODO: check cross-origin
3241
goBack () {
33-
if (this.$el) {
34-
this.$el.contentWindow.history.back()
42+
const el = this.$el
43+
if (el) {
44+
const win = el.contentWindow
45+
try {
46+
win.history.back()
47+
this.currentSrc = win.location.href
48+
}
49+
catch (err) {
50+
dispatchNativeEvent(el, 'error', err)
51+
}
3552
}
3653
},
3754
goForward () {
38-
if (this.$el) {
39-
this.$el.contentWindow.history.forward()
55+
const el = this.$el
56+
if (el) {
57+
const win = el.contentWindow
58+
try {
59+
win.history.forward()
60+
this.currentSrc = win.location.href
61+
}
62+
catch (err) {
63+
dispatchNativeEvent(el, 'error', err)
64+
}
4065
}
4166
},
4267
reload () {
43-
if (this.$el) {
44-
this.$el.contentWindow.history.reload()
68+
const el = this.$el
69+
if (el) {
70+
try {
71+
el.contentWindow.location.reload()
72+
dispatchNativeEvent(el, 'pagestart', { url: this.currentSrc })
73+
}
74+
catch (err) {
75+
dispatchNativeEvent(el, 'error', err)
76+
}
4577
}
4678
}
4779
},
4880

81+
created () {
82+
this.currentSrc = this.src
83+
},
84+
4985
mounted () {
5086
const el = this.$el
51-
this._prevSrc = this.src
87+
this._prevSrc = this.currentSrc
5288
if (el) {
53-
dispatchNativeEvent(el, 'pagestart', { url: this.src })
89+
dispatchNativeEvent(el, 'pagestart', { url: this.currentSrc })
5490
}
5591
},
5692

5793
updated () {
58-
if (this.src !== this._prevSrc) {
59-
this._prevSrc = this.src
60-
dispatchNativeEvent(this.$el, 'pagestart', { url: this.src })
94+
if (this.currentSrc !== this._prevSrc) {
95+
this._prevSrc = this.currentSrc
96+
dispatchNativeEvent(this.$el, 'pagestart', { url: this.currentSrc })
6197
}
6298
},
6399

64100
render (createElement) {
65101
return createElement('iframe', {
66102
attrs: {
67103
'weex-type': 'web',
68-
src: this.src
104+
src: this.currentSrc
69105
},
70106
on: {
71107
load: event => {
72-
dispatchNativeEvent(event.target, 'pagefinish', { url: this.src })
108+
this.$nextTick(function () {
109+
const el = this.$el
110+
try {
111+
const html = el.contentWindow.document.documentElement
112+
if (html) {
113+
dispatchNativeEvent(el, 'pagefinish', { url: this.currentSrc })
114+
}
115+
else {
116+
dispatchNativeEvent(el, 'error', new Error('[vue-render]:found no page content.'))
117+
}
118+
}
119+
catch (err) {
120+
dispatchNativeEvent(el, 'error', err)
121+
}
122+
})
73123
}
74124
},
75125
staticClass: 'weex-web weex-el',

0 commit comments

Comments
 (0)