简体   繁体   English

自定义元素 - 图像源未定义

[英]custom Element - image src undefined

I´m creating a customElement for a Instagram template, and i´m trying to make the image src a variable in order to change it as you want.我正在为 Instagram 模板创建一个 customElement,并且我正在尝试使图像 src 成为变量,以便根据需要进行更改。 And when i'm stating the attributes im calling correctly where the image is.当我说明属性时,我正确调用了图像所在的位置。 When I load the page the image doesn't charge and when I inspect the code it says that the image src it's undefined.当我加载页面时,图像不收费,当我检查代码时,它说图像 src 它是未定义的。

class IgTemplate extends HTMLElement {

  static get observedAttributes() {
    return ['username', 'location', 'caption', 'mainImg'];
  }

  constructor() {
    super();
    this.attachShadow({ mode: 'open' });
  }
  
  connectedCallback() {
    this.render();
  }

  attributeChangedCallback(propName, oldValue, newValue) {
    this[propName] = newValue;
    this.render();
  }

  render() {
    this.shadowRoot.innerHTML = `
    <link rel="stylesheet" href="./src/components/Igtemplate/style.css">
    <section>
    <div class="page">
<div class="above">
    <div class="userInform">
        <div class="userImg">
        <img src="./assets/profile-icon.png" alt="" class="cover">
        </div>
        <h3>${this.username}<br><span>${this.location}</span></h3>
    </div>
    <div>
    <img src="./assets/three-dots.png" alt="" class="menu">
    </div>
</div>
<div class="mainPics">
    <image class="cover"src="${this.mainImg}">
</div>
<div class="theButton">
    <div class="left">
        <img src="./assets/like-icon.png" alt="" class="btnLike">
        <img src="./assets/comment-icon.png" alt="" class="btnComment">
        <img src="./assets/share-icon.png" alt="">
    </div>
    <div class="rigth">
        <img src="./assets/save-icon.png" alt="" class="btnSave">
    </div>
</div>
<h4 class="likes">420.389 likes</h4>
<h4 class="captions"><b>${this.username} </b>${this.caption}</h4>
<h4 class="comments">View all 348 comments</h4>
<div class="addComment">
    <div class="userImg">
        <img src="./assets/profile-icon.png" alt="">
    </div>
    <input type="text" class="theComment" placeholder="Add a comment..."></input>
</div>
<h5 class="time">28 minutes ago</h5>
</div>
    </section>` 
  }
}

customElements.define('ig-template', IgTemplate);
export default IgTemplate;

And here is the other part这是另一部分

class AppContainer extends HTMLElement{
    constructor(){
        super();
        this.attachShadow({mode: 'open'});
    }

    connectedCallback(){
        this.render();
    }

    render(){
        this.shadowRoot.innerHTML = `
        <ig-template 
        username="a.solarte1" 
        caption="Final MIC 2022" 
        location="Estadio de Palamós" 
        mainImg="./assets/mainImg.png">
        </ig-template>

        <br>
        
        <ig-template 
        username="a.solarte1" 
        caption="Final MIC 2022" 
        location="Estadio de Palamós" 
        mainImg="./assets/mainImg.png">
        </ig-template>
        `
    }
}

customElements.define("app-container",AppContainer);

Here´s what I see on screen:这是我在屏幕上看到的:

在此处输入图像描述

In your IgTemplate class you forgot to inisilialise the image for the first time, you can do that easily in the constructor在您的 IgTemplate class 中,您第一次忘记初始化图像,您可以在构造函数中轻松完成

class IgTemplate extends HTMLElement {

  constructor() {
    super();
    this.mainImg = "./assets/mainImg.png";// put the src of the default/starting image
    this.attachShadow({ mode: 'open' });
  }

When the attribute of an object is never set, it stays undefined !当 object 的属性从未设置时,它保持未定义!

Problem问题

this.mainImg is undefined , and you never set it. this.mainImgundefined ,你从来没有设置它。

Why?为什么?

Attributes are always all-lower case, so in your attributeChangedCallback the mainImg attribute comes in as mainimg (doesn't matter which case you provided in observedAttributes ), hence you are setting this['mainimg'] (and not this['mainImg'] ).属性总是全小写,因此在您的attributeChangedCallbackmainImg属性以mainimg的形式出现(与您在observedAttributes中提供的情况无关),因此您正在设置this['mainimg'] (而不是this['mainImg'] )。 For Object property names (and variables) case matters in JS.对于 Object 属性名称(和变量)案例在 JS 中很重要。

How to fix怎么修

So rename your attribute to mainimg , and adjust your template:因此,将您的属性重命名为mainimg ,并调整您的模板:

<image class="cover" src="${this.mainimg}" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM