Giter VIP home page Giter VIP logo

Comments (6)

caoxiemeihao avatar caoxiemeihao commented on May 19, 2024

没有跨域的,用 axios 或 fetch 在渲染进程中写
有跨域的,用 axios 或 node-fetch 在主进程中写

from electron-vite-vue.

yzqdev avatar yzqdev commented on May 19, 2024

好的,感谢大佬解答

from electron-vite-vue.

k1ngbanana avatar k1ngbanana commented on May 19, 2024

有三种方案,不知道大佬推荐用哪一种

preload中使用contextBridge,然后传递给渲染进程

使用netapi,request库或者fetch

//使用fetch,然后渲染进程使用window.getData.request(url);
contextBridge.exposeInMainWorld("getData", {
  async request(url: RequestInfo, init: RequestInit) {
    const res = await fetch(url, init);
    return await res.json();
  },
});

想请教一下你的contextBridge.exposeInMainWorld是写在preload.ts文件里面吗?然后怎么预加载的.我用用下面的方式加载不了preload文件里面的代码。

want to know where your contextBridge.exposeInMainWorld save. is it in preload.ts file? then how to preload it, i use the way below, but it doesn't load it actually.

const createWindow = () => {
  const win = new BrowserWindow({
    width: 1600,
    height: 800,
    webPreferences: {
      nodeIntegration: true,
      contextIsolation: false,
      preload: 'electron/preload.ts', // path.join(__dirname, 'preload.js') <- not build in dist, so failed too.
    },
  });

  if (process.env.VITE_DEV_SERVER_URL) {
    win.loadURL(process.env.VITE_DEV_SERVER_URL);
  } else {
    // load your file
    win.loadFile(path.join(__dirname, "../dist/index.html"));
  }

  win.webContents.openDevTools();
};

from electron-vite-vue.

caoxiemeihao avatar caoxiemeihao commented on May 19, 2024

使用 contextBridge.exposeInMainWorld 需要关闭 nodeIntegration

from electron-vite-vue.

k1ngbanana avatar k1ngbanana commented on May 19, 2024

使用 contextBridge.exposeInMainWorld 需要关闭 nodeIntegration

关闭了之后要怎么preload那个文件..我那个preload的地址一直加载不成功。然后暴露不了自定义的api

from electron-vite-vue.

caoxiemeihao avatar caoxiemeihao commented on May 19, 2024

Demo 不可能加载不到 preload 你还是改了什么东西。去看下实际生成的文件,这个问题不难多自己研究研究。

from electron-vite-vue.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.