es6 eval模板字符串中缺少斜杠模板字符串:仅用于转义问题
问题描述:
我正在构建一个Electron应用程序,我使用的是webContents.executeJavaScipt(),它几乎是Electron浏览器的eval()。es6 eval模板字符串中缺少斜杠模板字符串:仅用于转义问题
module.js:472 Uncaught Error: Cannot find module 'C:UsersMichael Bruce AllenDocumentsGitHubschedule-crawl
enderer
emoteItems.js'
at Module._resolveFilename (module.js:470:15)
at Function.Module._resolveFilename (C:\Users\Michael Bruce Allen\Documents\GitHub\schedule-crawl\node_modules\electron\dist\resources\electron.asar\common\reset-search-paths.js:35:12)
at Function.Module._load (module.js:418:25)
at Module.require (module.js:498:17)
at require (internal/module.js:20:19)
at <anonymous>:3:25
at EventEmitter.electron.ipcRenderer.on (C:\Users\Michael Bruce Allen\Documents\GitHub\schedule-crawl\node_modules\electron\dist\resources\electron.asar\renderer\init.js:52:28)
at emitMany (events.js:127:13)
at EventEmitter.emit (events.js:201:7)
对我来说,似乎是一个逃避问题,对我说:直到我们把这个计划我的客户在Windows计算机上,这是错误我从来没有问题。所以,我打破了这下尽可能的简单,我想知道这里发生了什么:
const path = require('path');
const projectPath = `
console.log('${path.join(__dirname, "project_path")}');
`;
eval(projectPath);
我得到WINDOWS:(显然是错误的)
C:UsersMichael Bruce AllenDocumentsGitHubsandboxproject
在Linux上:(看上去很美)
/home/codeamend/Coding/projects/work/upwork/schedule-crawl/journal/learning/project_path
答
嗯,事实证明,有时你只需要知道什么类型的关键字用于搜索谷歌和找到你的答案。
发生了什么事情是Windows \正在逃离角色。尽管我知道这样的事情正在发生,但我不明白为什么。原来this stackexchange post帮我找到了答案。
这并不像我想要的那么漂亮,所以我会将我的实际项目重构为另一个不太冒险的解决方案。
const path = require('path');
const projectPath = `
console.log(${JSON.stringify(path.join(__dirname, "project_path"))});
`;
eval(projectPath);