Vue学习: 第六章 插槽、自定义指令

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>插槽、自定义指令</title>
    <script src="../node_modules/vue/dist/vue.js"></script>
</head>
<body>
    <div id="app">
        <submit-button></submit-button>
        <base-layout>
            <template v-slot:header>
                Here is Title
            </template>
            Here is content
            <template #footer>
                Here is footer
            </template>
        </base-layout>
        <input v-focus>
        <base-layout2></base-layout2>
        {{user | capitalize}}
        <base-layout3 :user="user"></base-layout3>
    </div>
    <script>
        //插槽:由父组件控制显示内容,子组件控制显示位置
        Vue.component('submit-button', {
           data: function() {
                return {
                  name: 'Lily'  ,
                };
           },
           template:`<button type="submit">你好<slot>{{name}}submit</slot></button>`
        });

        //具名插槽:在父组件中使用templat元素显示具名插槽,使用v-slot指令指定插槽名字
        Vue.component('base-layout', {
            template: `
                <div>
                    <h1><slot name="header"></slot></h1>
                    <span><slot></slot></span>
                    <h1><slot name="footer"></slot></h1>
                </div>
            `
        });

        //自定义指令
        //注册一个全局自定义指令
        Vue.directive('focus', {
            inserted: function (el) {
                el.focus();
            }
        });

        //注册一个局部自定义指令
         Vue.component('base-layout2', {
            directives: {
                focus2: {
                    inserted: function (el) {
                        el.focus();
                    }
                }
            },
             template: `<input v-focus2>`
        });

        //过滤器
        //定义全局过滤器
        Vue.filter('capitalize', function (value) {
            let s = value.toString();
            return s.charAt(0).toUpperCase() + s.slice(1);
        });

        //定义局部过滤器
        Vue.component('base-layout3', {
            props: ['user'],
            filters: {
                capitalize2: function (value) {
                    let s = value.toString();
                    return s.charAt(0).toUpperCase() + s.slice(1);
                }
            },
            template: `<span>{{user | capitalize2}}</span>`
        });

        let vm = new Vue({
           el: '#app',
           data: {
             user: 'i am a user'
           },
        });
    </script>
</body>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值