polymer 自定义元素入门 【4】

下面手动来创建一个干净的项目

1、cd nodejs

2、mkdir polymer-demo1

3、cd polymer-demo1

4、bower init    (一路回车)

5、echo>.bowerrc

{ "directory": "bower_components"}

打开“.bowerrc”文件增加bower安装组件路径

6、bower install -save polymer

7、根目录创建index.html文件

<!DOCTYPE html>
<html lang="en">
<head>
    <script src="bower_components/webcomponentsjs/webcomponents-lite.js"></script>
    <meta charset="UTF-8">
    <title>Document</title>
    <link rel="import" href="my-elem.html">
</head>
<body>
    <my-elem></my-elem>
</body>
</html>

8、自定义一个polymer元素my-elem.html

<link rel="import" href="bower_components/polymer/polymer.html">

<dom-module id="my-elem">
  <template>
    <h3>my elem test</h3>
  </template>

  <script>
    Polymer({
      is: "my-elem",
    });
  </script>
</dom-module>

9、安装 polyserve

npm install -g polyserve

安装完,启动服务

polyserve

 10、访问

http://localhost:8080/

第一个简单的polymer自定义元素完成!

11、为元素增加、样式、属性、监听事件

<link rel="import" href="bower_components/polymer/polymer.html">
<dom-module id="my-elem">
    <template>
        <style>
          :host {display: block;}
          h3 {color: red;}
          h3>span {color: blue;}
        </style>
        <h3>my <span>elem</span> test</h3>
        <p>{{text}}</p>
    </template>
    <script>
    Polymer({
        is: "my-elem",
        properties: {
            text: {
                type: String,
                value: 'default value'
            }
        },
        listeners: {
          'tap': 'click'
        },
        click: function() {
          this.text = "click!";
        },
    });
    </script>
</dom-module>
<!DOCTYPE html>
<html lang="en">
<head>
    <script src="bower_components/webcomponentsjs/webcomponents-lite.js"></script>
    <meta charset="UTF-8">
    <title>Document</title>
    <link rel="import" href="my-elem.html">
</head>
<body>
    <h3>hello</h3>
    <my-elem text='12345689'></my-elem>
    <my-elem></my-elem>
</body>
</html>

样式定义放在 template 标签里面

:host 就是当前模块 #my-elem

转载于:https://my.oschina.net/u/157383/blog/674748

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值