使用的微信小程序 wxml 原生语言
1. 新建一个空白页面 例: index_PDF
注册事件
2. 在生命周期直接打开该pdf
onLoad(options) {
let that = this
wx.downloadFile({
url: 'pdf地址',
success: function (res) {
wx.openDocument({
filePath: res.tempFilePath,
fileType: 'pdf',
showMenu: true,
complete: function (res) {
that.setData({
is_show: true
})
}
})
}
})
},
注意 由于是一个新页面 用户进行返回时 肯定会切到当前的空白页面 所以在代码中声明了一个变量 is_show 初始值为false
onShow() {
// 读取is_show 值
let { is_show } = this.data
if (is_show) { // 如果is_show true 证明用户已经成功预览过pdf
wx.navigateBack({
delta: 1
})
}
},