Create and Use a Custom HTML Component?

前端 未结 1 1778
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-24 02:23

I have the following local html:




  




        
1条回答
  •  臣服心动
    2021-01-24 02:53

    Try this:

      var tmpl = (document.currentScript||document._currentScript).ownerDocument.querySelector('template');
    

    The problem you ran into is that the template isn't really part of document but it is part of the currentScript. Due to polyfills and browser differences you need to check for currentScript and _currentScript to work correctly.

    Also be aware that HTML Imports will never be fully cross browser. Most web components are moving to JavaScript based code and will be loaded using ES6 module loading.

    There are things that help create templates in JS files. Using the backtick (`) is a reasonable way:

    var tmpl = document.createElement('template');
    tmpl.innerHTML = `
    
    The sky is blue
    `;

    0 讨论(0)
提交回复
热议问题