繁体   English   中英

Lit-Element绑定到svg图像。 TypeError:无法读取null的属性'split'

[英]Lit-Element binding to svg image. TypeError: Cannot read property 'split' of null

我一直在尝试将一个简单的url(字符串)绑定到svg元素中的图像标记。
但是我收到以下错误: TypeError: Cannot read property 'split' of null并且浏览器中没有显示图片。

图像以及绑定在正常的<img>标记内正常工作并且没有错误,或者在<image/>元素中进行硬编码。

SVG示例:

import { LitElement, svg } from 'lit-element';

class AppDevice extends LitElement {
static get properties() {
    return {
        selectedImage: {
            type: String
        }
    };
}

constructor() {
    super();
    this.selectedImage = 'https://mdn.mozillademos.org/files/6457/mdn_logo_only_color.png';
}

render() {
    return svg`

        <svg width="600px" height="600px" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">

             <image xlink:href="${this.selectedImage}" id="canvas" x="176.32" y="145.932" width="252.068" height="252.068"/>
             <!-- Works -->
             <!-- <image xlink:href="https://mdn.mozillademos.org/files/6457/mdn_logo_only_color.png" id="canvas" x="176.32" y="145.932" width="252.068" height="252.068"/>      -->

        </svg>

`;
}
}

解决方法示例:

在阅读了这个问题的评论中指出的关于github的讨论后,我更新了我的答案:

在此输入图像描述

const namespaced = directive((namespace, value) => (part) => {
       part.committer.element.setAttributeNS(namespace, part.committer.name, value);
    });

const xlinkNamespace = 'https://mdn.mozillademos.org/files/6457/mdn_logo_only_color.png';

import { LitElement, html } from 'lit-element';

class AppDevice extends LitElement {
  static get properties() {
    return {
    };
   }

constructor() {
  super();
}

render() {
   return html`

    <svg width="600px" height="600px" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">

      <image xlink:href="${namespaced(xlinkNamespace, 'something')}" id="canvas" x="176.32" y="145.932" width="252.068" height="252.068"/>

    </svg>

    `;
}
}

由于我无法从评论中得到实例,我想出了一个与我的问题不同的解决方案。 虽然这不是我想要的确切答案,但我认为在这里发布它可能仍然是一个好主意。

我没有进一步讨论我的解决方案:
我利用css clip-path在我的SVG元素中显示图像,特别是能够通过绑定来切换它。

这是一篇关于剪裁和屏蔽的文章

<img .src="${this.selectedimg}" style="clip-path: url(#myClip);" width="250" height="250">

<svg>
  <defs>
    <clipPath id="myClip">
      <rect width="250" height="250"/>
    </clipPath>
  </defs>
</svg>

暂无
暂无

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

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