uni-app:demo&媒体文件&配置全局的变量(三)

简介: uni-app 是一个使用 Vue.js 构建多平台应用的框架,支持微信小程序、支付宝小程序、H5 和 App 等平台。本文档介绍了 uni-app 的基本用法,包括登录示例、媒体文件处理、全局变量配置和 Vuex 状态管理的实现。通过这些示例,开发者可以快速上手并高效开发多平台应用。

引言

uni-app 是一个跨平台的开发框架,它允许开发者使用 Vue.js 来构建应用程序,并能够同时发布到多个平台,如微信小程序、支付宝小程序、H5、App(通过DCloud的打包服务)等。uni-app 的目标是通过统一的代码库,简化多平台开发过程,提高开发效率。

demo

login

<template>
    <div class="page">
        <div class="login-box">
            <div class="title">登录中心</div>
            <!-- @submit="handleSubmit" -->
            <!-- <form> -->
            <div class=" my_form">
                <input class="uni-input account" placeholder="请输入账号" />
                <input class="uni-input password" placeholder="请输入密码" />
                <button type="primary" size="default" @click="handleSubmit">登录</button>
            </div>
            <!-- </form> -->
        </div>
    </div>
</template>
<script setup>
    import {
        useRouter
    } from 'vue-router';
    const router = useRouter();
    const handleSubmit = () => {
        router.push('/home');
    }
</script>
<style lang="scss">
    .page {
        width: 100vw;
        height: 100vh;
    }
    .title {
        font-size: 44rpx;
        font-family: "Microsoft YaHei", sans-serif;
        text-align: center;
        padding-top: 15%;
        padding-bottom: 30rpx;
    }
    .my_form .account,
    .password {
        background-color: white;
        height: 100rpx;
        width: 80%;
        border-radius: 10rpx;
        margin: 50rpx auto;
        box-shadow: 1rpx 2rpx 3rpx #e8e8e8;
        text-align: center;
    }
    .mini-btn {
        width: 80%;
        margin-top: 70rpx !important;
    }
</style>

媒体文件

video

<template>
    <view>
        <view class="uni-padding-wrap uni-common-mt">
            <view>
                <!-- 直接引用便可 -->
                <video id="myVideo" src="https://qiniu-web-assets.dcloud.net.cn/unidoc/zh/2minute-demo.mp4"
                    @error="videoErrorCallback" :danmu-list="danmuList" enable-danmu danmu-btn controls></video>
            </view>
            <!-- #ifndef MP-ALIPAY -->
            <view class="uni-list uni-common-mt">
                <view class="uni-list-cell">
                    <view>
                        <view class="uni-label">弹幕内容</view>
                    </view>
                    <view class="uni-list-cell-db">
                        <input v-model="danmuValue" class="uni-input" type="text" placeholder="在此处输入弹幕内容" />
                    </view>
                </view>
            </view>
            <view class="uni-btn-v">
                <button @click="sendDanmu" class="page-body-button">发送弹幕</button>
            </view>
            <!-- #endif -->
        </view>
    </view>
</template>
<script>
    export default {
        data() {
            return {
                src: '',
                danmuList: [{
                        text: '第 1s 出现的弹幕',
                        color: '#ff0000',
                        time: 1
                    },
                    {
                        text: '第 3s 出现的弹幕',
                        color: '#ff00ff',
                        time: 3
                    }
                ],
                danmuValue: ''
            }
        },
        onReady: function(res) {
            // #ifndef MP-ALIPAY
            this.videoContext = uni.createVideoContext('myVideo')
            // #endif
        },
        methods: {
            sendDanmu: function() {
                this.videoContext.sendDanmu({
                    text: this.danmuValue,
                    color: this.getRandomColor()
                });
                this.danmuValue = '';
            },
            videoErrorCallback: function(e) {
                uni.showModal({
                    content: e.target.errMsg,
                    showCancel: false
                })
            },
            getRandomColor: function() {
                const rgb = []
                for (let i = 0; i < 3; ++i) {
                    let color = Math.floor(Math.random() * 256).toString(16)
                    color = color.length == 1 ? '0' + color : color
                    console.log(color)
                    rgb.push(color)
                }
                return '#' + rgb.join('')
            }
        }
    }
</script>

配置一个全局的变量

main.js

在这个函数里面

然后便可以在另一个地方使用
<template>
    <view>{{$title}}</view>
</template>

app.vue

在 app.vue 里面 配置这个

globalData: {
            name: 'i am hero'
        },

然后在其他页面中使用

<script>
    var app = getApp()
    export default {
        data() {
            return {
                globalName: app.globalData.name
            }
        }
    }
</script>

vuex

先创建一个 store/index
// #ifndef VUE3
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
const store = new Vuex.Store({
    import {
        createStore
    } from 'vuex'
    const store = createStore({
        state: {
            colorList: ['#FF0000', '#00FF00', '#0000FF'],
            colorIndex: 0
        },
        mutations: {
            changeColor(state, color) {
                state.colorList[0] = color
            }
        },
        getters: {
            currentColor: (state) => {
                return state.colorList[state.colorIndex]
            }
        }
    })
    // #endif  
})
export default store

在main里面使用

import store from './store'
App.mpType = 'app'
const app = new Vue({
    store,
    ...App
})
在page里面使用
        computed: {
            ...mapState(['colorList']),
        },
        methods: {
            test() {
                console.log(this.colorList);
            }
        }
import {
    mapState,
    mapMutations
} from 'vuex';
var app = getApp()
import { createSSRApp } from 'vue'
import App from './App.vue'
import store from './store/index.js'
// #ifdef VUE3
export function createApp() {
  const app = createSSRApp(App)
  app.use(store) // 在Vue3中使用use方法来注册Vuex store
  return { app }
}
// #endif
// #ifndef VUE3
import Vue from 'vue'
import App from './App'
Vue.config.productionTip = false
const app = new Vue({
  store, // 在Vue2中直接将store配置传入Vue实例
  render: h => h(App)
})
app.$mount()
// #endif
相关文章
|
4月前
|
域名解析 网络协议 API
【Azure Container App】配置容器应用的缩放规则 Managed Identity 连接中国区 Azure Service Bus 问题
本文介绍了在 Azure Container Apps 中配置基于自定义 Azure Service Bus 的自动缩放规则时,因未指定云环境导致的域名解析错误问题。解决方案是在扩展规则中添加 `cloud=AzureChinaCloud` 参数,以适配中国区 Azure 环境。内容涵盖问题描述、原因分析、解决方法及配置示例,适用于使用 KEDA 实现事件驱动自动缩放的场景。
123 1
|
24天前
|
JavaScript 前端开发
uni-app中如何使用scss定义变量
使用SCSS在uni-app项目中定义和使用变量,不仅提高了样式代码的可维护性,还使得样式的修改和主题定制变得更加方便。只需更改变量值,就可以轻松改变整个应用的外观和风格。通过合理地组织和管理SCSS代码,可以大大提高开发效率和项目的可维护性。
87 9
|
7月前
|
人工智能 JSON 小程序
【一步步开发AI运动APP】七、自定义姿态动作识别检测——之规则配置检测
本文介绍了如何通过【一步步开发AI运动APP】系列博文,利用自定义姿态识别检测技术开发高性能的AI运动应用。核心内容包括:1) 自定义姿态识别检测,满足人像入镜、动作开始/停止等需求;2) Pose-Calc引擎详解,支持角度匹配、逻辑运算等多种人体分析规则;3) 姿态检测规则编写与执行方法;4) 完整示例展示左右手平举姿态检测。通过这些技术,开发者可轻松实现定制化运动分析功能。
|
5月前
|
C++ Windows
【Function App】本地通过VS Code调试Function时候遇见无法加载文件错误Microsoft.Extensions.Diagnostics.Abstractions
在使用 VS Code 调试 Azure Functions 时,执行 `func host start` 可能因版本冲突报错。错误信息显示 Rpc Initialization Service 启动失败,可能是由于缺少文件或组件导致。解决方法包括:1) 使用 npm 卸载并重新安装 Azure Functions Core Tools;2) 若问题未解决,重新下载安装包(如 func-cli-x64.msi)修复旧版本工具;3) 退出并重启 VS Code,重新加载项目即可恢复正常运行。参考资料链接提供了更多背景信息。
224 1
|
5月前
《仿盒马》app开发技术分享-- 首页活动配置(5)
上一篇文章中我们实现了项目端云一体化首页部分模块动态配置,实现了对模块模块的后端控制显示和隐藏,这能让我们的app更加的灵活,也能应对更多的情况。现在我们来对配置模块进行完善,除了已有的模块以外,我们还有一些banner ,活动入口等模块,这些模块的数据并不多,所以我们也归纳到配置中去实现。并且我们在配置表中添加了一些不同的id,我们只需要根据相对应的id 去查询对应的表就可以了
98 0
|
8月前
|
安全 算法 小程序
【03】微信支付商户申请下户到配置完整流程-微信开放平台创建APP应用-填写上传基础资料-生成安卓证书-获取Apk签名-申请+配置完整流程-优雅草卓伊凡
【03】微信支付商户申请下户到配置完整流程-微信开放平台创建APP应用-填写上传基础资料-生成安卓证书-获取Apk签名-申请+配置完整流程-优雅草卓伊凡
498 28
【03】微信支付商户申请下户到配置完整流程-微信开放平台创建APP应用-填写上传基础资料-生成安卓证书-获取Apk签名-申请+配置完整流程-优雅草卓伊凡
|
8月前
|
Swift iOS开发 开发者
苹果app上架-ios上架苹果商店app store 之苹果支付In - App Purchase内购配置-优雅草卓伊凡
苹果app上架-ios上架苹果商店app store 之苹果支付In - App Purchase内购配置-优雅草卓伊凡
1087 13
苹果app上架-ios上架苹果商店app store 之苹果支付In - App Purchase内购配置-优雅草卓伊凡
|
7月前
|
存储 监控 API
【Azure App Service】分享使用Python Code获取App Service的服务器日志记录管理配置信息
本文介绍了如何通过Python代码获取App Service中“Web服务器日志记录”的配置状态。借助`azure-mgmt-web` SDK,可通过初始化`WebSiteManagementClient`对象、调用`get_configuration`方法来查看`http_logging_enabled`的值,从而判断日志记录是否启用及存储方式(关闭、存储或文件系统)。示例代码详细展示了实现步骤,并附有执行结果与官方文档参考链接,帮助开发者快速定位和解决问题。
215 23
|
8月前
|
小程序
【04】微信支付商户申请下户到配置完整流程-微信开放平台移动APP应用通过-微信商户继续申请-微信开户函-视频声明-以及对公打款验证-申请+配置完整流程-优雅草卓伊凡
【04】微信支付商户申请下户到配置完整流程-微信开放平台移动APP应用通过-微信商户继续申请-微信开户函-视频声明-以及对公打款验证-申请+配置完整流程-优雅草卓伊凡
548 1
【04】微信支付商户申请下户到配置完整流程-微信开放平台移动APP应用通过-微信商户继续申请-微信开户函-视频声明-以及对公打款验证-申请+配置完整流程-优雅草卓伊凡

热门文章

最新文章