No More Posting New Topics!

If you have a question or an issue, please start a thread in our Github Discussions Forum.
This forum is closed for new threads/ topics.

Navigation

    Quasar Framework

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search

    Get dynamic image from assets folder

    Help
    8
    14
    16667
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • H
      hata last edited by

      I cannot get dynamic image from assets folder
      ex: <img :src="/assets/${account.avatar}">
      Anyone can help me or guideline some tips?

      1 Reply Last reply Reply Quote 0
      • Jon
        Jon last edited by Jon

        Is this in a plain HTML template, or are you using some custom templating engine instead?

        If the former, then I don’t think ${foo} does what you think it does. Try this instead…

        <img :src="'/assets/' + account.avatar" />

        1 Reply Last reply Reply Quote 0
        • H
          hata last edited by

          It is the same syntax, cannot get image from assets, i think it is the webpack loader issue, anyone can help me?

          1 Reply Last reply Reply Quote 0
          • rstoenescu
            rstoenescu Admin last edited by

            @hata Check publicPath from /config/index.js.

            1 Reply Last reply Reply Quote 0
            • Martin
              Martin last edited by

              @hata Here’s some more clarification
              http://quasar-framework.org/guide/app-handling-static-assets.html

              @rstoenescu @Jon
              Will handling dynamic paths with :src work in the assets folder?
              <img :src="'/assets/' + account.avatar" />

              I know it does work in the statics folder
              <img :src="'/statics/' + account.avatar" />

              1 Reply Last reply Reply Quote 0
              • Jon
                Jon last edited by

                TBH I didn’t pay attention to the path, just the ${} syntax. My bad, sorry.

                1 Reply Last reply Reply Quote 0
                • H
                  hata last edited by

                  @rstoenescu : I think publishPath for static folder ? I have a slider with many image and i would like load image dynamic by webpack to optimize performance when display slider

                  rstoenescu 1 Reply Last reply Reply Quote 0
                  • rstoenescu
                    rstoenescu Admin @hata last edited by

                    Some notes on assets and statics:

                    • publicPath matters for Webpack in determining if a resource is static or dynamic (dynamic meaning it’s handled by Webpack as an asset of your app) and you must set taking into account Vue Router mode (hash or history) as the two modes work differently in respect to URL path
                    • Images in assets should be used as src="~assets/...." OR set a relative path to the file you’re specifing as source
                    • Webpack + Vue has a limitation on what can be considered an asset of your app or a static. In order for your resource to be considered an asset (and be optimized and included by Webpack) you must NOT use Vue dynamic props (such as :src – note the dot in front of src for this HTML attribute). If you use a dynamic prop then Webpack won’t be able to know which asset you will actually use at runtime, so it skips it.
                    • Webpack rule: when specifying the source, if this source starts with / then it’s considered a static. So <img src="/statics/my_image.png"> uses a static. If instead we omit / and write <img src="statics/my_image.png"> then Webpack tries to manage it as a relative path (to the file containing this tag) and include it as an asset.

                    To summarize:

                    • As a general rule of thumb: if you want to include an asset, hard-code it in src WITHOUT using a Vue dynamic prop (making it :src).
                    • Vue/Quasar components using images (like image gallery as an example) uses a binded :src for the images, so these images must be in statics folder. Due to Webpack + Vue limitations, you won’t be able to make Webpack consider them as assets.
                    1 Reply Last reply Reply Quote 0
                    • E
                      Emptyless last edited by

                      @rstoenescu when using the Electron wrapper you cannot set publicPath to ‘/’, this does mean however that using statics in the form of ‘/statics/some-image.png’ will not work. I’ve solved this by using relative paths instead of the ‘/statics’ directory. However this is not sustainable for a large Electron application, is it possible to use the ‘/statics’ directory in Electron in some way?

                      1 Reply Last reply Reply Quote 0
                      • rstoenescu
                        rstoenescu Admin last edited by

                        Don’t set publicPath to ‘/’ and don’t use Vue Router’s “history” mode. They are not compatible with Electron architecture.
                        Use ‘statics’ (notice no ‘/’ in front of statics).

                        1 Reply Last reply Reply Quote 1
                        • M
                          MagoSchmidt last edited by

                          is there something similar to “string resources” for me to manage localization and flavours messages?

                          1 Reply Last reply Reply Quote 0
                          • M
                            mas last edited by

                            I’ve read the article on handling assets and statics but still can’t get it work right.
                            I’m trying to render images in a v-for loop in a list, tried different variations:

                            <q-item-side><img :src="‘statics/faces/’+images.id+’.jpg’" /></q-item-side>

                            or

                            <q-item-side :image="‘statics/faces/’+images.id+’.jpg’"/>

                            However, I get rendered only the first image in the looping for all list items, as if I hard-coded <img src="‘statics/faces/1.jpg’" /> instead of dynamic path.Does anyone know how to solve that?

                            1 Reply Last reply Reply Quote 0
                            • Hawkeye64
                              Hawkeye64 last edited by Hawkeye64

                              I’m guessing what your code looks like…
                              HTML:

                              <q-item v-for="image in images" ...
                                  <q-item-side :image="getImage(image)"/>
                              </q-item>
                              

                              CODE:

                              methods: {
                                  getImage: function (imageData) {
                                      return `statics/faces/${imageData.id}.jpg`
                                  }
                              }
                              
                              1 Reply Last reply Reply Quote 0
                              • M
                                mas last edited by

                                Hawkeye64, only that I wasn’t creating a separate method to built image path, I was doing that in HTML. However, looks like I found the cause. The images.id was a number and :image="‘statics/faces/’+images.id+’.jpg’", the syntax I used, didn’t convert it into a string.

                                1 Reply Last reply Reply Quote 0
                                • First post
                                  Last post