Meteor将SVG作为React的组件导入

时间:2018-06-06 20:12:33

标签: reactjs svg meteor async-await assets

我不能在Meteor-React项目中做一个明确的import svg from './path/to/file.svg'

根据这篇文章" import svg files inside meteor",这段代码应该可以解决问题:

服务器/ main.js

Meteor.methods({
    'svg.get'(data) {
        return Assets.getText(data.path);
    }
});

的客户机/ 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 个答案:

答案 0 :(得分:1)

您可能指定了错误的路径。

根据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函数中,不能在常规函数或顶级代码中使用。

相关问题