Vue生命周期中的组件化你知道吗

 更新时间:2022年03月08日 15:53:42   作者:BHW1293773843  
这篇文章主要为大家详细介绍了Vue生命周期中的组件化,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,希望能够给你带来帮助

引出生命周期

此时调用change,定时器回调修改opacity,数据修改,模板重新解析,再次调用change。

销毁流程

解绑(自定义)事件监听器

生命周期

生命周期总结

 <div id="root">
        <!-- <h2 :style="{opacity}">hello,{{name}}</h2> -->
        <h2 :style="{opacity:opacity}">hello,{{name}}</h2>
        <button @click="stop">click stop</button>
        <button @click="opacity = 1">opacity 1</button>
    </div>
    <script type="text/javascript">
        Vue.config.productionTip = false;
        new Vue({
            el: "#root",
            data: {
                name: "atguigu",
                opacity: 1,
            },
            methods: {
                stop(){
                    this.$destroy();
                }
            },
            beforeDestroy() {
                clearInterval(this.timer);
            },
            //vue完成模板解析,并把初始的真实的dom元素放入页面后(挂载完毕),会调用该函数。
            mounted() {
                this.timer = setInterval(() => {
                    this.opacity -= 0.01;
                    if (this.opacity <= 0) { this.opacity = 1 }
                }, 16);
            },
        });
    </script>

组件化 

template:

整个root容器当作模板

会直接替换掉root,把template当作模板进行解析。 

 

 非单文件组件

data需要用函数式写法

<div id="root">
        <h2>{{msg}}</h2>
       <!--组件标签-->
       <school>
       </school>
       <hr>
       <student>       
       </student>
       <student>   
       </student>
       <hello>
       </hello>
    </div>
    <div id="root2">
    </div>
    <script type="text/javascript">
        Vue.config.productionTip = false;
        //创建school组件
       const school = Vue.extend({
            template:`
            <div>
                <h2>schoolname:{{schoolname}}</h2>
                 <h2>schoolage{{schoolage}}</h2>
                 <button @click='show'>点击提示</button>
            </div>
            `,
            data(){
                return{
                    schoolname: "atguigu",
                    schoolage:20,
                }
            },
            methods: {
                show(){
                    alert(this.schoolname);
                }
            },
       });
       //创建stu组件
       const student = Vue.extend({
        template:`
            <div>
                <h2>stuname:{{stuname}}</h2>
                <h2>stuage{{stuage}}</h2>
            </div>
            `,
            data(){
                return{
                    stuname:'tom',
                    stuage:18,
                }
            },
       });
       //创建hello组件
       const hello = Vue.extend({
            template:`
            <div>
                <h2>stuname:{{stuname}}</h2>
                <h2>stuage{{stuage}}</h2>
            </div>
            `,
            data(){
                return{
                    stuname:'tom',
                    stuage:18,
                }
            },
       });
       //全局注册组件
       Vue.component('hello',hello);
        new Vue({
            el: "#root",
            data:{
                msg:'this is msg'
            },
            //局部注册组件
            components:{
                school:school,
                student,
            }
        });
    </script>

 组件的几个注意点 

 组件的嵌套 

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script type="text/javascript" src="../js/vue.js"></script>
    <title>Document</title>
</head>
<body>
    <div id="root">
    </div>
    <script type="text/javascript">
        Vue.config.productionTip = false;
        //创建student组件
        const student = Vue.extend({
            template:`
            <div>
                <h2>stuname:{{stuname}}</h2>
                <h2>stuage{{stuage}}</h2>
            </div>
            `,
            data(){
                return{
                    stuname:'tom',
                    stuage:18,
                }
            },
       });
        //创建school组件
       const school = Vue.extend({
            template:`
            <div>
                <h2>schoolname:{{schoolname}}</h2>
                 <h2>schoolage{{schoolage}}</h2>
                 <button @click='show'>点击提示</button>
                 <student></student>
            </div>
            `,
            data(){
                return{
                    schoolname: "atguigu",
                    schoolage:20,
                }
            },
            methods: {
                show(){
                    alert(this.schoolname);
                }
            }, 
            components:{
                student:student,               
            }  
       });
       //创建hello组件
       const hello = Vue.extend({
            template:`
            <div>
                <h2>{{msg}}</h2>
            </div>
            `,
            data(){
                return{
                    msg:'hello!'
                }
            },
       });
       const app = Vue.extend({
           template:`
                <div>
                    <hello></hello>
                    <school></school>
                </div>
           `,
           components:{
                school,
                hello,              
            } 
       })
       //vue
        new Vue({
            template:'<app></app>',
            el: "#root",
            //局部注册组件
            components:{
                app,             
            }         
        });
    </script>
</body>
</html>

 VueComponent

每次调用extend,都返回了一个VueComponent

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script type="text/javascript" src="../js/vue.js"></script>
    <title>Document</title>
</head>
<body>
    <div id="root">
        <!--组件标签-->
        <school>
        </school>
        <hello>
        </hello>
    </div>
    <div id="root2">
    </div>
    <script type="text/javascript">
        Vue.config.productionTip = false;
        //创建school组件
        const school = Vue.extend({
            template: `
            <div>
                <h2>schoolname:{{schoolname}}</h2>
                 <h2>schoolage{{schoolage}}</h2>
                 <button @click='show'>点击提示</button>
            </div>
            `,
            data() {
                return {
                    schoolname: "atguigu",
                    schoolage: 20,
                }
            },
            methods: {
                show() {
                    console.log(this)//VueComponent实例对象  vc
                    alert(this.schoolname);
                }
            },
        });
        //创建hello组件
        const hello = Vue.extend({
            template: `
            <div>
                <h2>hello:{{hello}}</h2>
            </div>
            `,
            data() {
                return {
                    hello: "hello",
                }
            },
        });
        console.log(school);//一个构造函数
        console.log(hello);//一个构造函数
        console.log(school === hello);//false
        new Vue({
            el: "#root",
            data: {
            },
            //局部注册组件
            components: {
                school: school,
                hello:hello,
            }
        });
    </script>
</body>
</html>

 Vue实例与组件实例

总结

本篇文章就到这里了,希望能够给你带来帮助,也希望您能够多多关注脚本之家的更多内容!  

相关文章

  • vue项目前端错误收集之sentry教程详解

    vue项目前端错误收集之sentry教程详解

    Sentry 是一个开源的错误追踪工具,可以帮助开发人员实时监控和修复系统中的错误。这篇文章主要介绍了vue项目前端错误收集之sentry,本文通过实例代码给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
    2019-05-05
  • vue环形进度条组件实例应用

    vue环形进度条组件实例应用

    在本文中我们给大家分享了关于vue环形进度条组件的使用方法以及实例代码,需要的朋友们跟着测试下吧。
    2018-10-10
  • vue仿网易云音乐播放器界面的简单实现过程

    vue仿网易云音乐播放器界面的简单实现过程

    兴趣乃学习的动力,想自己动手写个音乐播放器,查了网上一些博客写了一个,这篇文章主要给大家介绍了关于vue仿网易云音乐播放器界面的简单实现过程,需要的朋友可以参考下
    2021-11-11
  • Django+Vue跨域环境配置详解

    Django+Vue跨域环境配置详解

    这篇文章主要介绍了Django+Vue跨域环境配置详解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-07-07
  • vue实现下拉加载其实没那么复杂

    vue实现下拉加载其实没那么复杂

    这篇文章主要给大家介绍了关于vue实现下拉加载的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用vue具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
    2019-08-08
  • WebStorm启动vue项目报错代码:1080 throw err解决办法

    WebStorm启动vue项目报错代码:1080 throw err解决办法

    在使用webstorm新建vue项目时常会遇到一些报错,下面这篇文章主要给大家介绍了关于WebStorm启动vue项目报错代码:1080 throw err的解决办法,文中将解决办法介绍的非常详细,需要的朋友可以参考下
    2023-12-12
  • vue实现登录类型切换

    vue实现登录类型切换

    这篇文章主要为大家详细介绍了vue实现登录类型切换,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-04-04
  • vue使用video插件vue-video-player详解

    vue使用video插件vue-video-player详解

    这篇文章主要为大家详细介绍了vue使用video插件vue-video-player,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-10-10
  • 基于vue的换肤功能的示例代码

    基于vue的换肤功能的示例代码

    本篇文章主要介绍了基于vue的换肤功能的示例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-10-10
  • Vue 2.0在IE11中打开项目页面空白的问题解决

    Vue 2.0在IE11中打开项目页面空白的问题解决

    这篇文章主要给大家介绍了关于Vue 2.0在ie 11中打开项目页面空白问题的解决方法,文中详细分析出现该问题的原因,并给出了详细的解决方法,需要的朋友可以参考借鉴,下面跟着小编来一起学习学习吧。
    2017-07-07

最新评论