Vue3.0代码学习笔记

简介: Vue3.0代码学习笔记

defineEmits和defineProps获取父组件传过来值和事件

// 第一种不带默认值props
const props = defineProps<{
  foo: string
  bar?: number
}>()
// 第二种带默认值props

export interface ChildProps {
  foo: string
  bar?: number
}

const props = withDefaults(defineProps<ChildProps>(), {
   foo: "1qsd"
  bar?: 3
})

// 第一种获取事件
const emit = defineEmits<{
  (e: 'change', id: number): void
  (e: 'update', value: string): void
}>()

// 第二种获取事件
 
const emit = defineEmits(["dosth"])

使用useAttrs和useSlots


useAttrs 可以获取父组件传过来的id和class等值。 useSlots 可以获得插槽的内容。 例子中,我们使用useAttrs获取父组件传过来的id和class,useSlots获取插槽的内容。  


父组件:  

<template>

    <div class="father">{{ fatherRef }}</div>

    <Child :fatherRef="fatherRef" @changeVal="changeVal" class="btn" id="111">

        <template #test1>

        <div>1223</div>

        </template>

    </Child>

</template>

<script setup lang="ts">

import { ref } from "vue";

import Child from "./Child.vue";

const fatherRef = ref("1");

function changeVal(val: string) {

    fatherRef.value = val;

}

</script>

<style lang="scss" scoped>

.father {

    margin-top: 40px;

    margin-bottom: 40px;

}

.btn {

    font-size: 20px;

    color: red;

}

</style>

子组件:  

<template>

    <!-- <div class="child">{{ props.fatherRef }}</div> -->

    <div v-bind="attrs">

        <slot name="test1">11</slot>

        <input type="text" v-model="inputVal" />

    </div>

</template>

<script setup lang="ts">

import { computed, useAttrs, useSlots } from "vue";

const props = defineProps<{

    fatherRef: string;

}>();

const emits = defineEmits(["changeVal"]);

const slots = useSlots();

const attrs = useAttrs();

console.log(122, attrs, slots);

const inputVal = computed({

    get() {

        return props.fatherRef;

    },

    set(val: string) {

        emits("changeVal", val);

    },

});

</script>


<style lang="scss" scoped>

.child {

}

</style>

使用自定义指令


在setup里边自定义指令的时候,只需要遵循vNameOfDirective 这样的命名规范就可以了


比如如下自定义focus指令,命名就是vMyFocus,使用的就是v-my-focus


自定义指令

<script setup lang="ts">
const vMyFocus = {
  onMounted: (el: HTMLInputElement) => {
    el.focus();
    // 在元素上做些操作
  },
};
</script>
<template>
  <input v-my-focus value="111" />
</template>

使用defineExpose子组件传父组件


子组件  

<template>

    <div class="child"></div>

</template>


<script setup lang="ts">

import { ref, reactive } from "vue";

function doSth() {

    console.log(333);

}

defineExpose({ doSth });

</script>


<style lang="scss" scoped>

.child {

}

</style>

父组件  

<template>

<div class="father" @click="doSth1">222</div>

    <Child ref="childRef"></Child>

</template>

<script setup lang="ts">

import { ref, reactive } from "vue";

import Child from "./Child.vue";

const childRef = ref();

function doSth1() {

    childRef.value.doSth();

}

</script>

<style lang="scss" scoped>

.father {

}

</style>

父组件传子组件


父组件  

<template>

    <div class="father"></div>

    <Child @click="doSth"></Child>

</template>

<script setup lang="ts">

import { ref, reactive } from "vue";

import Child from "./Child.vue";

function doSth() {

    console.log(112);

}

</script>

<style lang="scss" scoped>

.father {

}

</style>

子组件  

<template>

    <div class="child">2222</div>

</template>

<script setup lang="ts">

import { ref, reactive, onMounted } from "vue";

const emits = defineEmits(["doSth"]);

onMounted(() => {

    emits("doSth");

});

</script>

<style lang="scss" scoped>

.child {

}

</style>

toRefs


当从父组件向子组件传props的时候,必须使用toRefs或者toRef进行转一下,这是为什么呢?

这里是因为如果不使用toRefs转一次的话,当父组件中的props改变的时候,子组件如果使用了Es6的解析,会失去响应性。


可以看下如下例子


父组件  

<template>

<div class="father" @click="changeVal">{{ fatherRef }}</div>

    <Child :fatherRef="fatherRef"></Child>

</template>

<script setup lang="ts">

import { ref, reactive } from "vue";

import Child from "./Child.vue";

const fatherRef = ref(1);

function changeVal() {

    fatherRef.value = 2;

}

</script>

<style lang="scss" scoped>

.father {

    margin-bottom: 40px;

}

</style>

子组件  

<template>

    <div class="child" @click="changeVal">{{ fatherRef }}</div>

</template>

<script setup lang="ts">

import { ref, reactive, onMounted, toRefs } from "vue";

const props = defineProps<{

    fatherRef: any;

}>();

const { fatherRef } = props;

function changeVal() {

    fatherRef.value = 34;
}

</script>

<style lang="scss" scoped>

.child {

}

</style>

可以看到当父组件如果点击之后,因为使用const { fatherRef } = props;进行解析,就失去了响应性

所以当父组件变成2的时候,子组件还是1。

这里有两种解决办法

  1. 使用const { fatherRef } = toRefs(props);
  2. 在模版中中使用props.fatherRef

子组件使用v-model


可以在子组件中使用computed,实现双向绑定


父组件  

<template>

    <div class="father">{{ fatherRef }}</div>

    <Child :fatherRef="fatherRef" @changeVal="changeVal"></Child>

</template>

<script setup lang="ts">

import { ref } from "vue";

import Child from "./Child.vue";

const fatherRef = ref("1");

function changeVal(val: string) {

    fatherRef.value = val;

}

</script>


<style lang="scss" scoped>

.father {

    margin-top: 40px;

    margin-bottom: 40px;

}

</style>

子组件  

<template>

    <!-- <div class="child">{{ props.fatherRef }}</div> -->

    <input type="text" v-model="inputVal" />

</template>

<script setup lang="ts">

import { computed } from "vue";

const props = defineProps<{

    fatherRef: string;

}>();

const emits = defineEmits(["changeVal"]);


const inputVal = computed({

    get() {

        return props.fatherRef;

    },

    set(val: string) {

        emits("changeVal", val);

    },

});

</script>

<style lang="scss" scoped>

.child {

}

</style>

可以从父组件传递值和改变值的方法,然后子组件也可以使用v-model


例子中父组件传递 modelValue和update:modelValue方法 父组件:  

<template>

    <Child :modelValue="searchText" @update:modelValue="changeVal"> </Child>

</template>

<script setup lang="ts">

import { ref } from "vue";

import Child from "./Child.vue";

const searchText = ref(1);

function changeVal(val: number) {

    searchText.value = val;

}

</script>

<style lang="scss" scoped>

.father {

    margin-top: 40px;

    margin-bottom: 40px;

}

.btn {

    font-size: 20px;

    color: red;

}

</style>

子组件:  

<template>

    <!-- <div class="child">{{ props.fatherRef }}</div> -->

    <!-- <div v-bind="attrs">

        <slot name="test1">11</slot>

        <input type="text" v-model="inputVal" />

    </div> -->

    <input v-model="modelValue" />

</template>


<script setup lang="ts">

import { computed, useAttrs, useSlots } from "vue";

const props = defineProps<{

    modelValue: number;

}>();

// const emits = defineEmits(["changeVal"]);

</script>


<style lang="scss" scoped>

.child {

}

</style>

递归组件


组件本身是可以调用组件自身的,也就是递归。 比如名为 Child.vue 的组件可以在其模板中用 <Child/> 引用它自己。这里需要注意的是需要设置条件语句,用来中断递归,不然递归会无限递归下去。  


父组件  

<template>
  <Child :modelValue="searchText" @update:modelValue="changeVal"> </Child>
</template>

<script setup lang="ts">
import { ref } from "vue";
import Child from "./Child.vue";
const searchText = ref(1);
function changeVal(val: number) {
  searchText.value = val;
}
</script>

<style lang="scss" scoped>
.father {
  margin-top: 40px;
  margin-bottom: 40px;
}
.btn {
  font-size: 20px;
  color: red;
}
</style>

子组件  

<template>
  <input v-model="modelValue" />
  <Child
    :modelValue="test"
    @update:modelValue="changeTest"
    v-if="modelValue > 2"
  ></Child>
</template>

<script setup lang="ts">
import { computed, useAttrs, useSlots, ref } from "vue";
const props = defineProps<{
  modelValue: number;
}>();
const test = ref(0);
function changeTest(val: number) {
  test.value = val;
}

// const emits = defineEmits(["changeVal"]);
</script>

<style lang="scss" scoped>
.child {
  position: relative;
}
</style>
相关文章
|
开发工具
Vim 模式切换 | 命令集
Vim 模式切换 | 命令集
838 0
|
机器学习/深度学习 弹性计算 云计算
阿里云ECS实例规格
阿里云ECS实例规格
|
2月前
|
应用服务中间件
新手小白:阿里云轻量应用服务器0基础上手(附最新活动价格)
阿里云轻量应用服务器2026年最新报价:新用户专享,2核2G仅38元/年(秒杀)、68元/年;2核4G 379元、4核8G 1159元、4核16G 1599元/年。全系标配200M峰值带宽、不限流量,性价比远超友商。阿里云轻量应用服务器官网:https://t.aliyun.com/U/dwftch
447 0
|
22天前
|
存储 人工智能 弹性计算
阿里云服务器学生免费领取指南:2026年最新0元获得一台学生机教程
阿里云学生认证后可领300元无门槛抵扣金,限1年内在“学生用券中心”购买≤300元且明确支持该券的商品(如轻量服务器),实现0元购机。仅限中国大陆及港澳台全日制在校生,需支付宝授权学信网验证。99元学生机不支持叠券,务必认准券适用商品!
905 0
|
2月前
|
缓存 Linux
Centos 7配置阿里云yum源
Centos 7配置阿里云yum源
377 0
|
4月前
|
弹性计算 人工智能 数据库
阿里云服务器全配置价格表:轻量、ECS、GPU 云服务器最新报价与选型指南
阿里云服务器作为国内主流的云计算基础设施,覆盖轻量应用、弹性计算、GPU计算三大核心产品线,针对个人开发者、中小企业、AI科研团队等不同用户群体,提供从入门级到企业级的全场景算力支持。8月,阿里云持续推出普惠定价政策,轻量应用服务器低至38元/年,ECS经济型实例99元/年新老同价,GPU云服务器享7.6折优惠,同时支持包年包月、按量付费、抢占式实例等多种计费模式,满足不同业务的成本与性能需求。本文将全面梳理轻量应用服务器、ECS云服务器、GPU云服务器的最新配置、价格、计费模式与适用场景,帮助用户精准选型、高效上云。
510 2
|
5月前
|
存储 人工智能 缓存
企业AI私有化与云端协同方案:GLM5.2部署+OpenClaw/Hermes运维教程
GLM 5.2是一款753B混合专家架构开源旗舰大模型,原生支持百万Token超长上下文、复杂代码工程、长周期自主智能体任务,完整支持本地、私有云自托管部署。自托管模式能够实现全部业务数据不出环境、企业自定义推理调度、全链路操作私有化审计,对数据安全管控有严格要求的研发、金融、政企团队具备不可替代价值。整套落地链路包含硬件分级选型、多量化权重存储配置、vLLM/SGLang双推理框架部署、长期使用成本盈亏测算四大核心模块,同时可搭配轻量应用服务器部署OpenClaw、Hermes两类主流AI智能体,结合百炼Token Plan云端订阅算力构建混合AI架构,兼顾私有化数据安全与云端灵活弹性
636 1
|
6月前
|
存储 人工智能 前端开发
本地离线知识库的隐私底线 数据落用户目录不出域,察元AI还超级智能体
察元AI桌面版坚守“数据不出域”核心承诺:所有对话、知识库、文件、密钥、日志均本地存储(CHAYUAN_ROOT下),默认禁用遥测,Sidecar绑定127.0.0.1,模型密钥强加密。支持Ollama等本地模型实现完全闭环,0字节外传,满足政企等保与国产化合规要求。(239字)
|
8月前
|
数据采集 人工智能 搜索推荐
AI搜索时代的权衡:Geo优化中哪些是核心基石,哪些是次要干扰?
在生成式AI重塑搜索的今天,SEO正加速演进为GEO(生成式引擎优化)。本文基于麦肯锡2025报告与于磊专家实践,提出“两大核心”(人性化Geo、内容交叉验证)与“四轮驱动”(EEAT、结构化内容、关键词兼容、精准引用)方法论,厘清必须做与不必急做的关键边界,助力企业抢占AI搜索新入口。
402 2
|
12月前
|
弹性计算
阿里云服务器最便宜多少钱一年?38元一年,配置、价格及购买限制介绍
现在阿里云服务器最便宜多少钱一年?38元一年,配置、价格及购买限制介绍来了,亲身测试是38元一年,价格确实便宜,38元一年相当于3元1个月、一毛钱一天。这是一台轻量应用服务器,200M峰值带宽、2核2G、40G ESSD系统盘,不限流量。