Skip to content

[bug] Calling Rust from the frontend: invalid type #4583

@thep0y

Description

@thep0y

Describe the bug

I have a function defined in main.ts that needs to be called in the frontend:

#[tauri::command]
fn close_conn() -> i8 {
    -1
}

fn main() {
    let context = tauri::generate_context!();
    tauri::Builder::default()
        .menu(if cfg!(target_os = "macos") {
            tauri::Menu::os_default(&context.package_info().name)
        } else {
            tauri::Menu::default()
        })
        .invoke_handler(tauri::generate_handler![close_conn])
        .run(context)
        .expect("error while running tauri application");
}

And call it in App.vue:

<script setup lang="ts">
import HelloWorld from './components/HelloWorld.vue'
import { invoke } from "@tauri-apps/api"

invoke('close_conn')
  .then((code) => alert(code))
  .catch((error) => console.error(error))

But, I got an error:
image

Reproduction

No response

Expected behavior

No response

Platform and versions

Environment
  › OS: Windows 10.0.22000 X64
  › Webview2: 103.0.1264.44
  › MSVC:
      - Visual Studio Community 2022
  › Node.js: 16.15.1
  › npm: 8.5.2
  › pnpm: Not installed!
  › yarn: 1.22.19
  › rustup: 1.24.3
  › rustc: 1.62.0
  › cargo: 1.62.0
  › Rust toolchain: stable-x86_64-pc-windows-msvc

Packages
  › @tauri-apps/cli [NPM]: 1.0.1
  › @tauri-apps/api [NPM]: 1.0.1
  › tauri [RUST]: 1.0.2,
  › tauri-build [RUST]: 1.0.2,
  › tao [RUST]: 0.12.1,
  › wry [RUST]: 0.19.0,

App
  › build-type: bundle
  › CSP: unset
  › distDir: ../dist
  › devPath: http://localhost:3000/
  › framework: Vue.js

App directory structure
  ├─ .vscode
  ├─ dist
  ├─ node_modules
  ├─ public
  ├─ src
  └─ src-tauri

Stack trace

No response

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions