uniapp vue3 上传视频组件封装

首先创建一个 components 文件在里面进行组件的创建 

下面是 vvideo组件的封装 也就是图片上传组件 只是我的命名是随便起的

<template>
	<!-- 上传视频 -->
	<view class="up-page">
		<!--视频-->
		<view class="show-box" v-for="(item1, index1) in videoList" :key="index1">
			<video class="full" :src="item1"></video>
			<view  class="delect-icon" @tap="delectVideo(index1)">
				<image class="full" :src="clearIcon" mode=""></image>
			</view>
		</view>
		<view v-if="VideoOfImagesShow" @tap="chooseVideoImage" class="box-mode">
			<image class="full" :src="selectfile" mode=""></image>
		</view>
	</view>
 
</template>

<script setup>  
import { ref } from 'vue';  
  
// 假设sourceType是一个外部定义或在其他地方已经处理的数组,这里我们直接使用  
// 如果它是动态的,你可能需要将其也转换为ref  
const sourceType = ref([['camera'], ['album'], ['camera', 'album']]);  
  
// 创建响应式数据  
const clearIcon = ref('../../static/xxx.png');  
const selectfile = ref('../../static/jiahao.png');  
const VideoOfImagesShow = ref(true);  
const imageList = ref([]);  
const videoList = ref([]);  
const sourceTypeOptions = ref(['拍摄', '相册', '拍摄或相册']);  
const sourceTypeIndex = ref(2);  
const cameraList = ref([{  
  value: 'back',  
  name: '后置摄像头',  
  checked: 'true'  
}, {  
  value: 'front',  
  name: '前置摄像头'  
}]);  
const cameraIndex = ref(0);  
const maxCount = ref(9);  
  
// 方法  
function chooseVideoImage() {  
  uni.showActionSheet({  
    title: '选择上传类型',  
    itemList: ['视频'], // 注意:这里我添加了'图片'选项,你可能需要调整你的逻辑来处理它  
    success: res => {  
      if (res.tapIndex === 0) {  
        chooseVideo();  
      } else if (res.tapIndex === 1) {  
        // 假设你有一个chooseImages方法来处理图片选择  
        // chooseImages();  
        console.log('选择图片');  
      }  
    }  
  });  
}  
  
function chooseVideo() {  
  uni.chooseVideo({  
    maxDuration: 60,  
    count: maxCount.value,  
    camera: cameraList.value[cameraIndex.value].value,  
    sourceType: sourceType.value[sourceTypeIndex.value],  
    success: res => {  
      videoList.value = [...videoList.value, res.tempFilePath];  
      if (imageList.value.length + videoList.value.length === maxCount.value) {  
        VideoOfImagesShow.value = false;  
      }  
      console.log(videoList.value);  
    }  
  });  
}  
  
function delectVideo(index) {  
  uni.showModal({  
    title: '提示',  
    content: '是否要删除此视频',  
    success: res => {  
      if (res.confirm) {  
        videoList.value.splice(index, 1);  
        VideoOfImagesShow.value = !(imageList.value.length + videoList.value.length >= maxCount.value);  
      }  
    }  
  });  
}  
</script>

<style lang="scss">
	/* 统一上传后显示的盒子宽高比 */
	.box-mode {
		width: 50vw;
		height: 60vw;
		
		border-radius: 8rpx;
		overflow: hidden;
	}
	
	.full {
		width: 100%;
		height: 100%;
	}
 
	.up-page {
		display: flex;
		flex-wrap: wrap;
		display: flex;
		width: 100%;
		.show-box:nth-child(3n){
			margin-right: 0;
		}
		.show-box {
			position: relative;
			margin-bottom:4vw;
			margin-right: 4vw;
			@extend .box-mode;
 
			.delect-icon {
				height: 40rpx;
				width: 40rpx;
				position: absolute;
				right: 0rpx;
				top: 0rpx;
				z-index: 1000;
			}
		}
	}
 
</style>

直接在页面引用

	<view class="videobox">
			<view class="example-body">
				<!-- <uni-file-picker limit="9"  file-mediatype="video" title="最多选择9个视频"></uni-file-picker> -->
				<div>选择视频-最多只能选择九个</div>
				<vvideo></vvideo>
			</view>
		</view>
		

最终样子

 

 

### 如何在 UniApp 中使用 Vue 以命令式方式封装组件 #### 创建基础结构 为了创建一个命令式的组件,在项目中定义一个新的 JavaScript 文件作为组件逻辑的容器。假设要创建名为 `MessageComponent.js` 的文件。 ```javascript // MessageComponent.js export default function (Vue, options) { const messageConstructor = Vue.extend({ data() { return { msg: &#39;&#39; } }, methods: { setMessage(message) { this.msg = message; } }, template: `<div>{{msg}}</div>` }); let instance; return { show(message) { if (!instance || !instance.$el.parentNode) { instance = new messageConstructor().$mount(); document.body.appendChild(instance.$el); } instance.setMessage(message); setTimeout(() => { if (instance && instance.$el.parentNode === document.body) { document.body.removeChild(instance.$el); instance = null; // 清除实例以便下次调用show方法时重新挂载 } }, 3000); // 自动关闭消息框的时间设置为3秒 } }; } ``` 此代码片段展示了如何通过扩展 Vue 构造器来构建自定义的消息显示功能[^2]。 #### 注册全局方法并初始化插件 为了让上述命令式组件能够在整个应用范围内被访问到,需将其注册成全局的方法: ```javascript import Message from &#39;./path/to/MessageComponent&#39;; const install = (Vue, opts = {}) => { Vue.prototype.$message = Message(Vue, opts); }; if (typeof window !== &#39;undefined&#39; && window.Vue) { window.Vue.use(install); } export default install; ``` 这段脚本实现了将 `$message` 方法注入至 Vue 实例原型链上,从而允许任何地方都能方便地调用它。 #### 使用命令式组件 最后一步是在页面或其他组件内部实际运用这个新建立的功能: ```html <template> <view class="example"> <!-- 页面其他部分 --> <button type="primary" @click="sendMessage">发送消息</button> </view> </template> <script> export default { name: "ExamplePage", methods: { sendMessage() { this.$message.show(&#39;这是一个测试消息&#39;); } } }; </script> ``` 以上就是完整的 uniapp vue 命令式 组件 封装 示例教程。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值