Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

web component #10

Open
FoxDaxian opened this issue Dec 28, 2018 · 0 comments
Open

web component #10

FoxDaxian opened this issue Dec 28, 2018 · 0 comments

Comments

@FoxDaxian
Copy link
Owner

FoxDaxian commented Dec 28, 2018

<!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">
    <title>Document</title>
</head>

<body>

    <my-btn click-fnname="whenClick">
        <div slot="text" class="text">123</div>
    </my-btn>
    <template class="template">
        <style>
            /* 指代当前宿主 */
            :host {
                display: block;
                width: 100px;
                height: 40px;
                border: 1px solid rgba(0, 0, 0, .4);
                border-radius: 10px;
                text-align: center;
                line-height: 40px;
                cursor: pointer;
            }

            :host(:hover) {
                border-color: blue;
            }

            .slotBox {
                color: red;
            }
        </style>
        <div class="slotBox">
            <slot name="text" class=".slotText">默认的slot展示</slot>
        </div>
    </template>
    <script>
        const config = {
            // ...
            funcs: {
                whenClick() {
                    console.log('我被点击了');
                }
            }
            // ...
        };
        const customTemplate = (function (config) {
            return class extends HTMLElement {
                // 监听可以更新的属性
                static get observedAttributes() {
                    return ['click-fnname'];
                }
                constructor() {
                    super();
                    this._clickFnname = () => {
                        console.log('默认点击事件');
                    }
                    // this 就是当前这个自定义的标签
                    this.template = document.querySelector('.template');
                    this.shadow = this.attachShadow({
                        mode: 'open'
                    })
                    // 添加template到shadow dom
                    this.shadow.appendChild(document.importNode(this.template.content, true))
                }
                // 当被添加的时候会被触发
                connectedCallback() {
                    console.log('调用了');
                    this.addEventListener('click', this._clickFnname)
                }
                // 接绑的时候调用
                disconnectedCallback() {
                    console.log('disconnectedCallback');

                }
                // 自定义标签属性改变时触发
                attributeChangedCallback(name, oldValue, newValue) {
                    switch (name) {
                        case 'click-fnname':
                            // 获取全局下的事件
                            this._clickFnname = config.funcs[newValue];
                            break;
                        default:
                            break;
                    }
                }
            }
        })(config);
        customElements.define('my-btn', customTemplate);

    </script>
</body>

</html>

参考链接: 英文 | 中文 | 一些例子

@FoxDaxian FoxDaxian changed the title hapi node框架 web component Jan 13, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant