代码之家  ›  专栏  ›  技术社区  ›  NeitoFR

聚合物单元素

  •  1
  • NeitoFR  · 技术社区  · 8 年前

    我很难理解如何使用聚合物创建Web组件。我的目的只是在单击按钮时显示一个字符串、一个输入文本和一个按钮;字符串将使用输入文本的实际值进行更新。

    这是我的第一次尝试:

    <link rel="import" href="./../bower_components/polymer/polymer.html">
    
    <dom-module id="neito-sidebar">
        <template>
            <style>
    
            </style>
    
            <label for="test">Name : </label>
            <input type="text" name="test" id="test">
            <button on-tap="_updateSpan">Valider</button>
            <span>[[mot]]</span>
    
        </template>
        <script>
            Polymer({
                is: 'neito-sidebar',
                properties: {
                    mot: String,
                    notify: true
                },
    
            _updateSpan: function()
            {
                this.mot = $('#test').val();
            }
            });
    
        </script>
    </dom-module>
    

    我是接近结果了还是一切都错了?

    哦,我忘了,这里是索引。调用我的组件的html:

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">
        <link rel="import" href="./components/neito-sidebar.html">
        <link rel="import" href="./bower_components/polymer/polymer.html">
        <link rel="import" href="./bower_components/jquery/dist/jquery.js">
        <title>Polymer Element</title>
    </head>
    <body>
        <neito-sidebar></neito-sidebar>
    </body>
    </html>
    

    这是 structure of the project :

    2 回复  |  直到 8 年前
        1
  •  0
  •   Cappittall    8 年前

    事实上,如果你喜欢用Polymer的方式,这很容易,甚至你不需要任何js代码,也不需要任何按钮。您输入的内容将显示在范围中。

    <link rel="import" href="./../bower_components/polymer/polymer.html">
    <link rel="import" href="./../bower_components/iron-input/iron-input.html">
    <dom-module id="neito-sidebar">
        <template>
            <style></style>
            <iron-input bind-value="{{mot}}">
               <label for="test">Name : </label>
               <input id="test" type="text"  value="{{mot::input}}">
            </iron-input>
            <span>[[mot]]</span>
        </template>
        <script>
            Polymer({is: 'neito-sidebar' });
        </script>
    </dom-module>
    

    <html>
    <head>
    <base href="https://polygit.org/polymer+:master/components/">
      <link href="polymer/polymer.html" rel="import">
      <link rel="import" href="iron-input/iron-input.html">
      <script src="webcomponentsjs/webcomponents-lite.js"></script>
    </head>
    <body>
    <x-foo></x-foo>
    
    <dom-module id="x-foo">
      <template>
        <style>
          :host {
            display: block;
            height: 100%;
          }
          span { 
             color:red;
          }
        </style>
            <iron-input bind-value="{{mot}}">
               <label for="test">Name : </label>
               <input id="test" type="text"  value="{{mot::input}}">
            </iron-input>
         <br/>
         <span>[[mot]]</span>
        </template>
    <script>
        HTMLImports.whenReady(function() {
        class XFoo extends Polymer.Element {
          static get is() { return 'x-foo'; }
          static get properties() { return { 
            
         }};
        static get observers() { return []}
       
     }
    customElements.define(XFoo.is, XFoo);
     });
        
    </script>
    </dom-module>
    </body>
    </html>
        2
  •  0
  •   Davie    8 年前

    我无权对之前的解决方案发表评论,但您可能只需要确保您的应用程序根目录中安装了HakanC建议的聚合物组件:

    bower install --save PolymerElements/iron-input