[add] EventBus使用 雙向溝通

This commit is contained in:
建喵 2022-08-01 17:12:37 +08:00
parent cf95786b9c
commit 7d4efe136f
9 changed files with 103927 additions and 134 deletions

View File

@ -21,21 +21,28 @@ export default class HelloWorld extends cc.Component {
// LIFE-CYCLE CALLBACKS:
// protected onLoad() {}
protected onLoad() {
let self = this;
this.Label.string = this.Text;
window["eventBus"].on('SendContent', (content: string) => {
self.Label.string = content;
})
}
public onClickBtn() {
let url: string = window.location.search;
let URLscheme: any[] = [];
if (url.indexOf("?") !== -1) {
let str = url.substring(1);
let strs = str.split("&");
for (let i = 0; i < strs.length; i++) {
URLscheme[strs[i].split("=")[0]] = decodeURI(strs[i].split("=")[1]);
}
}
if (URLscheme["host"]) {
opener?.window.parent.postMessage({ data: "hello" }, URLscheme["host"]);
window.close();
}
window["eventBus"].emit("Alert", "hello");
// let url: string = window.location.search;
// let URLscheme: any[] = [];
// if (url.indexOf("?") !== -1) {
// let str = url.substring(1);
// let strs = str.split("&");
// for (let i = 0; i < strs.length; i++) {
// URLscheme[strs[i].split("=")[0]] = decodeURI(strs[i].split("=")[1]);
// }
// }
// if (URLscheme["host"]) {
// opener?.window.parent.postMessage({ data: "hello" }, URLscheme["host"]);
// window.close();
// }
}
}

File diff suppressed because it is too large Load Diff

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -7,9 +7,13 @@
<link href="./src/assets/ZenMaruGothic-Regular.ttf" rel="stylesheet">
<!-- <link href="https://fonts.googleapis.com/css2?family=Zen+Maru+Gothic&display=swap" rel="stylesheet"> -->
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>BJ_ServerState</title>
<script type="module" crossorigin src="./assets/index.12a12ee9.js"></script>
<link rel="stylesheet" href="./assets/index.b312efde.css">
<title>cocos-vue-demo</title>
<script src="./static/js/EventBus.js" charset="utf-8"></script>
<script src="./static/cocos-build/web-mobile/src/settings.js"></script>
<script src="./static/cocos-build/web-mobile/cocos2d-js.js"></script>
<!-- <script src="./static/cocos-build/web-mobile/main.js"></script> -->
<script type="module" crossorigin src="./assets/index.a0e31fb5.js"></script>
<link rel="stylesheet" href="./assets/index.2f5031fd.css">
</head>
<body>

View File

@ -5,17 +5,20 @@ import 'vue-loading-overlay/dist/vue-loading.css';
import GameCanvas from './components/GameCanvas.vue';
import Popup from './components/Popup.vue';
const visible = ref(false)
const PopupVisible = ref(false)
const self = {
PopupVisible: PopupVisible
};
window["eventBus"].on('alert', (msg: string) => {
visible.value = true;
window["eventBus"].on('Alert', (msg: string) => {
PopupVisible.value = true;
})
</script>
<template>
<GameCanvas />
<el-dialog v-model="visible" :show-close="false">
<Popup />
<el-dialog v-model="PopupVisible" :show-close="false">
<Popup :APP="self" />
</el-dialog>
</template>

View File

@ -2,10 +2,17 @@
import { ref } from 'vue';
import 'vue-loading-overlay/dist/vue-loading.css';
const props = defineProps<{ APP: any }>()
const input = ref('')
function Send(content: string) {
window["eventBus"].emit("SendContent", content);
props.APP.PopupVisible.value = false;
}
</script>
<template>
<el-input v-model="input" placeholder="Please input A" />
<el-input v-model="input" placeholder="Please input B" />
<el-input v-model="input" placeholder="Please input Label" />
<el-button type="primary" @click.native="() => { Send(input) }" size="large" round>確定
</el-button>
</template>

View File

@ -67,24 +67,15 @@ window.__require = function e(t, n, r) {
_this.Btn = null;
return _this;
}
HelloWorld.prototype.onLoad = function() {
var self = this;
this.Label.string = this.Text;
window["eventBus"].on("SendContent", function(content) {
self.Label.string = content;
});
};
HelloWorld.prototype.onClickBtn = function() {
var url = window.location.search;
var URLscheme = [];
if (-1 !== url.indexOf("?")) {
var str = url.substring(1);
var strs = str.split("&");
for (var i = 0; i < strs.length; i++) URLscheme[strs[i].split("=")[0]] = decodeURI(strs[i].split("=")[1]);
}
// if (URLscheme["host"]) {
if (window.location.host) {
// null === opener || void 0 === opener ? void 0 : opener.window.parent.postMessage({
// window.frames.postMessage({
// data: "hello"
// }, window.location.host);
// window.close();
window.eventBus.emit("alert", "hello");
return;
}
window["eventBus"].emit("Alert", "hello");
};
__decorate([ property(cc.Label) ], HelloWorld.prototype, "Label", void 0);
__decorate([ property ], HelloWorld.prototype, "Text", void 0);