Vue生命周期、组件、脚手架、全局事件总线、浏览器本地存储

目录

一、生命周期

vue 生命周期 初始化 开始 ,        (挂载流程)

vue 生命周期 初始化 结束

vue 生命周期 运行 开始  (更新流程)

vue 生命周期 运行 结束  (更新流程)

 vue 生命周期 销毁 开始  (销毁流程)

vue 生命周期 销毁 结束  (销毁流程)

二、组件

 (一)、非单文件组件

(二)、组件嵌套

注意:

1、子组件必须在父组件 前面 创建

2、经常在项目中使用 一个总的组件app 来嵌套其他组件

(三)、单文件组件

附:脚手架Vue CLI

一、不同版本Vue 

二、render 函数

三、ref 属性

四、props 配置

附:使用props 让父级从子级获得数据的方法

组件的自定义事件

附:传输和接受多个参数的方法

附:1、*全局事件总线(任意组件间通信)

 附:2、消息的订阅与发布(任意组件间通信)(vue中用的不是很多)

五、mixin 混入

       一、全局混入

       二、局部混入

         六、插件

        七、scoped样式

(四)、组件的实质

(五)、浏览器本地存储


一、生命周期

beforeCreate()

       vue 生命周期 初始化 开始 ,        (挂载流程)

已经发生

1、初始化:生命周期       目的是为了组件的生命周期 和 组件中的事件做准备

2、和事件

还未发生数据代理未开始:(vue 中还没有出现data中的属性和_data属性)
create()
已经发生

1、初始化:生命周期  和 事件

2、数据监测:监测数组、对象 怎么匹配的及对象的一些getter,setter方法

3、数据代理 :vue 可以 代理data中的 属性

还未发生

vue 在 内存中 生成虚拟dom

beforeMount() 此时vue 还没有编译dom 操作,所有页面上的dom还是不奏效
mounted()
已经发生

1、初始化:生命周期  和 事件

2、数据监测:监测数组、对象 怎么匹配的及对象的一些getter,setter方法

3、数据代理 :vue 可以 代理data中的 属性

4、vue 在 内存中 生成虚拟dom

5、vue 编译了 dom 操作,页面上的dom奏效

6、

页面能显示解析好的dom 

vue 生命周期 初始化 结束

beforeUpdate()

                      vue 生命周期 运行 开始  (更新流程)

已经发生

1、初始化:生命周期  和 事件

2、数据监测:监测数组、对象 怎么匹配的及对象的一些getter,setter方法

3、数据代理 :vue 可以 代理data中的 属性

4、vue 在 内存中 生成虚拟dom

5、数据更新了,页面还没来得及更新

还未发生

页面还没来得及更新 

 updated()

已经发生

1、初始化:生命周期  和 事件

2、数据监测:监测数组、对象 怎么匹配的及对象的一些getter,setter方法

3、数据代理 :vue 可以 代理data中的 属性

4、vue 在 内存中 生成虚拟dom

5、数据更新了,页面还没来得及更新

6、数据更新,页面更新

                                            vue 生命周期 运行 结束  (更新流程)

  beforeDestroy()

                      vue 生命周期 销毁 开始  (销毁流程)

已经发生

1、初始化:生命周期  和 事件

2、数据监测:监测数组、对象 怎么匹配的及对象的一些getter,setter方法

3、数据代理 :vue 可以 代理data中的 属性

4、vue 在 内存中 生成虚拟dom

5、数据更新了,页面还没来得及更新

6、数据更新,页面更新

还未发生

vm 中的所有 data ,methods,指令等 还可以使用

 destroy()

已经发生

1、初始化:生命周期  和 事件

2、数据监测:监测数组、对象 怎么匹配的及对象的一些getter,setter方法

3、数据代理 :vue 可以 代理data中的 属性

4、vue 在 内存中 生成虚拟dom

5、数据更新了,页面还没来得及更新

6、数据更新,页面更新

7、vm 中的所有 data 、methods,指令等 不可以使用(以前成功使用的内容,不会销毁)

8、vm 销毁

                    vue 生命周期 销毁 结束  (销毁流程)

<!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">
    <title>Document</title>
    <script src="./js/vue.js"></script>

</head>

<body>
    <div id="app">
        <h1>{{message}}</h1>
        <button @click=" message++ ">update</button>
        <input type="text" @keydown.ctrl="ctrlchufa" placeholder="按下ctrl键触发">
         <button @click="$destroy()">destroy</button>
    </div>

    <script>
        var app = new Vue({
            el: '#app',
            data: {
                message: 1,
                arr: [1, 2, 3],
                objArr: [{ name: 'jack' },
                { name: 'liMing' }],
            },
            methods: {
                ctrlchufa: function () {
                    alert('ctrl键触发');
                    console.log('ctrl键触发');
                }
            },
            beforeCreate() {
                console.log("beforeCreate", this);
            },
            created() {
                console.log("Create", this);
            },
            beforeMount() {
                console.log("beforeMount", this);
                // debugger;
            },
            mounted() {
                console.log("mounted", this);
                // debugger;
            },
            beforeUpdate() {
                console.log("beforeUpdate", this);
            },
            updated() {
                console.log("updated", this);
            },
            // vm.$destroy() 被调用的时候
            beforeDestroy() {
                console.log("beforeDestroy", this);

            },
            destroyed() {
                console.log("destroyed", this);
                debugger;
                
            },

        })

    </script>
</body>

</html>

二、组件

用来实现局部(特定) 功能代码 和 资源 的集合

 (一)、非单文件组件

一个文件包含多个组件

使用组件的步骤:

1、创建组件  2、 注册组件  3、编写(调用)组件标签

<!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">
    <title>组件</title>
    <script src="./js/vue.js"></script>

</head>
<body>
    <div id="root1">
        <!-- 3、编写组件标签 -->
        <Thehello></Thehello>
        <welcome></welcome>
    </div>
    <hr>
    <div id="root2">
        <welcome></welcome>
    </div>
    <script>

        // 1、创建hello组件
        const hello = Vue.extend({
            template: `
            <div>
                <div>{{helloObject}}</div>
                <div>{{helloFunction}}</div>
            </div>
           `,
            //    组件中的data 不能以对象的形式写,且需要返回
            data() {
                return {
                    helloObject: 'LiMing',
                    helloFunction: 'Waved'
                }
            },
        })

        // 1、创建welcome组件
        const welcome = Vue.extend({
            template: `
            <div>
                <h2>{{welcomeObject}}</h2>
                <h2>{{welcomeFunction}}</h2>
            </div>
           `,
            //    组件中的data 不能以对象的形式写,且需要返回
            data() {
                return {
                    welcomeObject: 'people',
                    welcomeFunction: 'hug'
                }
            },
        })

        // 注册公共组件welcome
        Vue.component('welcome', welcome)

        new Vue({
            el: '#root1',
            components: {
                Thehello: hello,   // hello
            },
        })

        new Vue({
            el: '#root2',

        })
    </script>

</body>

</html>

(二)、组件嵌套

注意:

1、子组件必须在父组件 前面 创建

2、经常在项目中使用 一个总的组件app 来嵌套其他组件

<!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">
    <title>非单个组件 组件嵌套</title>

    <script src="./js/vue.js"></script>

</head>

<body>
    <div id="root">
        <!-- 组件嵌套 -->
        <!-- 3、编写组件app标签 -->
        <app></app>

    </div>


    <script>
        // 1、创建welcome的子组件 thanks
        // 子组件必须在父组件 前面 创建
        const thanks = Vue.extend({
            name: 'thanks',
            template: `
            <div>
                <h4>{{thanksObject}}</h4>
                <h4>{{thanksFunction}}</h4>
            </div>
           `,
            //组件中的data 不能以对象的形式写,且需要返回
            data() {
                return {
                    thanksObject: 'thanks a person',
                    thanksFunction: 'thanks  a hug'
                }
            },
        })


        // 1、创建welcome组件
        const welcome = Vue.extend({
            // 3、编写组件thanks标签
            template: `
            <div>
                
                <h3>{{welcomeObject}}</h3>
                <h3>{{welcomeFunction}}</h3>
                <thanks></thanks>

            </div>
           `,
            data() {
                return {
                    welcomeObject: 'welcome-person',
                    welcomeFunction: 'welcome-hug',
                }
            },
            // 2、注册thanks 组件
            components: {
                thanks: thanks
            }
        })

        // 1、创建hello组件
        const hello = Vue.extend({
            template: `
            <div>
                <div>{{helloObject}}</div>
                <div>{{helloFunction}}</div>
            </div>
           `,
            data() {
                return {
                    helloObject: 'hello-LiMing',
                    helloFunction: 'hello-Waved'
                }
            },
        })

        // 1、创建app组件
        const app = Vue.extend({
            name: 'app',  //编译器中看到组件的名称
            // 3、编写组件 theHello和welcome 标签
            template: `
            <div>
               <Thehello></Thehello>
               <welcome></welcome>
            </div>

            `,
            // 父组件app 嵌套 子组件Thehello和welcome
            components: {
                // 2、注册组件Thehello和welcome
                Thehello: hello,
                welcome
            }
        })

        new Vue({
            el: '#root',

            // template:'<app></app>',  //编写组件app标签

            // 2、注册局部组件app
            components: { app },

        })
    </script>
</body>

</html>

(三)、单文件组件

1、一个文件就是一个组件 xxx.vue,vue文件需要转化为js文件浏览器才可以识别。

2、单组件 有三部分标签  

<template>(组件的结构),<script>(组件的交互),<style>(组件的样式)

3、vue 文件 需要暴露给其他文件进而引入,暴露的方式有三种。

在<script> 标签中: 

1、分别暴露

 export const hello = Vue.extend({

            data() {

                return {

                    helloObject: 'hello-LiMing',

                    helloFunction: 'hello-Waved'

                }

            },

        })

2、统一暴露

const hello = Vue.extend({

            data() {

                return {

                    helloObject: 'hello-LiMing',

                    helloFunction: 'hello-Waved'

                }

            },

        })

        export {hello}

3、默认暴露

const hello = Vue.extend({

            data() {

                return {

                    helloObject: 'hello-LiMing',

                    helloFunction: 'hello-Waved'

                }

            },

        })

        export default hello

常用默认暴露

export default {

  name: "welcome",

  data() {

    return {

      welcomeObject: "welcome-person",

      welcomeFunction: "welcome-hug",

    };

  },

};

附:脚手架Vue CLI

Vue CLIi 包含几大组件 

CLI (@vue/cli)                    CLI服务(@vue/cli-service)                        CLI插件

cli  (command line interface)是一个基于 Vue.js 进行快速开发的完整系统。

Vue CLI 4.x 需要 Node.js v10 以上的版本。

一、不同版本Vue 

二、render 函数

 // 将App组件放入容器中,这里将createElement简化成了h

  render: h => h(App),

==》

  // render(createElement){

  //   // 引入的是组件

  //   return createElement(App)

  //   // 引入的是html标签

  //   // return createElement('h1','hello Vue CLI')

  // }

三、ref 属性

1、加在 html 元素上

2、加在组件上

(1)、id 属性    获得的是整个dom 标签

var hello = document.querySelector('#hello');
      console.log(hello);

(2)、ref 属性  获得 hello 组件的实例对象 vc

<template>
  <div>
    <div ref="get_msg">{{ msg }}</div>
    <button @click="change_msg">点击</button>
    <hello ref="hello" />
  </div>
</template>

<script>
// 引入组件
import hello from './components/hello.vue';

export default {
  name: "App",
  components:{
    hello
  },
  data() {
    return {
      msg:'ref shuxing'
    }
  },
  methods: {
    change_msg(){
      var mmsg = this.$refs.get_msg;
      console.log(mmsg);
      mmsg.style.color = 'red';

      // hello 组件的实例对象 vc
      var hello = this.$refs.hello;
      // var hello = document.querySelector('#hello');
      console.log(hello);
    }
  }
  
  
};
</script>

四、props 配置

已经使用过的配置项有:el,data,methods,computed,watch 等

props 的优先级 要高于 data 中的 数值

          data 中的值可以修改 

                 props 里面的值不可以修改

附:使用props 让父级从子级获得数据的方法

(回调函数一定在父组件里面)

1、从父组件 中传出一个带参的方法;

子组件通过props配置项获得这个vc方法,并向该方法传递参数

2、给父组件绑定一个自定义事件

组件的自定义事件

自定义事件用于组件身上,用ref 属性打标签;用$emit() 来接受。

普通事件同于组件身上时,组件不认识,任然需要$emit()来接受,可以在使用时后面添加native修饰符,这样就可以直接使用.。

解绑一个自定义事件

(1)、直接在组件上标签上绑定 

自定义事件名:helloEvent

<hello v-on:helloEvent = "getHelloObject"/> 

(2)、通过使用ref 属性,在钩子函数mounted() 中通过调用组件标签,并给其子定义事件处理。

<hello ref="hello"></hello>

 mounted(){
    // $on表示在helloEvent事件发生的时候触发,与前面的v-on 一个作用
    // this.$refs.hello 指的就是hello组件
    console.log(this)
    this.$refs.hello.$on('helloEvent', this.getHelloObject);
  }

发送给其他:给其一个方法来触发 发送信息

附:传输和接受多个参数的方法

1、将所有的数据包装为一个对象。

传:   this.getWelcomeObject({

        name:this.welcomeObject,

        Number:666,

        Float32Array:[0.1,0.3,8.0],

      });

接:  getWelcomeObject(name){

      console.log('收到 welcome 中的WelcomeObject',name);

    },

  2、不包装对象,直接单独传数据

传:      this.getWelcomeObject(this.welcomeObject,123,7.9,"hqiuwdv");

接:      getWelcomeObject(name,...a){

      console.log('收到 welcome 中的WelcomeObject',name,a);

    },

name 为第一个数,其余的都封装在了数组a里

附:1、*全局事件总线(任意组件间通信)

1、安装全局事件总线
2、给想要接受数据的组件在钩子函数里面给总线绑定自定义事件

此处将接收到的数据,放在了组件的方法里。

 

3、发送数据的一方,通过总线调用$emit()方法触发自定义事件,进而传递事件此处 通过点击button按钮来触发一个函数进而$emit 来发送数据
4、销毁前解绑当前组件的所有事件

在接受数据的一方通过 beforeDestroy 来解绑自定义事件

 附:2、消息的订阅与发布(任意组件间通信)(vue中用的不是很多)

 

五、mixin 混入

两个组件共享一个配置项,可以写在一个混合文件中。

一、全局混入

          这四个都要挂载

二、局部混入

 

 六、插件

 

七、scoped样式

<style> 元素中加上 scoped 表示该样式的作用于仅限于 此文件内。

<!-- 组件的样式 -->
<style scoped>
.first {
  background-color: aquamarine;
}
</style>

                                                                                       

(四)、组件的实质

组件本质是一个叫VueComponent 的构造函数 

Vue(vm) 管理着 VueComponent(vc)

(五)、浏览器本地存储

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值