前端组件化Polymer.js入门教程(2)——Hello world

本节为体验篇,就是让你了解它有哪些功能,不做详细说明,后面再来讲细节。

自定义元素

组件页

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

<dom-module id="my-tab">
    <!-- 可以在template里面写样式、html、js -->
    <template>
        <style>
            div{
                color:red;
                border:1px solid #dedede;
            }
        </style>
        <div>
            Hello World!
        </div>
        <script>
            console.log('Hello world');
        </script>
    </template>
<dom-module/>

<script>
    Polymer({
        // 组件名字,必须加-否则不起作用。
        is:'my-tab'
    });
</script>

index.html

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

效果
857662-20160929124825031-680301927.png

配置元素属性和内容

tab.html

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

<dom-module id="my-tab">
    <!-- 可以在template里面写样式、html、js -->
    <template>
        <style>
            div{
                color:red;
                border:1px solid #dedede;
            }
        </style>
        <div>
            {{msg}}
        </div>
    </template>
<dom-module/>

<script>
    Polymer({
        is:'my-tab',
        properties:{
            msg:{
                type:String
            }
        }
    });
</script>

index.html

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

857662-20160929140501531-1101545859.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值