Comments (3)
YOUR_PROJECT_DIR/electron/preloader.js
const { contextBridge, ipcRenderer } = require('electron');
///////Do Not Edit////////
require("./node_modules/@capacitor-community/electron/dist/electron-bridge.js");
/////////////////////////
contextBridge.exposeInMainWorld('electron', {
ipc: { ...ipcRenderer }
})
YOUR_PROJECT_DIR/electron/src/index.ts
import { app, ipcMain } from "electron";
import { createCapacitorElectronApp } from "@capacitor-community/electron";
// The MainWindow object can be accessed via myCapacitorApp.getMainWindow()
const myCapacitorApp = createCapacitorElectronApp({
mainWindow: {
windowOptions: {
webPreferences: {
contextIsolation: true,
}
}
}
});
// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some Electron APIs can only be used after this event occurs.
app.on("ready", () => {
myCapacitorApp.init();
});
// Quit when all windows are closed.
app.on("window-all-closed", function () {
// On OS X it is common for applications and their menu bar
// to stay active until the user quits explicitly with Cmd + Q
if (process.platform !== "darwin") {
app.quit();
}
});
app.on("activate", function () {
// On OS X it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (myCapacitorApp.getMainWindow().isDestroyed()) myCapacitorApp.init();
});
// Define any IPC or other custom functionality below here
ipcMain.on('asynchronous-message', (event, arg) => {
console.log(arg) // prints "ping"
event.reply('asynchronous-reply', 'pong')
})
ipcMain.on('synchronous-message', (event, arg) => {
console.log(arg) // prints "ping"
event.returnValue = 'pong'
})
In your Ionic App [App.vue] (used vue as example):
<template>
<ion-app>
<ion-router-outlet />
</ion-app>
</template>
<script lang="ts">
import { IonApp, IonRouterOutlet } from '@ionic/vue';
import { defineComponent } from 'vue';
function isElectron() {
return window.electron !== null && window.electron !== undefined;
}
export default defineComponent({
name: 'App',
components: {
IonApp,
IonRouterOutlet
},
created() {
if (isElectron()) {
window.electron.ipc.on('asynchronous-reply', (event, arg) => {
console.log(arg) // prints "pong"
})
}
},
mounted() {
if (isElectron()) {
window.electron.ipc.send('asynchronous-message', 'ping')
console.log(window.electron.ipc.sendSync('synchronous-message', 'ping')) // prints "pong"
}
},
});
</script>
This is how I do it roughly, some other stuff but from here your editor or IDE will tell you if there are any issues that relate to your particular use.
Note: Questions are better suited to the Ionic forums rather than github issues
from electron.
So i tried this and i am getting the following error: window.electron.ipc.on is not a function
What am I missing here?
from electron.
I was able to get this to work using this const {ipcRenderer: ipc} = require('electron-better-ipc');
as the ipc
from electron.
Related Issues (20)
- Ability to Enable Chrome Experimental Features: Web Share
- Installation for Nuxt.js 2 not working and ERROR for Login features.
- How to implement @electron/remote? HOT 3
- capacitor.config.json does not exist, did you setup capacitor in your project root? HOT 3
- Opening Electron platformError: undefined HOT 2
- npx cap sync @capacitor-community/electron with Ionic7 Vite React or Vue Apps HOT 2
- Add capacitor v5 support HOT 1
- Lack of documentation for release 5.0.1 HOT 3
- Build error when using 5.0.1 HOT 7
- [CSP issues] refuses to load inline styles, I have no ideia what to add anymore.
- Add "skipLibCheck:true" to the generated tsconfig.json
- Cannot read properties of undefined (reading 'lastId')
- can't build ionic verion 7 with angular after add @capacitor-community/electron
- What is difference between electron-platform-template and electron-platform for deep linking?
- Including the package increases the build size by ~15 MB
- Splash screen in v5 seems to be broken HOT 3
- Plugin Examples Directory is missing HOT 1
- Error: chokidar FSWatcher incorrectly implements interface fs FSWatcher. HOT 1
- How to add tray icon?
- Error starting electron app on new projects (capacitor 6) : UnhandledPromiseRejection on /live-runner.js HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from electron.