提交 f142f2f6 编写于 作者: X xty

添加iOS uts event-bus 示例

上级 c3334609
......@@ -1853,6 +1853,15 @@
"backgroundColorContent": "#fffae8"
}
},
// #ifdef APP-IOS
{
"path" : "pages/API/event-bus/uts-event-bus",
"style" :
{
"navigationBarTitleText" : ""
}
},
// #endif
// #ifdef APP
{
"path": "pages/privacy",
......
// uni-app自动化测试教程: https://uniapp.dcloud.net.cn/worktile/auto/hbuilderx-extension/
const PAGE_PATH = '/pages/API/event-bus/uts-event-bus'
if ( !process.env.uniTestPlatformInfo.toLowerCase().startsWith('ios')) {
it('pass', async () => {
expect(1).toBe(1);
});
return;
}
describe('event-bus', () => {
let page
beforeAll(async () => {
page = await program.reLaunch(PAGE_PATH)
await page.waitFor('view')
})
it('on', async () => {
await page.callMethod('clear')
await page.callMethod('JsOnUts')
await page.callMethod('emitFromUts')
const l1 = (await page.data()).log.length
expect(l1).toBeGreaterThan(0)
await page.callMethod('clear')
await page.callMethod('offUts')
await page.callMethod('emitFromUts')
const l2 = (await page.data()).log.length
expect(l2).toBe(0)
await page.callMethod('clear')
await page.callMethod('UtsOnJS')
await page.callMethod('emitFormJS')
const l3 = (await page.data()).log.length
expect(l3).toBeGreaterThan(0)
await page.callMethod('clear')
await page.callMethod('offJs')
await page.callMethod('emitFormJS')
const l4 = (await page.data()).log.length
console.log(l4)
expect(l4).toBe(0)
await page.callMethod('clear')
})
it('once', async () => {
await page.callMethod('clear')
await page.callMethod('JsOnUtsOnce')
await page.callMethod('emitFromUts')
const l1 = (await page.data()).log.length
expect(l1).toBeGreaterThan(0)
await page.callMethod('clear')
await page.callMethod('emitFromUts')
const l2 = (await page.data()).log.length
expect(l2).toBe(0)
await page.callMethod('clear')
await page.callMethod('UtsOnJSOnce')
await page.callMethod('emitFormJS')
const l3 = (await page.data()).log.length
expect(l3).toBeGreaterThan(0)
await page.callMethod('clear')
await page.callMethod('emitFormJS')
const l4 = (await page.data()).log.length
expect(l4).toBe(0)
await page.callMethod('clear')
})
})
<template>
<!-- #ifdef APP -->
<scroll-view class="page-scroll-view">
<!-- #endif -->
<view>
<button @click="JsOnUts">1. js监听uts消息</button>
<button @click="emitFromUts">2. uts中触发监听</button>
<button @click="offUts">取消uts消息监听</button>
<button @click="UtsOnJS">1. uts监听js消息</button>
<button @click="emitFormJS">2 .js中触发监听</button>
<button @click="offJs">取消js消息监听</button>
<button @click="clear">清空消息</button>
<view class="box">
<view>收到的消息:</view>
<view>
<view v-for="(item, index) in log" :key="index">{{ item }}</view>
</view>
</view>
</view>
<button @click="testAll">test all</button>
<!-- #ifdef APP -->
</scroll-view>
<!-- #endif -->
</template>
<script>
import {
onJsMessage,
offJsMessage,
emitUtsMessage,
getMessageChannel,
getRevJsMessage,
clearJsMessage,
onJsMessageOnce,
} from "@/uni_modules/uts-eventbus"
export default {
data() {
return {
log: [] as string[],
}
},
methods: {
fn(res : string, res2 : string) {
console.log("on rev: " + res)
this.log.push(res)
this.log.push(res2)
},
fn2(res : string) {
this.log.push(res)
},
JsOnUts() {
uni.$off(getMessageChannel(), this.fn)
uni.$on(getMessageChannel(), this.fn)
},
offUts() {
uni.$off(getMessageChannel(), this.fn)
},
emitFromUts() {
emitUtsMessage("emit form uts")
},
JsOnUtsOnce() {
uni.$once(getMessageChannel(), this.fn2)
},
UtsOnJS() {
onJsMessage("JsMessage")
},
UtsOnJSOnce() {
onJsMessageOnce("JsMessage")
},
offJs() {
offJsMessage("JsMessage")
},
emitFormJS() {
clearJsMessage()
uni.$emit("JsMessage", "emit form js")
let msg = getRevJsMessage()
console.log("message:"+msg)
if (msg && msg.length){
this.log.push(msg)
}
},
clear() {
clearJsMessage()
this.log.length = 0
},
testAll() {
this.JsOnUts();
this.emitFromUts();
this.UtsOnJS();
this.emitFormJS();
}
},
}
</script>
<style>
.box {
padding: 10px;
}
</style>
{
"id": "uts-eventbus",
"displayName": "uts-eventbus",
"version": "1.0.0",
"description": "uts-eventbus",
"keywords": [
"uts-eventbus"
],
"repository": "",
"engines": {
"HBuilderX": "^3.6.8"
},
"dcloudext": {
"type": "uts",
"sale": {
"regular": {
"price": "0.00"
},
"sourcecode": {
"price": "0.00"
}
},
"contact": {
"qq": ""
},
"declaration": {
"ads": "",
"data": "",
"permissions": ""
},
"npmurl": ""
},
"uni_modules": {
"dependencies": [],
"encrypt": [],
"platforms": {
"cloud": {
"tcb": "u",
"aliyun": "u",
"alipay": "u"
},
"client": {
"Vue": {
"vue2": "u",
"vue3": "u"
},
"App": {
"app-android": "u",
"app-ios": "u"
},
"H5-mobile": {
"Safari": "u",
"Android Browser": "u",
"微信浏览器(Android)": "u",
"QQ浏览器(Android)": "u"
},
"H5-pc": {
"Chrome": "u",
"IE": "u",
"Edge": "u",
"Firefox": "u",
"Safari": "u"
},
"小程序": {
"微信": "u",
"阿里": "u",
"百度": "u",
"字节跳动": "u",
"QQ": "u",
"钉钉": "u",
"快手": "u",
"飞书": "u",
"京东": "u"
},
"快应用": {
"华为": "u",
"联盟": "u"
}
}
}
}
}
\ No newline at end of file
# uts-eventbus
### 开发文档
[UTS 语法](https://uniapp.dcloud.net.cn/tutorial/syntax-uts.html)
[UTS API插件](https://uniapp.dcloud.net.cn/plugin/uts-plugin.html)
[UTS 组件插件](https://uniapp.dcloud.net.cn/plugin/uts-component.html)
[Hello UTS](https://gitcode.net/dcloud/hello-uts)
\ No newline at end of file
import { UniUTSJS } from 'DCloudUniappRuntime'
import { NSNumber } from 'Foundation';
let MessageChannel : string = "UtsMessage"
var revFromJS = ""
export function getMessageChannel() : string {
return MessageChannel
}
export function getRevJsMessage() : string {
return revFromJS
}
export function emitUtsMessage(arg : string) {
uni.$emit(MessageChannel, arg);
}
var callbackId : NSNumber | null = null
export function onJsMessage (messageChannel : string ) {
revFromJS = ""
if (callbackId != null) {
uni.$off(messageChannel, callbackId!)
callbackId = null
}
callbackId = uni.$on(messageChannel, function (...spreadArgs : Any) {
if (spreadArgs.length > 0) {
let arg0 = spreadArgs[0] as String | null
if ( arg0 != null ){
revFromJS = arg0!;
}
}
})
}
export function onJsMessageOnce (messageChannel : string ) {
revFromJS = ""
uni.$once(messageChannel, function (...spreadArgs : Any) {
if (spreadArgs.length > 0) {
let arg0 = spreadArgs[0] as String | null
if ( arg0 != null ){
revFromJS = arg0!;
}
}
})
}
export function offJsMessage(messageChannel : string) {
if (callbackId != null) {
uni.$off(messageChannel, callbackId!)
}
}
export function clearJsMessage() {
revFromJS = ""
}
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册