This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(() => { | |
// find root element: nuxt instance, #app, first div child in <body> | |
const root = window?.$nuxt?.$el || document.getElementById('app') || [...document.body.children].find(i => i.tagName === 'DIV'); | |
const app = root?.__vue__; | |
if (!app || !window.__VUE_DEVTOOLS_GLOBAL_HOOK__) return 0; | |
const Vue = app.constructor; | |
const VueSuper = Vue.super || Vue; | |
VueSuper.config.devtools = true; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const express = require('express') | |
const wsServer = require('ws').Server | |
const PORT = 5566 | |
// 建立 express 物件並綁定在 port 5566 | |
const server = express().listen(PORT, () => { | |
console.log(`Listening on ${PORT}`) | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 建立一個 WebSocket 物件,並連上 socket server | |
const ws = new WebSocket('ws://localhost:5566') | |
// 連線建立後 | |
ws.onopen = () => { | |
console.log('open connection to server') | |
} | |
// 連線斷開後 | |
ws.onclose = () => { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
async fetch({ app, store }) { | |
app.$registerStore({ | |
module: MyModule, | |
moduleName: `myModule`, | |
store | |
}) | |
await store.dispatch('myModule/INIT_DATA') | |
}, | |
beforeCreate() { | |
this.$registerStore({ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
plugins: [ | |
{ src: '~/plugins/register-store' }, | |
... | |
], |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function registerStore({ module, moduleName, store }) { | |
const moduleIsRegistered = | |
store._modules.root._children[moduleName] !== undefined | |
const stateExists = store.state[moduleName] | |
if (!moduleIsRegistered) { | |
store.registerModule(moduleName, module, { preserveState: stateExists }) | |
} | |
} | |
export default ({ app }, inject) => { |