简体   繁体   English

使用Polymer importHref动态加载HTML页面

[英]Dynamically Load HTML page using Polymer importHref

I'm writing a simple element that loads html files using Polymer 1.0's helper function importHref() . 我正在编写一个使用Polymer 1.0的辅助函数importHref()加载html文件的简单元素。 The page loads in, but instead of the html rendering to the page, I'm getting [object HTMLDocument] . 页面加载完毕,但不是[html渲染[object HTMLDocument]页面,而是[object HTMLDocument]

When I log the successful callback, the imported page is wrapped in a #document object (not sure on the terminology here). 当我记录成功的回调时,导入的页面将包装在#document对象中(不确定此处的术语)。 But, the info is all there in the console. 但是,信息就在控制台中。

So, my question is: How do I render the html to the page? 因此,我的问题是:如何将html呈现到页面?

element: 元件:

<dom-module id="content-loader">

<template>
    <span>{{fileContent}}</span>
</template>

<script>

Polymer({

    is: "content-loader",

    properties: {
        filePath: {
            type: String
        }
    },

    ready: function() {
        this.loadFile();
    },

    loadFile: function() {
        var baseUrl;

        if (!window.location.origin)
        {
            baseUrl = window.location.origin = window.location.protocol + "//" + window.location.hostname + (window.location.port ? ':' + window.location.port: '');
        }
        else
        {
            baseUrl = window.location.origin;
        }

        //import html document and assign to fileContent
        if(this.filePath)
        {
            this.importHref(baseUrl + this.filePath, function(file){
                this.fileContent = file.target.import;
                console.log(this.fileContent); //logs fine
            },
            function(error){
                console.log(error);
            });
        }
    }

});

</script>

in use: 正在使用:

<content-loader file-path="/app/general/contact.html"></content-loader>

<span>{{fileContent}}</span> will render fileContent cast to a String, which is why you see [object HTMLDocument] (which is what you get when you call toString() on a document Object). <span>{{fileContent}}</span>会将fileContent呈现为字符串,这就是为什么您看到[object HTMLDocument] (这是在document Object上调用toString()时得到的)的原因。

Just in general, Polymer won't let you bind to HTML or node content because it's a security risk. 通常,Polymer不允许您绑定到HTML或节点内容,因为这存在安全风险。

The fileContent you have is a document , which means it's a collection of DOM nodes. 您拥有的fileContent是一个document ,这意味着它是DOM节点的集合。 How you use that document depends on what the content is that you loaded. 您使用该文档的方式取决于您加载的内容。 One way to render the nodes is to append fileContent.body onto your local DOM, like so: 呈现节点的一种方法是将fileContent.body附加到本地DOM上,如下所示:

Polymer.dom(this.root).appendChild(this.fileContent.body);

Here is a more complete example ( http://jsbin.com/rafaso/edit?html,output ): 这是一个更完整的示例( http://jsbin.com/rafaso/edit?html,output ):

<content-loader file-path="polymer/bower.json"></content-loader>

<dom-module id="content-loader">

  <template>
    <pre id="content"></pre>
  </template>

  <script>

    Polymer({
      is: "content-loader",
      properties: {
        filePath: {
          type: String,
          observer: 'loadFile'
        }
      },

      loadFile: function(path) {
        if (this.filePath) {
          console.log(this.filePath);
          var link = this.importHref(this.filePath, 
            function() {
              Polymer.dom(this.$.content).appendChild(link.import.body);
            },
            function(){
              console.log("error");
            }
          );
        }
      }
    });

  </script>
</dom-module>

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

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