| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 |
- <template>
- <view class="web-view-container">
- <web-view :src="webUrl" @message="handleMessage" @error="handleError"></web-view>
- </view>
- </template>
- <script setup>
- import { ref } from 'vue';
- import { onBackPress,onLoad } from '@dcloudio/uni-app';
- // 页面参数
- const webUrl = ref('');
- // 页面加载时接收参数
- onLoad((options) => {
- // 从跳转入参中获取url,如果没有则使用默认地址
- if (options.url) {
- // 解码URL,防止参数传递时的编码问题
- webUrl.value = decodeURIComponent(options.url);
- } else {
- // 默认地址,可以根据实际需求修改
- webUrl.value = 'about:blank';
- }
- });
- // 返回上一页
- function goBack() {
- uni.navigateBack();
- }
- // 处理web-view发送的消息
- function handleMessage(e) {
- console.log('web-view message:', e);
- // 可以根据实际需求处理来自web-view的消息
- }
- // 处理web-view加载错误
- function handleError(e) {
- console.error('web-view load error:', e);
- // 可以在这里添加错误处理逻辑,如显示错误页面等
- }
- // 监听返回按钮事件
- onBackPress(() => {
- goBack();
- return true;
- });
- </script>
- <style scoped lang="scss">
- .web-view-container {
- width: 100%;
- height: 100vh;
- background-color: #f5f5f5;
- }
- web-view {
- flex: 1;
- width: 100%;
- }
- </style>
|