美文网首页
vue3和ts的使用

vue3和ts的使用

作者: Biao_349d | 来源:发表于2021-11-14 14:27 被阅读0次

一. setup
变量不在写在data里面, 反而在setup内写。

<script lang="ts">
import { defineComponent, ref } from "vue";
export default defineComponent({
  name: "App",
  setup() {
    const girls = ref(["大脚", "刘英", "晓红"]);
    const selectGirl = ref("");
    const selectGirlFun = (index: number) => {
      selectGirl.value = girls.value[index];
    };
    //因为在模板中这些变量和方法都需要条用,所以需要return出去。
    return {
      girls,
      selectGirl,
      selectGirlFun,
    };
  },
});
</script>
  • ps:
    1. 使用ref定义字符串,数组等变量值;
    2. 变量赋值需要使用.value;

二. reactive


<script lang="ts">
import { ref, reactive } from "vue";
export default {
 name: "App",
 setup() {
   const data = reactive({
     girls: ["大脚", "刘英", "晓红"],
     selectGirl: "",
     selectGirlFun: (index: number) => {
       data.selectGirl = data.girls[index];
     },
   });

   return {
     data,
   };
 },
};
</script>

  • ps:
    1. reactive 接收对象。
    2. 使用时需要使用data.girls方式使用;

三. 添加类型注释

  interface DataProps {
  girls: string[];
  selectGirl: string;
  selectGirlFun: (index: number) => void;
}

cosnt data: DataProps = reactive({
      girls: ["大脚", "刘英", "晓红"],
      selectGirl: "",
      selectGirlFun: (index: number) => {
        data.selectGirl = data.girls[index];
      },
    });

四. toRefs()

import { reactive, toRefs } from "vue";
export default {
  name: "App",
  setup() {
    // const girls = ref(["大脚", "刘英", "晓红"]);
    // const selectGirl = ref("");
    // const selectGirlFun = (index: number) => {
    //   selectGirl.value = girls.value[index];
    // };
    const data: DataProps = reactive({
      girls: ["大脚", "刘英", "晓红"],
      selectGirl: "",
      selectGirlFun: (index: number) => {
        data.selectGirl = data.girls[index];
      },
    });
    const refData = toRefs(data);

    return {
      ...refData,
    };
  },
};

  • ps:
    1. 解决了在template中必须使用data.girls的方式使用变量, 现在可以直接使用变量名称了;

五. 钩子

import {
  reactive,
  toRefs,
  onMounted,
  onBeforeMount,
  onBeforeUpdate,
  onUpdated,
} from "vue";



<script lang="ts">

//....
const app = {
  name: "App",
  setup() {
    console.log("1-开始创建组件-----setup()");
    const data: DataProps = reactive({
      girls: ["大脚", "刘英", "晓红"],
      selectGirl: "",
      selectGirlFun: (index: number) => {
        data.selectGirl = data.girls[index];
      },
    });
    onBeforeMount(() => {
      console.log("2-组件挂载到页面之前执行-----onBeforeMount()");
    });

    onMounted(() => {
      console.log("3-组件挂载到页面之后执行-----onMounted()");
    });
    onBeforeUpdate(() => {
      console.log("4-组件更新之前-----onBeforeUpdate()");
    });

    onUpdated(() => {
      console.log("5-组件更新之后-----onUpdated()");
    });

    const refData = toRefs(data);

    return {
      ...refData,
    };
  },
};
export default app;
</script>
  • ps
    1. 生命周期需要引用
    2. 什么周期需要卸载setup内
    3. 都是以on开头。
    4. 变化: BeforeDestroy变成了onBeforeUnmount destroyed变成了onUnmounted

生命周期对比


Vue2--------------vue3
beforeCreate  -> setup()
created       -> setup()
beforeMount   -> onBeforeMount
mounted       -> onMounted
beforeUpdate  -> onBeforeUpdate
updated       -> onUpdated
beforeDestroy -> onBeforeUnmount
destroyed     -> onUnmounted
activated     -> onActivated
deactivated   -> onDeactivated
errorCaptured -> onErrorCaptured

六. watch

watch([overText, data.selectGirl], (newValue, oldValue) => {
      console.log(`new--->${newValue}`);
      console.log(`old--->${oldValue}`);
      document.title = newValue[0];  //返回的newValue也是一个数组
  });

  1. teleport

可以定义绑定到dom内;

<template>
  <teleport to="#modal">
    <div id="center">
      <h2>JSPang11</h2>
    </div>
  </teleport>
</template>
<div id="app"></div>
<div id="modal"></div>

  1. 使用vuex

      const store = useStore();
const data = {
            startTime: store.state.startTime,
            endTime: store.state.endTime,
            projectId: store.state.projectId
        }

          store.commit('data', data)

  1. 事件

  setup(props, context) {

            context.emit('onShow', userId) 
  }

相关文章

  • vue3和ts的使用

    一. setup变量不在写在data里面, 反而在setup内写。 ps:使用ref定义字符串,数组等变量值;变...

  • vue3 的一些知识点

    vue3 支持 jsx 安装依赖 vite.config.ts 中引用插件 使用 jsx CSS Module 任...

  • Vite2+vue3+ts 使用 router,layout 搭

    Vite2+vue3+ts 使用 router,layout 搭建页面框架,并做页面自适应 vue3 对应使用的是...

  • 2021-01-21

    今天做了什么有意思的事 1. vue3 + ts项目 2. vue3使用g2 今天学会了什么 今天居然没学到什么?...

  • vue CLI3中使用ts坑之路由篇

    今天想使用ts配置vue3路由,原本以为特别简单,毕竟在不使用ts的时候几乎可以说是闭着眼睛都可以做到的东西(允许...

  • vue3与vue2的区别

    整体区别 使用proxy代替defineProperty实现数据相应试 vue3支持多个根元素 更好的ts支持 体...

  • 利用vscode安装vue3,并使用

    利用vscode安装vue3,并使用 1、打开终端ctrl+~ 2、安装过程 ts安装 vue-cli 3....

  • 9、Vue3 crud案例

    基于vue3、vuex、vue-router、ts 案例 效果 案例 App.vue main.ts src/ro...

  • VSCode 中,TS 提示 ”无法找到 *.vue 声明文件

    在使用 VSCode 开发 Vue3 + TS 项目时,编辑器一直有一个报错,因为不影响编译和运行,所以就一直没在...

  • vue3+ts+electron踩坑记录

    本文包括vue3的基础环境搭建和electron配置,全TS。全部技术栈为:vue3+ts+antdv+vite+...

网友评论

      本文标题:vue3和ts的使用

      本文链接:https://www.haomeiwen.com/subject/vctxtrtx.html