day 67 Summary

Package

The concept: html css and js aggregates for the collection named with the name change and reuse html css js composed of aggregates => reusability

Component Category:
root component: new Vue () generated by the component
sub-assembly: component name = {}, {} syntax for internal use is vue
global components: Vue.component ( 'component name ", {}), {} internal use the syntax is vue

It features components:
1) components are managed component results html page template instance member, and the template has only one root tag
2) as the topmost components are root parent element, as a local and global component sub-assembly, may be to become a parent component other local and global components
3) data sub-assemblies require isolation (data components of each component has its own separate namespace data)
4) before use of local components must be registered, global components does not require registration, advocate using the local component
of all variables (templates, logic) 5) assembly arise, the assembly is provided by its own management
6) local and global components are a more vue example, one instance of the corresponding set of html, css, js structure, so example is the component

<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
    <div id="app">
        {{ msg }}
    </div>
</body>
<script src="js/vue.js"></script>
<script>
    new Vue({
        el: '#app',  // 被组件 template 模块进行替换的占位符
        data: {
            msg: '组件信息'
        },
        template: '<p>{{ msg }}</p>'
    })
    // 总结:根组件,可以不明确template,template默认采用挂载点页面结构;如果设置的template,挂载点内部的内容无效,因为会被替换
    // 解释:html,body标签不能被替换,所以不能作为挂载点
    </script>
</html>

Subassembly

<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <title>子组件</title>
    <style>
        body, h2 {
            margin: 0;
        }

        .wrap {
            width: 880px;
            margin: 0 auto;
        }
        .wrap:after {
            content: '';
            display: block;
            clear: both;
        }
        .box {
            width: 200px;
            border-radius: 10px;
            overflow: hidden;
            background-color: #eee;
            float: left;
            margin: 10px;
        }

        .box img {
            width: 100%;
        }

        .box h2 {
            text-align: center;
            font-weight: normal;
            font-size: 20px;
        }
    </style>
</head>
<body>
<div id="app">
    <div class="wrap">
        <local-tag></local-tag>
        <local-tag></local-tag>
        <local-tag></local-tag>
        <local-tag></local-tag>

        <global-tag></global-tag>
        <global-tag></global-tag>
        <global-tag></global-tag>
        <global-tag></global-tag>
    </div>
</div>
</body>
<script src="js/vue.js"></script>
<script>

    // 声明局部组件:局部组件要在其父组件中注册才能使用
    // 1、声明组件  2、注册组件  3、渲染组件  => 全局组件不需要注册
    let localTag = {
        template: `
        <div class="box" @click="fn">
            <img src="img/001.jpg" alt="">
            <h2>美女</h2>
        </div>
        `,
        methods: {
            fn() {
                console.log(this)  // this 代表当前组件
            }
        }
    };


    Vue.component('global-tag', {
        template: `
        <div class="box" @click="fn">
            <img src="img/002.jpg" alt="">
            <h2>大长腿</h2>
        </div>
        `,
        methods: {
            fn() {
                console.log(this)
            }
        }
    });


    new Vue({
        el: '#app',
        data: {},
        components: {  // 注册组件
            localTag,
        }
    })
</script>
</html>

The localized data component

<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <title>组件化</title>
    <style>
        body, h2 {
            margin: 0;
        }

        .wrap {
            width: 880px;
            margin: 0 auto;
        }

        .wrap:after {
            content: '';
            display: block;
            clear: both;
        }

        .box {
            width: 200px;
            border-radius: 10px;
            overflow: hidden;
            background-color: #eee;
            float: left;
            margin: 10px;
        }

        .box img {
            width: 100%;
        }

        .box h2 {
            text-align: center;
            font-weight: normal;
            font-size: 20px;
        }
    </style>
</head>
<body>
<div id="app">
    <div class="wrap">
        <local-tag></local-tag>
        <local-tag></local-tag>
        <local-tag></local-tag>
        <local-tag></local-tag>
    </div>
</div>

</body>
<script src="js/vue.js"></script>
<script>
    let localTag = {
        template: `
        <div class="box" @click="fn">
            <img src="img/001.jpg" alt="">
            <h2>捶了美女{{ count }}下</h2>
        </div>
        `,
        data() {  // 局部或全局取件,一个组件可能会被复用多次,每个组件都应该有自己独立的变量名称空间
            return {
                count: 0,
            }
        }, // 数据需要组件化,作为方法的返回值(方法执行后会产生一个局部作用域)
        methods: {
            fn() {
                console.log(this);
                this.count++;
            }
        }
    };

    new Vue({
        el: '#app',
        data: {},
        components: {
            localTag,
        }
    });

    `
    class A:
        name = 'Owen'
        def __init__(self, name):
            self.name = name

    a1 = A('1')
    a2 = A('2')
    a1.name
    a2.name
    `;
</script>
</html>

Parameter passing components - father to son

<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <title>父传子</title>
    <style>
        body, h2 {
            margin: 0;
        }

        .wrap {
            width: 880px;
            margin: 0 auto;
        }

        .wrap:after {
            content: '';
            display: block;
            clear: both;
        }

        .box {
            width: 200px;
            border-radius: 10px;
            overflow: hidden;
            background-color: #eee;
            float: left;
            margin: 10px;
        }

        .box img {
            width: 200px;
            height: 240px;
        }

        .box h2 {
            text-align: center;
            font-weight: normal;
            font-size: 20px;
        }
    </style>
</head>
<body>
<div id="app">
    <div class="wrap">
        <local-tag v-for="dog in dogs" :dog="dog" def="12345" :xyz="dog.name"></local-tag>
    </div>
</div>

</body>
<script src="js/vue.js"></script>
<script>
    let dogs = [
        {
            name: '二哈1号',
            img: 'img/100.jpg',
        },
        {
            name: '二哈2号',
            img: 'img/200.jpg',
        },
        {
            name: '二哈3号',
            img: 'img/300.jpg',
        },
        {
            name: '二哈4号',
            img: 'img/400.jpg',
        },
        {
            name: '二哈1号',
            img: 'img/100.jpg',
        },
        {
            name: '二哈2号',
            img: 'img/200.jpg',
        },
        {
            name: '二哈3号',
            img: 'img/300.jpg',
        },
        {
            name: '二哈4号',
            img: 'img/400.jpg',
        }
    ];


    // 1)子组件可以通过props自定义组件属性(采用反射机制,需要填写字符串,但是使用时可以直接作为变量)
    // 2)子组件会在父组件中渲染,渲染时,将父组件的变量绑定给子组件的自定义属性,将可以将变量值传递给子组件
    let localTag = {
        props: ['dog', 'def', 'xyz'],

        template: `
        <div class="box" @click="fn">
            <img :src="dog.img" alt="">
            <h2>捶{{ dog.name }}{{ count}}下</h2>
            <!--<h3>{{ abc }}</h3>-->
            <!--<h3>{{ def }}</h3>-->
            <!--<h3>{{ xyz }}</h3>-->
        </div>
        `,
        data() {
            return {
                count: 0,
            }
        },
        methods: {
            fn() {
                console.log(this.dog);
                this.count++;
            }
        }
    };

    new Vue({
        el: '#app',
        data: {
            dogs,
        },
        components: {
            localTag,
        }
    });

</script>
</html>

Parameter passing assembly - pass the parent child

<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <title>子传父</title>
</head>
<body>
    <div id="app">
        <h1>{{ h1 }}</h1>
        <h3>{{ h3 }}</h3>


        <!--自定义组件标签的事件
            自定义事件是属于子组件的,子组件在父组件中渲染并绑定事件方法,所以事件方法由父组件来实现
            子组件如何触发自定义事件:this.$emit('自定义事件名', 触发事件回调的参数们)

            子组件触发自定义事件,携带出子组件的内容,在父组件中实现自定义事件的方法,拿到子组件传递给父组件的消息
        -->
        <tag @action="actionFn"></tag>
        <hr>
        <tag2 @h1a="aFn1" @h3a="aFn3"></tag2>
    </div>
</body>
<script src="js/vue.js"></script>
<script>
    let tag = {
        template: `
        <div>
            <input type="text" v-model="t1">
            <input type="text" v-model="t2">
            <button @click="changeTitle">修改标题</button>
        </div>
        `,
        data() {
            return {
                t1: '',
                t2: '',
            }
        },
        methods: {
            changeTitle() {
                if (this.t1 && this.t2) {
                    // console.log(this.t1, this.t2);
                    this.$emit('action', this.t1, this.t2);
                    this.t1 = '';
                    this.t2 = '';
                }
            }
        }
    };


    let tag2 = {
        template: `
        <div>
            主标题内容:<input type="text" v-model="t1" @input="t1Fn">
            子标题内容:<input type="text" v-model="t2">
        </div>
        `,
        data() {
            return {
                t1: '',
                t2: '',
            }
        },
        methods: {
            t1Fn() {
                this.$emit('h1a', this.t1);
            }
        },
        watch: {
            t2 () {
                this.$emit('h3a', this.t2);
            }
        }
    };

    new Vue({
        el: '#app',
        data: {
            h1: '主标题',
            h3: '子标题'
        },
        components: {
            tag,
            tag2,
        },
        methods: {
            actionFn(a, b, c) {
                // console.log('触发了', a, b, c);
                this.h1 = a;
                this.h3 = b;
            },
            aFn1(a) {
                if (!a) {
                    this.h1 = '主标题';
                    return;
                }
                this.h1 = a;
            },
            aFn3(a) {
                if (!a) {
                    this.h3 = '子标题';
                    return;
                }
                this.h3 = a;
            },
        }
    })
</script>
</html>

Guess you like

Origin www.cnblogs.com/LZF-190903/p/12064658.html
67