Golang wails How to send data from go to js when dom ready and window is opened

1k Views Asked by At

what i need to do in domReady func or ...?

func (a App) domReady(ctx context.Context) {
    
}

and in index.html

<script></script>

Cant understand how to send data from go to js when dom ready and window is opened

Thx.

2

There are 2 best solutions below

0
Diego Stéfano On

You have to define the Go function you want to call on the event DOMContentLoaded as a member function of the App structure, for instance:

func (a *App) OnDOMContentLoaded(arg1 string) string {
    return arg1;
}

After compiling, this function will be available on the "frontend\wailsjs\go\main\App.js" file, and you can import it from your JS/TS files:

// assuming your JS file is in "frontend/src".
import {OnDOMContentLoaded} from '../wailsjs/go/main/App';

The compiled Go functions imported from JS return a promise with their return value. Then, inside your JS source, you just have to call the imported function on the listener of the DOMContentLoaded event:

document.addEventListener('DOMContentLoaded', (event) => {
    OnDOMContentLoaded('Hi!').then((result) => alert(result));
});
0
steven On

use this at the golang back end

runtime.EventsEmit(a.ctx, "app:apikey", chatString)

javascript front end below

import { EventsOn } from '../wailsjs/runtime/runtime';

EventsOn('app:apikey', (data) => {

} hope this helps