4***@qq.com
4***@qq.com
  • 发布:2026-06-23 16:58
  • 更新:2026-06-23 16:59
  • 阅读:39

uniapp 扫码不做整个页面跳转,只在页面固定部分出现扫码框

分类:uni-app

uniapp 扫码不做整个页面跳转,只在页面固定部分出现扫码框。
我发现uni.scanCode只能整个页面跳转,我需要页面不跳转,获取扫码内容。

这是我的示例如:

<!-- pages/index/index.vue -->
<template>
<view class="container">
<button type="primary" @click="handleScan">点击扫一扫</button>
<view v-if="scanResult" class="result">
<text>上次扫码结果: {{ scanResult }}</text>
</view>
<view v-else-if="!isAppPlatform" class="tip">
<text>提示:扫码功能仅在 App 环境下有效,请在真机上运行测试。</text>
</view>
</view>
</template>

<script setup>
import { ref, computed } from 'vue';
// 注意:移除了错误的 import { uni } ...

const scanResult = ref('');

// 检查当前平台是否为 App (Android/iOS)
const isAppPlatform = computed(() => {
// uni 对象是全局的,可以直接使用
const platform = uni.getSystemInfoSync().platform;
return platform === 'android' || platform === 'ios';
});

function handleScan() {
if (!isAppPlatform.value) {
console.warn('当前环境不支持扫码功能,请在 App 真机上运行。');
uni.showModal({
title: '提示',
content: '扫码功能仅在 App 环境下有效,请在真机上运行测试。',
showCancel: false
});
return; // 如果不是 App 平台,则直接返回,不执行扫码
}

scanQRCode();
}

function scanQRCode() {
uni.scanCode({
scanType: ['qrCode', 'barCode'],
success: (res) => {
console.log('扫码成功:', res);
scanResult.value = res.result;
uni.showModal({
title: '扫码结果',
content: res.result,
showCancel: false,
confirmText: '确定'
});
},
fail: (err) => {
console.error('扫码失败:', err);
if (err.cancel) {
console.log('用户取消扫码');
uni.showToast({
title: '已取消',
icon: 'none'
});
} else {
// 检查是否是 "not supported" 错误
if (err.errMsg && err.errMsg.includes('not supported')) {
console.warn('当前环境不支持扫码 API。');
uni.showModal({
title: '提示',
content: '当前环境不支持扫码功能,请在 App 真机上运行测试。',
showCancel: false
});
} else {
uni.showToast({
title: '扫码失败,请重试',
icon: 'error'
});
}
}
},
complete: () => {
console.log('扫码操作结束(无论成功失败)');
}
});
}

</script>

<style scoped>
.container {
padding: 20px;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
min-height: 100vh;
box-sizing: border-box;
}

.result {
margin-top: 20px;
padding: 10px;
background-color: #f0f0f0;
border-radius: 4px;
word-break: break-all;
}

.tip {
margin-top: 20px;
padding: 10px;
background-color: #fff3cd; / 浅黄色背景提醒 /
border-radius: 4px;
color: #856404; / 深黄色文字 /
text-align: center;
font-size: 14px;
}
</style>

2026-06-23 16:58 负责人:无 分享
已邀请:
Ask小助手

Ask小助手 - Ask社区AI助手

欢迎到专业群( uni-app 官方技术交流群 1 ) 咨询,群中有相关专业的管理员和群友。

针对你的需求——在页面固定区域嵌入扫码框,而不跳转全屏,标准的 uni.scanCode API 确实无法实现,因为它只能调起系统全屏扫码界面。

不过,在 uni-app 的 App 端(Android 和 iOS)有几种可行的实现方案:

方案一:使用 nvue 页面的 barcode 组件(推荐)

这是 uni-app 官方提供的组件,专用于 nvue 页面,可以像普通组件一样嵌入到页面布局中,实现自定义扫码区域。

实现步骤:

  1. 创建一个 nvue 页面(如 pages/scan/scan.nvue
<template>  
  <view class="scan-container">  
    <!-- barcode 组件可以像普通 view 一样布局 -->  
    <barcode   
      ref="barcode"  
      class="barcode"   
      :autostart="true"  
      :filters="[0,1,2]"   
      @marked="onSuccess"  
      @error="onError"  
    />  
    <!-- 可以在扫码框周围添加自定义 UI -->  
    <view class="scan-tips">  
      <text>将二维码/条形码放入框内</text>  
    </view>  
  </view>  
</template>  

<script>  
export default {  
  data() {  
    return {  
      scanned: false  
    }  
  },  
  methods: {  
    onSuccess(e) {  
      const result = e.detail.result  
      if (this.scanned) return  
      this.scanned = true  

      // 通过事件通信将结果传回上一页  
      uni.$emit('scanResult', result)  
      uni.navigateBack()  
    },  
    onError(e) {  
      console.error('扫码错误:', e)  
    }  
  }  
}  
</script>  

<style>  
.scan-container {  
  flex: 1;  
  position: relative;  
}  
.barcode {  
  width: 500rpx;  
  height: 500rpx;  
  margin: 100rpx auto;  
}  
.scan-tips {  
  position: absolute;  
  bottom: 100rpx;  
  width: 100%;  
  text-align: center;  
  color: #fff;  
}  
</style>

本助手使用国产模型,生成代码可能不准确。专业的代码生成需要全球顶尖的AI Coding模型。推荐使用uni-agent

  1. 在原页面中监听扫码结果
// 在你的原页面中  
function handleScan() {  
  // 监听扫码结果  
  uni.$on('scanResult', (data) => {  
    scanResult.value = data  
    uni.$off('scanResult') // 移除监听  
  })  

  // 跳转到 nvue 扫码页  
  uni.navigateTo({  
    url: '/pages/scan/scan'  
  })  
}

本助手使用国产模型,生成代码可能不准确。专业的代码生成需要全球顶尖的AI Coding模型。推荐使用uni-agent

方案二:使用 plus.barcode 原生控件(vue 页面)

在 App 端,可以使用 5+ API 的 plus.barcode.create 创建原生扫码控件,直接嵌入到当前页面中,无需跳转。

<template>  
  <view class="container">  
    <button type="primary" @click="handleScan">点击扫一扫</button>  
    <!-- 扫码区域容器 -->  
    <view id="scan-box" class="scan-box"></view>  
    <view v-if="scanResult" class="result">  
      <text>扫码结果: {{ scanResult }}</text>  
    </view>  
  </view>  
</template>  

<script setup>  
import { ref } from 'vue'  

const scanResult = ref('')  
let barcode = null  

function handleScan() {  
  // #ifdef APP-PLUS  
  if (!barcode) {  
    // 创建扫码控件  
    barcode = plus.barcode.create('barcode', [plus.barcode.QR, plus.barcode.EAN13, plus.barcode.EAN8], {  
      top: '100px',  
      left: '10%',  
      width: '80%',  
      height: '300px',  
      position: 'static',  
      frameColor: '#00FF00',  
      scanbarColor: '#00FF00'  
    })  

    // 扫码成功回调  
    barcode.onmarked = function(type, result) {  
      console.log('扫码成功:', type, result)  
      scanResult.value = result  
      // 扫码成功后可以停止扫码  
      barcode.cancel()  
    }  

    // 扫码错误回调  
    barcode.onerror = function(error) {  
      console.error('扫码错误:', error)  
    }  

    // 将扫码控件添加到当前页面  
    const currentWebview = uni.getSystemInfoSync().platform === 'ios'   
      ? plus.webview.currentWebview()   
      : plus.webview.currentWebview()  
    currentWebview.append(barcode)  
  }  

  // 开始扫码  
  barcode.start()  
  // #endif  
}  

// 页面卸载时清理  
onUnmounted(() => {  
  if (barcode) {  
    barcode.close()  
    barcode = null  
  }  
})  
</script>  

<style scoped>  
.scan-box {  
  width: 80%;  
  height: 300px;  
  margin: 20px auto;  
  background-color: #000;  
}  
</style>

本助手使用国产模型,生成代码可能不准确。专业的代码生成需要全球顶尖的AI Coding模型。推荐使用uni-agent

方案三:使用插件市场的扫码插件

如果对扫码效果和 UI 自定义要求较高,推荐使用插件市场的专业扫码插件,它们通常支持更好的识别效果和更灵活的 UI 定制。

可以在 插件市场 搜索扫码相关插件,很多插件都支持在页面中嵌入扫码框,并提供丰富的自定义选项。

注意事项

  1. 平台限制:方案一仅支持 App-nvue 页面,方案二仅支持 App 端(Android/iOS),其他平台(小程序、H5)需要使用各自的实现方式
  2. 权限配置:确保在 manifest.json 中配置了相机权限
  3. 性能考虑:扫码结束后记得调用 cancel()close() 方法释放资源
  4. uni-app x 推荐:如果条件允许,官方推荐使用 uni-app x,其扫码效果和 UI 自定义性都更优

根据你的需求,推荐使用方案一(nvue + barcode 组件),这是官方推荐且最稳定的做法。

内容为 AI 生成,仅供参考

要回复问题请先登录注册