Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Polymer 聚合:调用动态自定义元素';从另一个脚本中删除方法_Polymer - Fatal编程技术网

Polymer 聚合:调用动态自定义元素';从另一个脚本中删除方法

Polymer 聚合:调用动态自定义元素';从另一个脚本中删除方法,polymer,Polymer,我正在尝试调用动态自定义元素的方法。只有在自定义元素的html文件的脚本标记内进行调用时,它才起作用。但是当在另一个自定义元素的script标记或index.html的script标记中进行调用时,我得到错误:“methodname”不是控制台中的函数。谢谢你的回复。对于上下文,下面是一个片段 // in my custom element html file .... <script type="text/javascript">

我正在尝试调用动态自定义元素的方法。只有在自定义元素的html文件的脚本标记内进行调用时,它才起作用。但是当在另一个自定义元素的script标记或index.html的script标记中进行调用时,我得到错误:“methodname”不是控制台中的函数。谢谢你的回复。对于上下文,下面是一个片段

        // in my custom element html file
         ....

        <script type="text/javascript">

           Polymer( {
             is: "my-new-view",

             toggleContent: function() {
                 this.$.collapse.toggle();
              },

           insertContent: function (userContent) {
           console.log("inserting userContent...");

           }

         });
         </script> 
       </dom-module>
    </html>


    Now  in another file my-app.html
    ...
   <link rel="import" href="my-new-view.html">
   ...
   <dom-module is="my-app">
       ...
      <script>
      ...
        // i want to test my-new-view. insertContent() here.
        var dynamicView = document.createElement('my-new-view');
         // in the following line i get the error insertContent is
         // not a function       
         dynamicView.insertContent();      
       </script>
  </dom-module>
//在我的自定义元素html文件中
....
聚合物({
是:“我的新观点”,
toggleContent:function(){
这是$.collapse.toggle();
},
insertContent:函数(userContent){
log(“插入用户内容…”);
}
});
现在在另一个文件my-app.html中
...
...
...
...
//我想测试一下我的新观点。此处插入内容()。
var dynamicView=document.createElement('my-new-view');
//在下面的一行中,我得到错误insertContent是
//不是功能
dynamicView.insertContent();
请帮忙。我做错了什么。我也尝试了index.html中的最后两行javascript,但得到了相同的错误。谢谢


我认为,当元素尚未注册时,可能您试图调用该方法太早了

index.html
中,您可以将代码包装到
WebComponentsReady
事件的处理程序中

window.addEventListener('WebComponentsReady', function(e) {
  document.createElement('my-new-view').insertContent();
});
在其他聚合元素中,您可以将代码移动到
myapp
元素中,而不是直接移动到脚本中


另外,为了检查自定义元素是否可用,我查看了
document.createElement('my-new-view').constructor
。如果它说
函数HTMLElement(){[native code]}
(在Chrome中),则表示它不可用(通常缺少导入)。

对于初学者,您缺少函数的参数userContent,非常感谢。我忘了导入我的新视图html文件。