打開網頁的方法有兩種第一種是最簡單的微信官方提供的方法,直接把要打開的網頁地址賦給web-view標簽的src屬性
-
<web-view src="{{article}}"> </web-view>
第二種需要引入一個第三方插件,下面的寫法只適用于wepy框架中,其他框架中寫法略有不同。
-
<template>
-
<view>
-
//插件中的固定寫法
-
<import src="../../wxParse/wxParse.wxml" />
-
<template is="wxParse" data="{{wxParseData:article.nodes}}" />
-
<view wx:if='article' class='addclass'></view>
-
</view>
-
</template>
-
<script>
-
//引入插件
-
import WxParse from "../../wxParse/wxParse";
-
export default class webview extends wepy.page {
-
data = {
-
//網頁地址路徑
-
article: '',
-
}
-
methods = {
-
}
-
async onLoad(options) {
-
let ret = await api.rentalContracts({
-
id: this.id,
-
method: 'GET'
-
});
-
this.article = ret.data
-
//調用插件中的方法設置article中的網頁路徑
-
WxParse.wxParse('article', 'html', this.article, this, 1);
-
}
-
}
打開phf文件給按鈕定義一個preview方法,在downloadFile方法中調用wx.openDocument方法就可以實現。
-
preview() {
-
let that=this.
-
wx.downloadFile({
-
url: 'https://www.*******.com/contract/default/pdf',
-
success: function(res) {
-
console.log(res)
-
var Path = res.tempFilePath //返回的文件臨時地址,用于后面打開本地預覽所用
-
that.webview=Path
-
wx.openDocument({
-
filePath: Path,
-
success: function(res) {
-
console.log('打開文檔成功')
-
}
-
})
-
},
-
fail: function(res) {
-
console.log(res)
-
}
-
})
-
},
|