import svg files inside meteor

2019-07-14 05:58发布

I'm working on a project using meteor + react as front-and-back end.

For front-end UI, I am using element-react (https://eleme.github.io/element-react/#/en-US/quick-start) which is really cool and awesome. However when I tried to import element-react into my project (as instructed in the quick start of element-react homepage), meteor failed to compile static files and returned "Uncaught Error: Cannot find module './assets/error.svg''" which is the file do exist and has correct relative path.

Is there something missing or in meteor we simply can not use "require('./assets/error.svg')" to load a svg image?

1条回答
一夜七次
2楼-- · 2019-07-14 06:39

According to this post in Meteor's forum.

You can use something like Meteor methods and the Assets API to get most any data from your server though. Something like

/server/main.js

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

and

/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') 
查看更多
登录 后发表回答