在流星反应项目中,我不能做一个明确的import svg from './path/to/file.svg'。
根据这篇文章"在流星中导入svg文件",这段代码应该可以解决这个问题:
server/main.js
Meteor.methods({
'svg.get'(data) {
return Assets.getText(data.path);
}
});client/main.js
const getSVG = async (path) => {
return await new Promise((resolve, reject) => {
Meteor.call('svg.get', { path }, (err, res) => {
if (err) reject('Something went wrong');
resolve(res);
});
});
}
const SVG = await getSVG('some/path/relative/to/private/file.svg') 但在Meteor 1.7+中,它并不适用于我,我得到了这样的错误:
I20180606-11:42:09.264(-3)? Exception while invoking method 'svg.get' Error: Unknown asset: /var/www/coreui-meteor-react/public/img/brand/logo.svg
I20180606-11:42:09.392(-3)? at getAsset (/var/www/coreui-meteor-react/.meteor/local/build/programs/server/boot.js:329:19)
I20180606-11:42:09.392(-3)? at Object.getText (/var/www/coreui-meteor-react/.meteor/local/build/programs/server/boot.js:340:16)
I20180606-11:42:09.393(-3)? at MethodInvocation.svg.get (server/main.js:6:21)
I20180606-11:42:09.393(-3)? at maybeAuditArgumentChecks (packages/ddp-server/livedata_server.js:1767:12)
I20180606-11:42:09.393(-3)? at DDP._CurrentMethodInvocation.withValue (packages/ddp-server/livedata_server.js:719:19)
I20180606-11:42:09.393(-3)? at Meteor.EnvironmentVariable.EVp.withValue (packages/meteor.js:1189:12)
I20180606-11:42:09.394(-3)? at DDPServer._CurrentWriteFence.withValue (packages/ddp-server/livedata_server.js:717:46)
I20180606-11:42:09.394(-3)? at Meteor.EnvironmentVariable.EVp.withValue (packages/meteor.js:1189:12)
I20180606-11:42:09.394(-3)? at Promise (packages/ddp-server/livedata_server.js:715:46)
I20180606-11:42:09.394(-3)? at new Promise (<anonymous>)
I20180606-11:42:09.395(-3)? at Session.method (packages/ddp-server/livedata_server.js:689:23)
I20180606-11:42:09.395(-3)? at packages/ddp-server/livedata_server.js:559:43即使我在这里添加了等待关键字:
const logo = await svg.get('/img/brand/logo.svg');它抛出此错误:
While building for web.browser:
client/containers/DefaultLayout/DefaultHeader.js:9:13: await is a reserved word (9:13)
While processing files with ecmascript (for target web.browser):
client/containers/DefaultLayout/DefaultHeader.js:9:13: /var/www/coreui-meteor-react/client/containers/DefaultLayout/DefaultHeader.js: await is a reserved word (9:13)
7 |
8 |
> 9 | const logo = await svg.get('/img/brand/logo.svg');
| ^
10 | const sygnet = await svg.get('/img/brand/sygnet.svg');
11 |
12 | const propTypes = {用于在Meteor-React项目中执行经典SVG导入的解决方案吗?
发布于 2018-06-07 12:33:37
您可能指定了错误的路径。
根据Assets.getText的文档,论点必须是:
相对于应用程序的私有子目录,资产的路径。
基于错误,您提供了/var/www/coreui-meteor-react/public/img/brand/logo.svg,您应该只执行img/brand/logo.svg,并将文件从public移到应用程序的private目录。
如果您想从public目录访问某个文件,可以使用直接链接在客户端上的任何时候进行访问。在本例中,your-app.com/img/brand/logo.svg
第二个错误是无关的。要使用await关键字,您的代码必须在async函数中,这在常规函数或顶级代码中是不允许的。
https://stackoverflow.com/questions/50728792
复制相似问题