当前位置: 首页 > news >正文

前端组件化Polymer入门教程(3)——快速入门

本系列主要翻译官方的教程,因为国内目前这方面的资料太少了,但也不一定和官网的一样,反正就是自己想到哪就写到哪。

如果我没有说明,默认情况下index.html始终包含这段代码,后面将不会再贴上来。

<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <!-- 这是一个基础版的兼容库 -->
    <script src="webcomponents-lite.min.js"></script>
    <!-- 将rel修改为import可以引入另外一个HTML,它将会被执行 -->
    <link rel="import" href="./template/template.html">
</head>

template.html始终包含

<link rel="import" href="../polymer-1.7.0/polymer.html">
我们可以创建一个自定义元素,并向这个自定义元素添加属性和方法,不过值得注意的是:自定义元素的名称必须包含一个“-”。

template.html

<script>
  Polymer({
    is: "proto-element",
    ready: function() {
      this.textContent = "I'm a proto-element. Check out my prototype!"
    }
  });
</script>
is:创建一个自定义元素,必须包含“-”,也就是你要的标签名。
ready:当在页面中创建了proto-element这个元素后将会调到这个方法,this表示当前这个元素,this.textContent设置内容。

index.html

<proto-element></proto-element>

就是用is定义的那个名字

效果
857662-20160929143655547-1813845096.png

添加本地DOM

template.html

<dom-module id="dom-element">

  <template>
    <p>I'm a DOM element. This is my local DOM!</p>
  </template>

  <script>
    Polymer({
      is: "dom-element"
    });
  </script>

</dom-module>
用dom-module来添加原生dom,如果用这种方式需要给它加一个id使用的时候就用这个id当做标签名。
template用来包含css、html

index.html

<dom-element></dom-element>

效果:
857662-20160929145251688-522405057.png

与原生DOM结合

template.html

<dom-module id="picture-frame">

  <template>
    <style>
      div {
        display: inline-block;
        background-color: #ccc;
        border-radius: 8px;
        padding: 4px;
      }
    </style>
    <div>
      <content></content>
    </div>
  </template>

  <script>
    Polymer({
      is: "picture-frame",
    });
  </script>

</dom-module>

如果希望在自定义组件中插入内容就需要用到content标签,它会将我们写的标签插入到content中。

index.html

<picture-frame>
  <p>听说你要学前端。</p>
</picture-frame>

效果
857662-20160929145927938-407371749.png

提示:将style放在template中,它之会影响template里面的元素。

数据绑定

template.html

<dom-module id="name-tag">
  <template>
    <!-- 将owner绑定到property上 -->
    This is <b>{{owner}}</b>'s name-tag element.
  </template>

  <script>
  Polymer({
    is: "name-tag",
    // 当元素准备好的时候将owner属性的值设置为"Daniel"
    ready: function() {
      this.owner = "Daniel";
    }
  });
  </script>
</dom-module>

index.html

<name-tag></name-tag>

857662-20160929150907203-1824500777.png

声明一个属性

template.html

<dom-module id="configurable-name-tag">

  <template>
    This is <b>{{owner}}</b>'s configurable-name-tag element.
  </template>

  <script>
    Polymer({
      is: "configurable-name-tag",
      properties: {
        owner: {
          type: String,
          value: "Daniel"
        }
      }
    });
  </script>

</dom-module>
通过properties声明一个自定义属性,type属性类型,value属性默认内容(如果自定义元素没有写这个属性的话)。

index.html

<configurable-name-tag owner="Scott"></configurable-name-tag>

效果
857662-20160929151717297-1439504655.png

双向数据绑定

template.html

<link rel="import" href="https://polygit2.appspot.com/components/iron-input/iron-input.html">

<dom-module id="editable-name-tag">

  <template>
    <p>
    This is a <strong>{{owner}}</strong>'s editable-name-tag.
    </p>
    <!-- iron-input exposes a two-way bindable input value -->
    <input is="iron-input" bind-value="{{owner}}" placeholder="Your name here...">
  </template>

  <script>
    Polymer({
      is: "editable-name-tag",
      properties: {
        owner: {
          type: String,
          value: "Daniel"
        }
      }
    });
  </script>

</dom-module>

注意这里不要把polymer.html引进来,iron-input.html是一个对input的扩展库。

index.html

<editable-name-tag></editable-name-tag>

效果
857662-20160929153326406-258308542.png

本节完,后面会更加详细的介绍,但是本人也是刚刚接触这个,难免有解释不太清楚的,还请见谅。

相关文章:

  • 仿天猫超市收藏抛物线动画工具库
  • jq的所有事件
  • iOS移动开发周报-第22期
  • Makefile-入门与进阶【转】
  • PHP 合并数组 追加数组例子
  • django文件上传下载
  • 【228】◀▶ Excel 函数说明?
  • lvm 动态扩展
  • 开启总结之路
  • ios 缺少合规证明
  • V-rep学习笔记:机器人逆运动学数值解法(Cyclic Coordinate Descent Method)
  • SQLMAP注入json格式数据
  • 自己写deque
  • 突然有一个时刻想过静静,我知道你是谁。
  • Qinq技术介绍与实战
  • (十五)java多线程之并发集合ArrayBlockingQueue
  • 2018天猫双11|这就是阿里云!不止有新技术,更有温暖的社会力量
  • 4月23日世界读书日 网络营销论坛推荐《正在爆发的营销革命》
  • create-react-app项目添加less配置
  • CSS选择器——伪元素选择器之处理父元素高度及外边距溢出
  • ES6语法详解(一)
  • Facebook AccountKit 接入的坑点
  • github指令
  • JAVA SE 6 GC调优笔记
  • Javascript设计模式学习之Observer(观察者)模式
  • JS学习笔记——闭包
  • Phpstorm怎样批量删除空行?
  • Promise面试题,控制异步流程
  • Redash本地开发环境搭建
  • text-decoration与color属性
  • uva 10370 Above Average
  • Vue UI框架库开发介绍
  • 不用申请服务号就可以开发微信支付/支付宝/QQ钱包支付!附:直接可用的代码+demo...
  • 服务器从安装到部署全过程(二)
  • 回顾 Swift 多平台移植进度 #2
  • 可能是历史上最全的CC0版权可以免费商用的图片网站
  • 快速体验 Sentinel 集群限流功能,只需简单几步
  • 前端学习笔记之观察者模式
  • 使用权重正则化较少模型过拟合
  • 微信端页面使用-webkit-box和绝对定位时,元素上移的问题
  • 小程序上传图片到七牛云(支持多张上传,预览,删除)
  • 移动端 h5开发相关内容总结(三)
  • 《码出高效》学习笔记与书中错误记录
  • Mac 上flink的安装与启动
  • MyCAT水平分库
  • Nginx实现动静分离
  • #Js篇:单线程模式同步任务异步任务任务队列事件循环setTimeout() setInterval()
  • #我与Java虚拟机的故事#连载01:人在JVM,身不由己
  • (c语言)strcpy函数用法
  • (免费领源码)Python#MySQL图书馆管理系统071718-计算机毕业设计项目选题推荐
  • (欧拉)openEuler系统添加网卡文件配置流程、(欧拉)openEuler系统手动配置ipv6地址流程、(欧拉)openEuler系统网络管理说明
  • (三) diretfbrc详解
  • (原)Matlab的svmtrain和svmclassify
  • **PHP分步表单提交思路(分页表单提交)
  • .NET成年了,然后呢?