Vue3 新特性

# Vue3的改进及特点

1.性能的提升:打包大小减少 41%,初次渲染快 55%,更新快 133%,内存使用减少 54%。

2.新推出的Composition API 使组件更易维护,减少无用数据绑定页面更流畅。

4.更好TypeScript支持,可以在创建命令里直接配置,页面集成畅通无阻。

5.Teleport(瞬移组件)、Suspense(解决异步加载组件问题)和全局 API 的修改和优化。

6.Vue3兼容大部分Vue2的特性,用Vue2代码开发Vue3都可以。

# 安装

vue --version # 查看版本

注意:如果以前安装过,需要检查一下版本,因为只有最新版本(V4.5.4 以上版本)才有创建 Vue3 的选项。

npm install -g @vue/cli

使用 vue-cli 命令行创建项目

vue create vue3-1 // 根据提示自己选择配置

启动命令

yarn serve 或 npm run serve

打包命令

yarn build 或 npm run build

# 新语法 setup(),ref(),reactive()

// 注:setup是为了优化性能让程序按需引入全局统一

1.用法一

<template>

  <div class="home">

    <div>名字:{{ name }}</div>

    <ul>

      <li v-for="item in list" :key="item" @click="show(item)">{{ item }}</li>

    </ul>

  </div>

</template>

<script lang="ts">

import { defineComponent, ref } from "vue";

// 注:defineComponent 在TypeScript下,给予了组件正确的参数类型推断

export default defineComponent({

  name: "Home",

  components: {},

  props:['msg'],

  setup(props,context) {

    // 注:setup函数是处于生命周期函数 beforeCreate 和 Created 两个钩子函数之间的函数 也就说在 setup 函数中是无法使用 data 和 methods 中的数据和方法,而methods等可以使用setup中return出去的数据。

    /*

    一.函数的第一个参数是 props 用于接收 props.msg

      这个props是一个响应式的Proxy对象,不可以解构,解构后会失去响应,如果要用解构的方式,要用toRefs

      let { msg } = toRefs(props) //但是解析成ref了要用msg.value,所以直接用props.msg更简单

    二.context对象在setup()中暴露三个属性 attrs 、slots 和 emit 因为在setup函数中还没有创建Vue实例,是无法使用vm.$attrs、vm.$slots和vm.$emit的,所以这三个属性充当了这样的作用,使用方法相同。

    注意:

      context.attrs和vm.$attrts包含的是在实例vm.props中没有被声明识别的attribute(class和style除外)。所以setup()中参数props中暴露的变量,就不会在context.attrs中暴露。

      context.slots和vm.$slots只能访问具名插槽,没有命名的插槽或者v-slot:default的是没有暴露的。

      context的attrs和slots是有状态的,当组件更新时也会实时更新,所以也不要解构。但与props不同的是,它们不是响应式的,在setup()中的使用应保持只读的状态,如果要改变可以在onUpdated的周期函数中进行。

      context.emit和vm.$emit可以触发实例上的监听事件。

    */

    const list = ref(["深圳", "北京", "上海"]);

    const name = ref("");

    //注:用ref是为了转换成引用类型,让全局引用保持一致,而之前原始类型是不行的,所以要name.value的方示赋值

    const show = (index: string) => {

        name.value = index;

    };

    // 注:不return出去的数据,模板是无法使用的。

    return {

        list,

        name,

        show

    };

  },

});

</script>

2.用法二 reactive() 优化

<template>

  <div class="home">

    <div>名字:{{ data.name }}</div>

    <ul>

      <li v-for="item in data.list" :key="item" @click="data.show(item)">{{ item }}</li>

    </ul>

  </div>

</template>

<script lang="ts">

import { defineComponent, reactive } from "vue";

export default defineComponent({

  name: "Home",

  components: {},

  setup() {

    const data = reactive({

      list: ["深圳", "北京", "上海"],

      name: "",

      show: (index: string) => {

        data.name = index;

      },

    });

    return {

      data

    };

  },

});

</script>

2.用法三 toRefs() 优化

<template>

  <div class="home">

    <div>名字:{{ name }}</div>

    <ul>

      <li v-for="item in list" :key="item" @click="show(item)">{{ item }}</li>

    </ul>

  </div>

</template>

<script lang="ts">

import { defineComponent,reactive,toRefs } from "vue";

export default defineComponent({

  name: "Home",

  components: {},

  setup() {

    const data = reactive({

      list: ["深圳", "北京", "上海"],

      name: "",

      show: (index: string) => {

        data.name = index;

      },

    });

    const refData = toRefs(data);

    //不能直接解析 ...data 必须用 toRefs()

    return {

      ...refData

    };

  },

});

</script>

# Vue3 生命周期函数用法, 需要引入 (注:vue2 生命周期函数不影响)

<script lang="ts">

import { defineComponent,ref,reactive,toRefs,

onBeforeMount,

onMounted,

onBeforeUpdate,

onUpdated,

onBeforeUnmount,

onUnmounted,

onActivated,

onDeactivated,

onErrorCaptured,

onRenderTracked,

onRenderTriggered

} from "vue";

export default defineComponent({

  name: "Home",

  components: {},

  setup() {

    //setup() 开始创建组件之前,在beforeCreate和created之前执行。

    const data = reactive({

      list: ["深圳", "北京", "上海"]

    });

    onBeforeMount(()=>{

      //组件挂载到节点上之前执行的函数。

    })

    onMounted(()=>{

      //组件挂载完成后执行的函数。

    })

    onBeforeUpdate(()=>{

      //组件更新之前执行的函数

    })

    onUpdated(()=>{

      //组件更新完成之后执行的函数。

    })

    onBeforeUnmount(()=>{

      //组件卸载之前执行的函数。

    })

    onUnmounted(()=>{

      //组件卸载完成后执行的函数。

    })

    onActivated(()=>{

      //被包含在<keep-alive>中的组件,会多出两个生命周期钩子函数。被激活时执行。

    })

    onDeactivated(()=>{

      //比如从 A 组件,切换到 B 组件,A 组件消失时执行。

    })

    onErrorCaptured(()=>{

      //当捕获一个来自子孙组件的异常时激活钩子函数。

    })

    //< 调试用生命函数

    onRenderTracked((event)=>{

      //跟踪所有状态触发

      console.log(event);

    });

    onRenderTriggered((event) => {

      //跟踪当前状态触发

      console.log(event);

      //key 那边变量发生了变化

      //newValue 更新后变量的值

      //oldValue 更新前变量的值

      //target 目前页面中的响应变量和函数

    });

    // 调试用生命函数 />

    const refData = toRefs(data);

    return {

      ...refData

    };

  },

  mounted(){

    console.log("vue2 生命周期");

  }

});

</script>

# Vue3 watch用法

<script lang="ts">

import { defineComponent, ref, reactive, toRefs, watch } from "vue";

export default defineComponent({

  name: "Home",

  components: {},

  setup() {

    const text = ref("测试单个值");

    const data = reactive({

      list: ["深圳", "北京", "上海"],

      name: "",

      show: (index: string) => {

        data.name = index;

      },

    });

    //watch(text, 单个用法,watch([text,()=>data.name], 多个用法,注:()=>data.name 为了兼容vue2

    watch([text,()=>data.name], (newValue, oldValue) => {

      console.log(`new--->${newValue}`);

      console.log(`old--->${oldValue}`);

    });

    const refData = toRefs(data);

    return {

      ...refData,

    };

  },

});

</script>

# Vue3 模块化重用功能 (优化 mixins)

1.新建useTime.ts文件

import { ref } from "vue";

const time = ref("00:00:00");

const getTime = () => {

    const now = new Date();

    const h= now.getHours() < 10 ? "0" + now.getHours() : now.getHours();

    const m = now.getMinutes() < 10 ? "0" + now.getMinutes() : now.getMinutes();

    const s= now.getSeconds() < 10 ? "0" + now.getSeconds() : now.getSeconds();

    time.value = h + ":" + m + ":" + s;

    setTimeout(getTime, 1000);

};

export { time, getTime }

2.引入

<template>

  <div class="home">

    <div>时间:{{time}} <button @click="startTime">开始</button></div>

  </div>

</template>

<script lang="ts">

import { defineComponent, ref } from "vue";

import { time, getTime } from './useTime';

export default defineComponent({

  name: "Home",

  components: {},

  setup() {

    const startTime = () => {

      getTime();

    };

    return {

      startTime,

      time

    };

  },

});

</script>

# teleport 独立挂载组件(解决样式等冲突问题不挂载到app下)

1. index.html 页面新加插入点(会挂载到 #headTitie DOM下)

<div id="headTitie"></div>

<div id="app"></div>

2. 在components目录下新建 headTitle.vue

<template>

  <teleport to="#headTitie">

    <div class="head">

      <h1>{{ title }}</h1>

    </div>

  </teleport>

</template>

<script lang="ts">

import { defineComponent, ref } from "vue";

export default defineComponent({

  name: "headTitie",

  setup() {

    const title = ref("Vue3 新特性示例");

    return {

      title,

    };

  },

});

</script>

3. 在 App.vue 加

<template>

  <headTitle />

  <router-view />

</template>

<script lang="ts">

import headTitle from "./components/headTitle.vue";

export default {

  name: "App",

  components: {

    headTitle,

  },

};

</script>

# Suspense 异步请求组件

1. 新建Demo.vue

<template>

  <div class="Demo">

    <div>名字:{{ name }}</div>

  </div>

</template>

<script lang="ts">

import { defineComponent } from "vue";

export default defineComponent({

  name: "Demo",

  components: {},

  setup() {

    return new Promise((resolve, reject) => {

      setTimeout(() => {

        return resolve({ name: "我是 Suspense 异步请求组件" });

      }, 2100);

    });

  },

});

</script>

2. 使用引入 home.vue

<template>

  <div class="home">

    <Suspense>

      <template #default>

        <Demo />

      </template>

      <template #fallback>

        <p>加载中...</p>

      </template>

    </Suspense>

  </div>

</template>

<script lang="ts">

import { defineComponent } from "vue";

import Demo from "./Demo.vue";

export default defineComponent({

  name: "Home",

  components: {Demo}

});

</script>

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,088评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,715评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,361评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,099评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 60,987评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,063评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,486评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,175评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,440评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,518评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,305评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,190评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,550评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,880评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,152评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,451评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,637评论 2 335

推荐阅读更多精彩内容