Html 设置<;img>;在高分子元素中

Html 设置<;img>;在高分子元素中,html,css,image,data-binding,polymer,Html,Css,Image,Data Binding,Polymer,下面是我的聚合物元素的代码 我已经尝试了正常的dom绑定,一切都很好 然而,img的src不起作用。 我在chrome开发工具中检查了html,结果显示src完全错误 这里的src表示/img/12.jpg,这意味着图像位于html文件所在的文件夹中 但是,它指的是文件夹的根目录 开发工具中的预期src: 观察src:img/12.jpg 除了将其编码到预期的src之外,我还能做什么 <dom-module id="image-hover"> <tem

下面是我的聚合物元素的代码 我已经尝试了正常的dom绑定,一切都很好

然而,img的src不起作用。 我在chrome开发工具中检查了html,结果显示src完全错误

这里的src表示/img/12.jpg,这意味着图像位于html文件所在的文件夹中 但是,它指的是文件夹的根目录

开发工具中的预期src:

观察src:img/12.jpg

除了将其编码到预期的src之外,我还能做什么

<dom-module id="image-hover">
             <template>
                <img src="{{imgsrc}}"/>
             </template>
        </dom-module>
        <script>
          (function() {
            Polymer({
              is: 'image-hover',

              properties: {
                imgsrc: {
                    type: String,
                    value: 'img/12.jpg'
                }
              }
          });
          })();
       </script>

(功能(){
聚合物({
是:'图像悬停',
特性:{
imgsrc:{
类型:字符串,
值:“img/12.jpg”
}
}
});
})();
编辑:我使用polymer提供的内容标签解决了这个问题

<content id="image" select="img"></content>


问题仍然存在,我如何才能找出元素所在文件夹的来源。

您可以执行以下操作:

<dom-module id="image-hover">
    <template>
        <img src="{{imgsrc}}"/>
    </template>
</dom-module>
<script>
    Polymer({
        is: 'image-hover',

        properties: {
            imgsrc: {
                type: String,
                value: function () {
                    return location.host + "/img/12.jpg";
                }
            }
        }
    });
</script>

聚合物({
是:'图像悬停',
特性:{
imgsrc:{
类型:字符串,
值:函数(){
返回location.host+“/img/12.jpg”;
}
}
}
});
值现在是一个使用location对象返回
字符串的函数。关于这一点,可以找到更多信息。简而言之,它包含有关当前URL的信息。

您可以按如下方式使用:

          properties: {
            imgsrc: {
                type: String,
                value: function() {return this.resolveUrl('img/12.jpg')}
            }
          }

在我的例子中,location.host返回localhost:3000而不是localhost:3000/elements/image hover您是否尝试过执行
value:function(){return”/image/12.jpg”;}
?不起作用,我认为不应该。。。路径是/elements/img/12.jpg即使如此,我也不想要一个硬编码的解决方案。。。更一般的