Wails Svelte Events question #4055
-
Hello,
Go:
It always produces this error: (error in Page.svelte is the line with runtime.Events.On |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
So I think the issue is with your registering of the event handler on the JS side. Here is my example using In my example its Here's my demo code App.svelte <script lang="ts">
import logo from './assets/images/logo-universal.png'
import {Greet} from '../wailsjs/go/main/App.js'
import {EventsOn} from "../wailsjs/runtime";
let resultText: string = "Please enter your name below 👇"
let name: string
let count: number
function greet(): void {
Greet(name).then(result => resultText = result)
}
EventsOn("tick", (message)=>{
count = message
})
</script>
<main>
<img alt="Wails logo" id="logo" src="{logo}">
<div class="result" id="result">
<p>{resultText}</p>
</div>
<div class="input-box" id="input">
<p>Count: {count}</p>
<input autocomplete="off" bind:value={name} class="input" id="name" type="text"/>
<button class="btn" on:click={greet}>Greet</button>
</div>
</main>
...
styles omitted app.go package main
import (
"context"
"fmt"
"github.com/wailsapp/wails/v2/pkg/runtime"
"time"
)
// App struct
type App struct {
ctx context.Context
}
// NewApp creates a new App application struct
func NewApp() *App {
return &App{}
}
// startup is called when the app starts. The context is saved
// so we can call the runtime methods
func (a *App) startup(ctx context.Context) {
a.ctx = ctx
go func() {
var count int = 0
for {
time.Sleep(3 * time.Second)
runtime.EventsEmit(ctx, "tick", count)
runtime.LogPrintf(ctx, "Tick [%d]", count)
count++
}
}()
}
// Greet returns a greeting for the given name
func (a *App) Greet(name string) string {
return fmt.Sprintf("Hello %s, It's show time!", name)
} |
Beta Was this translation helpful? Give feedback.
So I think the issue is with your registering of the event handler on the JS side. Here is my example using
wails init -n myproject -t svelte-ts
In my example its
runtime.EventsOn
whereas yours isruntime.Events.On
Here's my demo code
App.svelte