Meteor将SVG作为React组件导入

3

在Meteor-React项目中,我无法通过import svg from './path/to/file.svg'进行显式导入。

根据这篇文章“import svg files inside meteor”,下面的代码应该可以解决问题:

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

即使我在这里添加了await关键字:
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项目中,有没有从React进行经典SVG导入的解决方案?
1个回答

2
您可能指定了错误的路径。
根据Assets.getText文档,参数必须是:
应用程序的“private”子目录相对于资源的路径。
基于错误信息,您提供了“/var/www/coreui-meteor-react/public/img/brand/logo.svg”,而应该只使用“img/brand/logo.svg”,并将文件从“public”移动到应用程序的“private”目录中。
如果您想随时在客户端访问某个文件夹中的文件,请使用直接链接。在这种情况下,“your-app.com/img/brand/logo.svg”。
第二个错误与此无关。要使用“await”关键字,您的代码必须在“async”函数内部,不能在常规函数或顶级代码中使用。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接