最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Image source binding in Aurelia - Stack Overflow

programmeradmin6浏览0评论

I am trying to bind the src attribute of an img tag in an aurelia ponent, how can I do so?

I'm creating some images in a reapeat.for loop this way:

<img repeat.for="picture of infoboard.memberPictures" src.bind="picture">

In which, the memberPictures array es from the view model, and the value of picture is a relative address: ../../../assets/pictures/img_avatar.png.

In the view model, I fetch members' info from the database and by processing the data, populate the memberPictures array this way:

this.httpClient.fetch(`boards/membersof/${this.infoboard.id}`)
      .then(response => response.json())
      .then(data => {
        this.infoboard.memberPictures = data.result.map(element => `../../../assets/pictures/${element.profile_pic}.png`);
      });

Binding the address this way, the images do not load, like this:

and also the browser console shows the following error:

img_avatar.png:1 GET http://localhost:8080/assets/pictures/img_avatar.png 404 (Not Found)

when inspecting the elements, the picture tag for member avatar is this:

<img src.bind="picture" class="au-target" au-target-id="10" src="../../../assets/pictures/img_avatar.png">

But if we provide the image source with a static image with the exact same address generated in the above example as shown below:

<img repeat.for="picture of infoboard.memberPictures" src.bind="../../../assets/pictures/img_avatar.png">

there will be no problem:

and now by inspecting elements there is different result:

<img src="/a5e81f19cf2c587876fd1bb08ae0249f.png">

Apparently, there is a difference in handling static files in aurelia. How is the image source changed this way, and what is the correct way of binding the image source?

I am trying to bind the src attribute of an img tag in an aurelia ponent, how can I do so?

I'm creating some images in a reapeat.for loop this way:

<img repeat.for="picture of infoboard.memberPictures" src.bind="picture">

In which, the memberPictures array es from the view model, and the value of picture is a relative address: ../../../assets/pictures/img_avatar.png.

In the view model, I fetch members' info from the database and by processing the data, populate the memberPictures array this way:

this.httpClient.fetch(`boards/membersof/${this.infoboard.id}`)
      .then(response => response.json())
      .then(data => {
        this.infoboard.memberPictures = data.result.map(element => `../../../assets/pictures/${element.profile_pic}.png`);
      });

Binding the address this way, the images do not load, like this:

and also the browser console shows the following error:

img_avatar.png:1 GET http://localhost:8080/assets/pictures/img_avatar.png 404 (Not Found)

when inspecting the elements, the picture tag for member avatar is this:

<img src.bind="picture" class="au-target" au-target-id="10" src="../../../assets/pictures/img_avatar.png">

But if we provide the image source with a static image with the exact same address generated in the above example as shown below:

<img repeat.for="picture of infoboard.memberPictures" src.bind="../../../assets/pictures/img_avatar.png">

there will be no problem:

and now by inspecting elements there is different result:

<img src="/a5e81f19cf2c587876fd1bb08ae0249f.png">

Apparently, there is a difference in handling static files in aurelia. How is the image source changed this way, and what is the correct way of binding the image source?

Share Improve this question edited Sep 3, 2024 at 19:55 Galiold asked Aug 4, 2019 at 15:47 GalioldGaliold 1131 silver badge7 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 9

This is because you are using webpack to bundle your project.

One of the things webpack does, is pack all of your static files (images, fonts etc.) into the bundle - and then replacing all the static references with a different "url" that points to the same asset in the bundle.

At run time, you don't have access to things that are out of the bundle.

By the way, this is why we need to use PLATFORM.moduleName() for all aurelia ponents, because webpack doesn't pick these up by default.

In your case, you are binding the img tag to dynamic urls. Webpack doesn't have any way of bundling them into the output bundle, because those url's are generated at runtime.

You need to use the require keyword for this to work at runtime for a directory structure like this:

export class App {
  public urls:string[] = ["test", "t1", "t2", "t3"];

  getUrl(name)
  {
    return require(`./assets/${name}.png`);
  }
}
<template>
  <img repeat.for="url of urls" src.bind="getUrl(url)">
</template>

Edit:

in your case, simply use:

this.httpClient.fetch(`boards/membersof/${this.infoboard.id}`)
      .then(response => response.json())
      .then(data => {
        this.infoboard.memberPictures = data.result.map(element => require(`../../../assets/pictures/${element.profile_pic}.png`));
      });
发布评论

评论列表(0)

  1. 暂无评论