Vue-cli: How to get base64 img source after webpack?

Created on 25 Feb 2016  ·  5Comments  ·  Source: vuejs/vue-cli

i write something like this:

<img :src='rtrImg(ava)'/>

rtrImg (sex) {
      if (sex === '女') {
        return '../img/girl.jpg'
      } else {
        return '../img/boy.jpg'
      }
    }

i cant get the img resource because img has turn to base64 code.
how can i get these source in method inside ?
or any other way to write?

Most helpful comment

return require('../img/girl.jpg')

The require will be handled by file-loader and return the after-build file location.

All 5 comments

Maybe pass one more attribute? Like data-src. Or you can just disable webpack's url-loader for images.

return require('../img/girl.jpg')

The require will be handled by file-loader and return the after-build file location.

1.<img :src="icon_src" />
2.import the image at the top level
import icon from 'icon.png';

  1. data () { return { icon_src:icon } }
    or computed instead

What if I got a base64 bytes array from a restful service?
Can I do

> ```js

return require('../img/girl.jpg')
```

The require will be handled by file-loader and return the after-build file location.

尤大, vue-cli3项目集成electron-vue 桌面系统, svg 怎么显示不出来,

import Vue from 'vue'
import SvgIcon from '@/components/SvgIcon'// svg component

// register globally
Vue.component('svg-icon', SvgIcon)

const req = require.context('./svg', false, /\.svg$/)
// const requireAll = requireContext => requireContext.keys().map(requireContext)

const requireAll = requireContext => requireContext.keys().map((key) => {
  console.log(key, req(key))
})
requireAll(req)

j转成了base64位图片
image

正常的情况是
image

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jgribonvald picture jgribonvald  ·  3Comments

chasegiunta picture chasegiunta  ·  3Comments

eladcandroid picture eladcandroid  ·  3Comments

BusyHe picture BusyHe  ·  3Comments

Gonzalo2683 picture Gonzalo2683  ·  3Comments