首页 > 解决方案 > Meteor 导入 SVG 作为 React 的组件

问题描述

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

根据这篇文章“在流星中导入svg文件”,这段代码应该可以解决问题:

服务器/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 导入的任何解决方案?

标签: reactjssvgmeteorasync-awaitassets

解决方案


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

根据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函数内,在常规函数或顶级代码中是不允许的。


推荐阅读