index.vue 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. <template>
  2. <web-view :webview-styles="webStyle" :style="webStyle" :src="pdfSrc" v-if="pdfSrc"/>
  3. </template>
  4. <script setup>
  5. import {ref, watch, onMounted} from "vue";
  6. //初始变量
  7. const props = defineProps({
  8. ui: {
  9. type: Object,
  10. default: () => ({})
  11. },
  12. src: String,
  13. });
  14. const windowInfo = uni.getWindowInfo()
  15. const pdfSrc = ref('')
  16. const webStyle = ref({})
  17. //渲染完成
  18. onMounted(() => {
  19. setPdfSrc(props.src)
  20. setWebStyle(props.ui)
  21. })
  22. //监听变化
  23. watch(() => [
  24. props.src
  25. ], ([src]) => {
  26. setPdfSrc(src)
  27. })
  28. //监听样式变化
  29. watch(() => [
  30. props.ui
  31. ], ([ui]) => {
  32. setWebStyle(ui)
  33. }, {deep: true})
  34. //设置pdf的url
  35. const setPdfSrc = (src) => {
  36. const url = '/hybrid/html/pdf/index.html?src='
  37. if (src) {
  38. pdfSrc.value = url + src
  39. } else {
  40. pdfSrc.value = ''
  41. }
  42. }
  43. //设置webview的样式
  44. const setWebStyle = (data) => {
  45. const {windowHeight} = windowInfo
  46. webStyle.value = {
  47. ...data,
  48. height: data.height ?? windowHeight + 'px'
  49. }
  50. }
  51. </script>