首页 > 解决方案 > WebAssembly 代码未在现有 angularjs 应用程序中运行

问题描述

我已经在 rust 和 webAssembly 中编写了代码并在其中玩弄,我遇到了一些问题。

lib.rs文件

use wasm_bindgen::prelude::*;

#[wasm_bindgen]
extern {
    pub fn alert(s: &str);
}

#[wasm_bindgen]
pub fn greet(name: &str) {
    alert(&format!("Hello, {}!", name));
}

使用cargo将上述文件构建到wasm并部署到 NPM 注册表。 https://www.npmjs.com/package/@livspace-3d/hello-wasm

现在,我已经将相同的 npm 模块安装到我的应用程序中,使用npm i <package_name>

在 index.html 代码中使用 wasm 文件时出错,代码如下:

      <script>
            function fetchAndInstantiate() {  
                    fetch('./static/js/hello-wasm/pkg/hello_wasm_bg.wasm').then(response =>
                         response.arrayBuffer()
                    ).then(bytes =>{
                        console.error("I am herer2", JSON.stringify(bytes));
                        return WebAssembly.compile(bytes) 
                    }).then(mod =>{
                        console.error("I am herer3", mod);
                        mod.greet("WebAssembly");
                    });
                }
                fetchAndInstantiate();
        <script>

控制台打印:

在此处输入图像描述

标签: javascriptrustwebassemblyrust-cargo

解决方案


推荐阅读