提交 ed221f2c 编写于 作者: fxy060608's avatar fxy060608

重构进阶语法示例,同时支持vue2,vue3

上级 1de30723
<template> <template>
<view>
<button @click="testUtsSync">点击测试uts同步方法</button> <button @click="testUtsSync">点击测试uts同步方法</button>
<view>测试return: <view>测试return:
{{ format(testUtsSyncResult) }} {{ format(testUtsSyncResult) }}
...@@ -40,280 +41,282 @@ ...@@ -40,280 +41,282 @@
<view>测试success:{{ format(testUtsClassAsyncResult.success) }}</view> <view>测试success:{{ format(testUtsClassAsyncResult.success) }}</view>
<view>测试complete:{{ format(testUtsClassAsyncResult.complete) }}</view> <view>测试complete:{{ format(testUtsClassAsyncResult.complete) }}</view>
<button @click="testAll">点击测试所有</button> <button @click="testAll">点击测试所有</button>
</view>
</template> </template>
<script lang="ts" setup> <script>
import { ref, reactive } from "vue"; import {
import {
MAX, MAX,
testSync, testSync,
testSyncWithCallback, testSyncWithCallback,
testAsync, testAsync,
Test, Test,
} from "../../uni_modules/uts-syntaxcase"; } from "../../uni_modules/uts-syntaxcase";
console.log("MAX", MAX);
console.log("MAX", MAX); let test
const testUtsSyncResult = ref<boolean | null>(null); let id = 0
interface Result { export default {
return: boolean | null; data() {
success: boolean | null; return {
fail: boolean | null; testUtsSyncResult: null,
complete: boolean | null; testUtsSyncWithCallbackResult: {
} return: null,
const testUtsSyncWithCallbackResult = reactive<Result>({ success: null,
fail: null,
complete: null,
},
testUtsAsyncResult: {
return: null,
success: null,
fail: null,
complete: null,
},
testUtsClassConstructorResult: {
callback: null
},
testUtsClassStaticPropResult: null,
testUtsClassStaticSyncWithCallbackResult: {
return: null,
success: null,
fail: null,
complete: null,
},
testUtsClassStaticAsyncResult: {
return: null,
success: null,
fail: null,
complete: null,
},
testUtsClassPropResult: null,
testUtsClassSyncWithCallbackResult: {
return: null, return: null,
success: null, success: null,
fail: null, fail: null,
complete: null, complete: null,
}); },
const testUtsAsyncResult = reactive<Result>({ testUtsClassAsyncResult: {
return: null, return: null,
success: null, success: null,
fail: null, fail: null,
complete: null, complete: null,
}); }
function testAll() { }
testUtsSync(); },
testUtsSyncWithCallback(); methods: {
testUtsAsync(); format(v) {
testUtsClassConstructor(); return v == null ? "--" : v ? "通过" : "未通过";
testUtsClassStaticProp(); },
testUtsClassStaticSyncWithCallback(); testAll() {
testUtsClassStaticAsync(); this.testUtsSync();
testUtsClassProp(); this.testUtsSyncWithCallback();
testUtsClassSyncWithCallback(); this.testUtsAsync();
testUtsClassAsync(); this.testUtsClassConstructor();
} this.testUtsClassStaticProp();
function testUtsSync() { this.testUtsClassStaticSyncWithCallback();
testUtsSyncResult.value = false; this.testUtsClassStaticAsync();
this.testUtsClassProp();
this.testUtsClassSyncWithCallback();
this.testUtsClassAsync();
},
testUtsSync() {
this.testUtsSyncResult = false;
try { try {
if (testSync("dcloud").msg === "hello dcloud") { if (testSync("dcloud").msg === "hello dcloud") {
testUtsSyncResult.value = true; this.testUtsSyncResult = true;
} }
} catch (e) { } catch (e) {
console.error("testUtsSync", e); console.error("testUtsSync", e);
} }
} },
function testUtsSyncWithCallback() { testUtsSyncWithCallback() {
try { try {
testUtsSyncWithCallbackResult.return = false; this.testUtsSyncWithCallbackResult.return = false;
testUtsSyncWithCallbackResult.success = false; this.testUtsSyncWithCallbackResult.success = false;
// testUtsSyncWithCallbackResult.fail = false; // testUtsSyncWithCallbackResult.fail = false;
testUtsSyncWithCallbackResult.complete = false; this.testUtsSyncWithCallbackResult.complete = false;
if ( if (
testSyncWithCallback({ testSyncWithCallback({
type: "success", type: "success",
success(res) { success: (res) => {
console.log("testSyncWithCallback.success.callback", res); console.log("testSyncWithCallback.success.callback", res);
testUtsSyncWithCallbackResult.success = true; this.testUtsSyncWithCallbackResult.success = true;
}, },
fail(res) { fail: (res) => {
console.log("testSyncWithCallback.fail.callback", res); console.log("testSyncWithCallback.fail.callback", res);
// testUtsSyncWithCallbackResult.fail = true; // testUtsSyncWithCallbackResult.fail = true;
}, },
complete(res) { complete: (res) => {
console.log("testSyncWithCallback.complete.callback", res); console.log("testSyncWithCallback.complete.callback", res);
testUtsSyncWithCallbackResult.complete = true; this.testUtsSyncWithCallbackResult.complete = true;
}, },
}).name === "testSyncWithCallback" }).name === "testSyncWithCallback"
) { ) {
testUtsSyncWithCallbackResult.return = true; this.testUtsSyncWithCallbackResult.return = true;
} }
} catch (e) { } } catch (e) {}
} },
async function testUtsAsync() { async testUtsAsync() {
testUtsAsyncResult.return = false; this.testUtsAsyncResult.return = false;
testUtsAsyncResult.success = false; this.testUtsAsyncResult.success = false;
// testUtsAsyncResult.fail = false; // testUtsAsyncResult.fail = false;
testUtsAsyncResult.complete = false; this.testUtsAsyncResult.complete = false;
try { try {
const res = await testAsync({ const res = await testAsync({
type: "success", type: "success",
success(res) { success: (res) => {
console.log("testAsync.success.callback", res); console.log("testAsync.success.callback", res);
testUtsAsyncResult.success = true; this.testUtsAsyncResult.success = true;
}, },
fail(res) { fail: (res) => {
console.log("testAsync.fail.callback", res); console.log("testAsync.fail.callback", res);
}, },
complete(res) { complete: (res) => {
console.log("testAsync.complete.callback", res); console.log("testAsync.complete.callback", res);
testUtsAsyncResult.complete = true; this.testUtsAsyncResult.complete = true;
}, },
}); });
if (res.name === "testAsync") { if (res.name === "testAsync") {
testUtsAsyncResult.return = true; this.testUtsAsyncResult.return = true;
} }
} catch (e) { } } catch (e) {}
} },
function format(v: boolean | null) { testUtsClassConstructor() {
return v == null ? "--" : v ? "通过" : "未通过"; this.testUtsClassConstructorResult.callback = false
}
let test: Test
let id = 0
const testUtsClassConstructorResult = reactive<{ callback: boolean | null }>({
callback: null
});
function testUtsClassConstructor() {
testUtsClassConstructorResult.callback = false
id++ id++
test = new Test(id, { test = new Test(id, {
name: 'name' + id, callback: (res) => { name: 'name' + id,
callback: (res) => {
console.log(res) console.log(res)
testUtsClassConstructorResult.callback = true this.testUtsClassConstructorResult.callback = true
} }
}) })
} },
const testUtsClassStaticPropResult = ref<boolean | null>(null); testUtsClassStaticProp() {
function testUtsClassStaticProp() { this.testUtsClassStaticPropResult = false
testUtsClassStaticPropResult.value = false
if (Test.type === 'Test') { if (Test.type === 'Test') {
testUtsClassStaticPropResult.value = true this.testUtsClassStaticPropResult = true
} }
} },
const testUtsClassStaticSyncWithCallbackResult = reactive<Result>({ testUtsClassStaticSyncWithCallback() {
return: null,
success: null,
fail: null,
complete: null,
});
function testUtsClassStaticSyncWithCallback() {
try { try {
testUtsClassStaticSyncWithCallbackResult.return = false; this.testUtsClassStaticSyncWithCallbackResult.return = false;
testUtsClassStaticSyncWithCallbackResult.success = false; this.testUtsClassStaticSyncWithCallbackResult.success = false;
// testUtsClassStaticSyncWithCallbackResult.fail = false; // testUtsClassStaticSyncWithCallbackResult.fail = false;
testUtsClassStaticSyncWithCallbackResult.complete = false; this.testUtsClassStaticSyncWithCallbackResult.complete = false;
if ( if (
Test.testClassStaticSyncWithCallback({ Test.testClassStaticSyncWithCallback({
type: "success", type: "success",
success(res) { success: (res) => {
console.log("testStaticSyncWithCallback.success.callback", res); console.log("testStaticSyncWithCallback.success.callback", res);
testUtsClassStaticSyncWithCallbackResult.success = true; this.testUtsClassStaticSyncWithCallbackResult.success = true;
}, },
fail(res) { fail: (res) => {
console.log("testStaticSyncWithCallback.fail.callback", res); console.log("testStaticSyncWithCallback.fail.callback", res);
// testUtsClassStaticSyncWithCallbackResult.fail = true; // testUtsClassStaticSyncWithCallbackResult.fail = true;
}, },
complete(res) { complete: (res) => {
console.log("testStaticSyncWithCallback.complete.callback", res); console.log("testStaticSyncWithCallback.complete.callback", res);
testUtsClassStaticSyncWithCallbackResult.complete = true; this.testUtsClassStaticSyncWithCallbackResult.complete = true;
}, },
}).name === "testSyncWithCallback" }).name === "testSyncWithCallback"
) { ) {
testUtsClassStaticSyncWithCallbackResult.return = true; this.testUtsClassStaticSyncWithCallbackResult.return = true;
} }
} catch (e) { } } catch (e) {}
} },
const testUtsClassStaticAsyncResult = reactive<Result>({ async testUtsClassStaticAsync() {
return: null, this.testUtsClassStaticAsyncResult.return = false;
success: null, this.testUtsClassStaticAsyncResult.success = false;
fail: null,
complete: null,
});
async function testUtsClassStaticAsync() {
testUtsClassStaticAsyncResult.return = false;
testUtsClassStaticAsyncResult.success = false;
// testUtsClassStaticAsyncResult.fail = false; // testUtsClassStaticAsyncResult.fail = false;
testUtsClassStaticAsyncResult.complete = false; this.testUtsClassStaticAsyncResult.complete = false;
try { try {
const res = await Test.testClassStaticAsync({ const res = await Test.testClassStaticAsync({
type: "success", type: "success",
success(res) { success: (res) => {
console.log("testAsync.success.callback", res); console.log("testAsync.success.callback", res);
testUtsClassStaticAsyncResult.success = true; this.testUtsClassStaticAsyncResult.success = true;
}, },
fail(res) { fail: (res) => {
console.log("testAsync.fail.callback", res); console.log("testAsync.fail.callback", res);
}, },
complete(res) { complete: (res) => {
console.log("testAsync.complete.callback", res); console.log("testAsync.complete.callback", res);
testUtsClassStaticAsyncResult.complete = true; this.testUtsClassStaticAsyncResult.complete = true;
}, },
}); });
if (res.name === "testAsync") { if (res.name === "testAsync") {
testUtsClassStaticAsyncResult.return = true; this.testUtsClassStaticAsyncResult.return = true;
} }
} catch (e) { } } catch (e) {}
} },
testUtsClassProp() {
const testUtsClassPropResult = ref<boolean | null>(null);
function testUtsClassProp() {
if (!test) { if (!test) {
testUtsClassConstructor() this.testUtsClassConstructor()
} }
testUtsClassPropResult.value = false this.testUtsClassPropResult = false
if (test.id > 0) { if (test.id > 0) {
testUtsClassPropResult.value = true this.testUtsClassPropResult = true
} }
} },
const testUtsClassSyncWithCallbackResult = reactive<Result>({ testUtsClassSyncWithCallback() {
return: null,
success: null,
fail: null,
complete: null,
});
function testUtsClassSyncWithCallback() {
if (!test) { if (!test) {
testUtsClassConstructor() this.testUtsClassConstructor()
} }
try { try {
testUtsClassSyncWithCallbackResult.return = false; this.testUtsClassSyncWithCallbackResult.return = false;
testUtsClassSyncWithCallbackResult.success = false; this.testUtsClassSyncWithCallbackResult.success = false;
// testUtsClassSyncWithCallbackResult.fail = false; // testUtsClassSyncWithCallbackResult.fail = false;
testUtsClassSyncWithCallbackResult.complete = false; this.testUtsClassSyncWithCallbackResult.complete = false;
if ( if (
test.testClassSyncWithCallback({ test.testClassSyncWithCallback({
type: "success", type: "success",
success(res) { success: (res) => {
console.log("testSyncWithCallback.success.callback", res); console.log("testSyncWithCallback.success.callback", res);
testUtsClassSyncWithCallbackResult.success = true; this.testUtsClassSyncWithCallbackResult.success = true;
}, },
fail(res) { fail: (res) => {
console.log("testSyncWithCallback.fail.callback", res); console.log("testSyncWithCallback.fail.callback", res);
// testUtsClassSyncWithCallbackResult.fail = true; // testUtsClassSyncWithCallbackResult.fail = true;
}, },
complete(res) { complete: (res) => {
console.log("testSyncWithCallback.complete.callback", res); console.log("testSyncWithCallback.complete.callback", res);
testUtsClassSyncWithCallbackResult.complete = true; this.testUtsClassSyncWithCallbackResult.complete = true;
}, },
}).name === "testSyncWithCallback" }).name === "testSyncWithCallback"
) { ) {
testUtsClassSyncWithCallbackResult.return = true; this.testUtsClassSyncWithCallbackResult.return = true;
} }
} catch (e) { } } catch (e) {}
} },
const testUtsClassAsyncResult = reactive<Result>({
return: null, async testUtsClassAsync() {
success: null,
fail: null,
complete: null,
});
async function testUtsClassAsync() {
if (!test) { if (!test) {
testUtsClassConstructor() this.testUtsClassConstructor()
} }
testUtsClassAsyncResult.return = false; this.testUtsClassAsyncResult.return = false;
testUtsClassAsyncResult.success = false; this.testUtsClassAsyncResult.success = false;
// testUtsClassAsyncResult.fail = false; // testUtsClassAsyncResult.fail = false;
testUtsClassAsyncResult.complete = false; this.testUtsClassAsyncResult.complete = false;
try { try {
const res = await test.testClassAsync({ const res = await test.testClassAsync({
type: "success", type: "success",
success(res) { success: (res) => {
console.log("testAsync.success.callback", res); console.log("testAsync.success.callback", res);
testUtsClassAsyncResult.success = true; this.testUtsClassAsyncResult.success = true;
}, },
fail(res) { fail: (res) => {
console.log("testAsync.fail.callback", res); console.log("testAsync.fail.callback", res);
}, },
complete(res) { complete: (res) => {
console.log("testAsync.complete.callback", res); console.log("testAsync.complete.callback", res);
testUtsClassAsyncResult.complete = true; this.testUtsClassAsyncResult.complete = true;
}, },
}); });
if (res.name === "testAsync") { if (res.name === "testAsync") {
testUtsClassAsyncResult.return = true; this.testUtsClassAsyncResult.return = true;
}
} catch (e) {}
}
}
} }
} catch (e) { }
}
</script> </script>
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册