简体   繁体   English

querySelector在Polymer 2的Shadow DOM中不起作用

[英]querySelector not working in Shadow DOM in Polymer 2

In Polymer 2, Im trying to change a property called urls that is in the shadow DOM. 在Polymer 2中,我试图更改影子DOM中称为urls的属性。 I try to acceess it in var loader . 我尝试在var loader接受它。 I have tried: 我努力了:

var loader = document.querySelector( '#urls' );
var loader = this.shadowRoot.querySelector('#urls');
var loader = Polymer.dom(this).querySelector('#urls');
var loader = this.$.urls;
var loader = this.$$.urls;
var loader = Polymer.dom(this.root).querySelector('#urls')

But none works. 但是没有办法。 What Im doing wrong? 我在做什么错?

<link rel="import" href="../bower_components/polymer/polymer-element.html">
<link rel="import" href="shared-styles.html">
<link rel="import" href="ami-loader2.html">

<dom-module id="my-view2">
  <template is="dom-bind" id="urls">
    <style include="shared-styles">
      :host {
        display: block;
        padding: 10px;
      }
    </style>

    <ami-loader2 urls="[[urls]]"></ami-loader2>
  </template>

  <script>
    class MyView2 extends Polymer.Element {
      static get is() { return 'my-view2'; }
    }

    window.customElements.define(MyView2.is, MyView2);

    var t2 = ['36444280', '36444294', '36444308', '36444322', '36444336'];
    var urlsstring = t2.map( function( v ) {

      return 'https://cdn.rawgit.com/FNNDSC/data/master/dicom/adi_brain/' + v;

    } );

    //var loader = document.querySelector( '#urls' );
    //var loader = this.shadowRoot.querySelector('#urls')
    //var loader = Polymer.dom(this).querySelector('#urls')
    var loader = this.$.urls

    loader.urls = urlsstring;

  </script>
</dom-module>

I can see <ami-loader2> in the document here: 我可以在这里的文档中看到<ami-loader2> 文档中的ami-loader2

If you wish to access an element via an ID you actually have to provide an id :p 如果您希望通过ID访问元素,则实际上必须提供一个id:p

<ami-loader2 id="loader" urls="[[urls]]"></ami-loader2>

so then you can access the element with this.$.loader or if you wish you can do this.shadowRoot.querySelector('#loader') 因此,您可以使用this.$.loader访问该元素,或者如果您愿意,可以执行此操作this.shadowRoot.querySelector('#loader')

This will give you back the ELEMENT. 这将使您还清ELEMENT。 So you can then use functions/apis this element provides. 因此,您可以使用此元素提供的功能/ API。

But as your are using Data-Binding to actually give some data to this elements it's much easier to actually select this value as the "raw" data. 但是,由于您正在使用数据绑定为该元素实际提供一些数据,因此实际上选择此值作为“原始”数据要容易得多。 Which would be just this.url . 就是this.url

And you can actually just do this.urls = 'http://my-url.com' and it will be set for this element. 实际上,您可以只执行this.urls = 'http://my-url.com'并将其设置为此元素。

Be sure to properly define data you wish to bind as properties as well and that all your code is within your class. 确保还将要绑定的数据正确定义为属性,并且所有代码都在类内。

<script>
class MyView2 extends Polymer.Element {

    static get is() { return 'my-view2'; }

    static get properties() {
        return {
            urls: {
                type: String,
                reflectToAttribute: true
            }
        };
    }

    connectedCallback() {
        super.connectedCallback();
        // your code 
    }

}

customElements.define(MyView2.is, MyView2);
</script>

Just as a reference. 仅供参考。 You would be using the element like this <my-view2 urls="http://my-path.com">...</my-view2> 您将使用像这样的元素<my-view2 urls="http://my-path.com">...</my-view2>

Following is very specific to your code and my interpretation on what I think you want to do. 以下内容非常具体地针对您的代码以及我对我想做的事情的解释。

if you only wish to set the variable dynamically inside your element and you don't want to actually change it within your ami-loader2. 如果您只希望在元素内部动态设置变量,而又不想在ami-loader2中实际更改它。 You could remove the reflectToAttribute: true but keep the rest and just use this.urls = 'http://my-path.com' . 您可以删除reflectToAttribute: true但保留其余部分,只需使用this.urls = 'http://my-path.com'

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

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