当前位置:   article > 正文

Vue3 + setup + ts 使用技巧_vue3 setup ts

vue3 setup ts

1.组件引入
当使用 setup 的时候,组件直接引入就可以了,不需要再自己手动注册

  1. <template>
  2. <Child />
  3. </template>
  4. <script setup lang="ts">
  5. import Child from "./Child.vue";
  6. </script>

2.ref 和 reactive
ref 一般用于基本的数据类型,比如 string,boolean ,reactive 一般用于对象 ref 的地方其实也是调用的 reactive 实现的。

  1. <template>
  2. <h1>{{ title }}</h1>
  3. <div>
  4. {{ data }}
  5. </div>
  6. </template>
  7. <script setup lang="ts">
  8. import { ref, reactive } from "vue";
  9. const title = ref("title");
  10. const data = reactive({
  11. userName: "xiaoming",
  12. age: 18,
  13. });
  14. </script>

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

  1. // 第一种不带默认值props
  2. const props = defineProps<{
  3. foo: string
  4. bar?: number
  5. }>()
  6. // 第二种带默认值props
  7. export interface ChildProps {
  8. foo: string
  9. bar?: number
  10. }
  11. const props = withDefaults(defineProps<ChildProps>(), {
  12. foo: "1qsd"
  13. bar?: 3
  14. })
  15. // 第一种获取事件
  16. const emit = defineEmits<{
  17. (e: 'change', id: number): void
  18. (e: 'update', value: string): void
  19. }>()
  20. // 第二种获取事件
  21. const emit = defineEmits(["dosth"])

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

父组件:

  1. <template>
  2. <div class="father">{{ fatherRef }}</div>
  3. <Child :fatherRef="fatherRef" @changeVal="changeVal" class="btn" id="111">
  4. <template #test1>
  5. <div>1223</div>
  6. </template>
  7. </Child>
  8. </template>
  9. <script setup lang="ts">
  10. import { ref } from "vue";
  11. import Child from "./Child.vue";
  12. const fatherRef = ref("1");
  13. function changeVal(val: string) {
  14. fatherRef.value = val;
  15. }
  16. </script>
  17. <style lang="scss" scoped>
  18. .father {
  19. margin-top: 40px;
  20. margin-bottom: 40px;
  21. }
  22. .btn {
  23. font-size: 20px;
  24. color: red;
  25. }
  26. </style>

子组件:

  1. <template>
  2. <!-- <div class="child">{{ props.fatherRef }}</div> -->
  3. <div v-bind="attrs">
  4. <slot name="test1">11</slot>
  5. <input type="text" v-model="inputVal" />
  6. </div>
  7. </template>
  8. <script setup lang="ts">
  9. import { computed, useAttrs, useSlots } from "vue";
  10. const props = defineProps<{
  11. fatherRef: string;
  12. }>();
  13. const emits = defineEmits(["changeVal"]);
  14. const slots = useSlots();
  15. const attrs = useAttrs();
  16. console.log(122, attrs, slots);
  17. const inputVal = computed({
  18. get() {
  19. return props.fatherRef;
  20. },
  21. set(val: string) {
  22. emits("changeVal", val);
  23. },
  24. });
  25. </script>

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

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

自定义指令

  1. <script setup lang="ts">
  2. const vMyFocus = {
  3. onMounted: (el: HTMLInputElement) => {
  4. el.focus();
  5. // 在元素上做些操作
  6. },
  7. };
  8. </script>
  9. <template>
  10. <input v-my-focus value="111" />
  11. </template>

5.使用 defineExpose 子组件传父组件
子组件

  1. <template>
  2. <div class="child"></div>
  3. </template>
  4. <script setup lang="ts">
  5. import { ref, reactive } from "vue";
  6. function doSth() {
  7. console.log(333);
  8. }
  9. defineExpose({ doSth });
  10. </script>

父组件

  1. <template>
  2. <div class="father" @click="doSth1">222</div>
  3. <Child ref="childRef"></Child>
  4. </template>
  5. <script setup lang="ts">
  6. import { ref, reactive } from "vue";
  7. import Child from "./Child.vue";
  8. const childRef = ref();
  9. function doSth1() {
  10. childRef.value.doSth();
  11. }
  12. </script>

6.父组件传子组件 
父组件

  1. <template>
  2. <div class="father"></div>
  3. <Child @doSth="doSth"></Child>
  4. </template>
  5. <script setup lang="ts">
  6. import { ref, reactive } from "vue";
  7. import Child from "./Child.vue";
  8. function doSth() {
  9. console.log(112);
  10. }
  11. </script>

子组件

  1. <template>
  2. <div class="child">2222</div>
  3. </template>
  4. <script setup lang="ts">
  5. import { ref, reactive, onMounted } from "vue";
  6. const emits = defineEmits(["doSth"]);
  7. onMounted(() => {
  8. emits("doSth");
  9. });
  10. </script>

7.toRefs

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

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

可以看下如下例子

父组件

  1. <template>
  2. <div class="father" @click="changeVal">{{ fatherRef }}</div>
  3. <Child :fatherRef="fatherRef"></Child>
  4. </template>
  5. <script setup lang="ts">
  6. import { ref, reactive } from "vue";
  7. import Child from "./Child.vue";
  8. const fatherRef = ref(1);
  9. function changeVal() {
  10. fatherRef.value = 2;
  11. }
  12. </script>
  13. <style lang="scss" scoped>
  14. .father {
  15. margin-bottom: 40px;
  16. }
  17. </style>


子组件

  1. <template>
  2. <div class="child" @click="changeVal">{{ fatherRef }}</div>
  3. </template>
  4. <script setup lang="ts">
  5. import { ref, reactive, onMounted, toRefs } from "vue";
  6. const props = defineProps<{
  7. fatherRef: any;
  8. }>();
  9. const { fatherRef } = props;
  10. function changeVal() {
  11. fatherRef.value = 34;
  12. }
  13. </script>

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

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

这里有两种解决办法

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

8.子组件使用 v-model
8.1 可以在子组件中使用 computed,实现双向绑定
父组件

  1. <template>
  2. <div class="father">{{ fatherRef }}</div>
  3. <Child :fatherRef="fatherRef" @changeVal="changeVal"></Child>
  4. </template>
  5. <script setup lang="ts">
  6. import { ref } from "vue";
  7. import Child from "./Child.vue";
  8. const fatherRef = ref("1");
  9. function changeVal(val: string) {
  10. fatherRef.value = val;
  11. }
  12. </script>
  13. <style lang="scss" scoped>
  14. .father {
  15. margin-top: 40px;
  16. margin-bottom: 40px;
  17. }
  18. </style>


子组件

  1. <template>
  2. <!-- <div class="child">{{ props.fatherRef }}</div> -->
  3. <input type="text" v-model="inputVal" />
  4. </template>
  5. <script setup lang="ts">
  6. import { computed } from "vue";
  7. const props = defineProps<{
  8. fatherRef: string;
  9. }>();
  10. const emits = defineEmits(["changeVal"]);
  11. const inputVal = computed({
  12. get() {
  13. return props.fatherRef;
  14. },
  15. set(val: string) {
  16. emits("changeVal", val);
  17. },
  18. });
  19. </script>

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

  1. <template>
  2. <Child :modelValue="searchText" @update:modelValue="changeVal"> </Child>
  3. </template>
  4. <script setup lang="ts">
  5. import { ref } from "vue";
  6. import Child from "./Child.vue";
  7. const searchText = ref(1);
  8. function changeVal(val: number) {
  9. searchText.value = val;
  10. }
  11. </script>
  12. <style lang="scss" scoped>
  13. .father {
  14. margin-top: 40px;
  15. margin-bottom: 40px;
  16. }
  17. .btn {
  18. font-size: 20px;
  19. color: red;
  20. }
  21. </style>

子组件:

  1. <template>
  2. <!-- <div class="child">{{ props.fatherRef }}</div> -->
  3. <!-- <div v-bind="attrs">
  4. <slot name="test1">11</slot>
  5. <input type="text" v-model="inputVal" />
  6. </div> -->
  7. <input v-model="modelValue" />
  8. </template>
  9. <script setup lang="ts">
  10. import { computed, useAttrs, useSlots } from "vue";
  11. const props = defineProps<{
  12. modelValue: number;
  13. }>();
  14. // const emits = defineEmits(["changeVal"]);
  15. </script>

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

父组件

  1. <template>
  2. <Child :modelValue="searchText" @update:modelValue="changeVal"> </Child>
  3. </template>
  4. <script setup lang="ts">
  5. import { ref } from "vue";
  6. import Child from "./Child.vue";
  7. const searchText = ref(1);
  8. function changeVal(val: number) {
  9. searchText.value = val;
  10. }
  11. </script>
  12. <style lang="scss" scoped>
  13. .father {
  14. margin-top: 40px;
  15. margin-bottom: 40px;
  16. }
  17. .btn {
  18. font-size: 20px;
  19. color: red;
  20. }
  21. </style>

子组件

  1. <template>
  2. <input v-model="modelValue" />
  3. <Child
  4. :modelValue="test"
  5. @update:modelValue="changeTest"
  6. v-if="modelValue > 2"
  7. ></Child>
  8. </template>
  9. <script setup lang="ts">
  10. import { computed, useAttrs, useSlots, ref } from "vue";
  11. const props = defineProps<{
  12. modelValue: number;
  13. }>();
  14. const test = ref(0);
  15. function changeTest(val: number) {
  16. test.value = val;
  17. }
  18. // const emits = defineEmits(["changeVal"]);
  19. </script>
  20. <style lang="scss" scoped>
  21. .child {
  22. position: relative;
  23. }
  24. </style>

10.vue3 ts 获取组件 ref 实例
通过ref直接拿到dom引用

  1. <template>
  2. <div class="demo1-container">
  3. <div ref="sectionRef" class="ref-section"></div>
  4. </div>
  5. </template>
  6. <script setup lang="ts">
  7. import {ref} from 'vue'
  8. const sectionRef = ref()
  9. </script>

通过对div元素添加了ref属性,为了获取到这个元素,我们声明了一个与ref属性名称相同的变量sectionRef,然后我们通过 sectionRef.value 的形式即可获取该div元素。

通过父容器的ref遍历拿到dom引用

  1. <template>
  2. <div class="demo2-container">
  3. <div ref="listRef" class="list-section">
  4. <div @click="higherAction(index)" class="list-item" v-for="(item, index) in state.list" :key="index">
  5. <span>{{item}}</span>
  6. </div>
  7. </div>
  8. </div>
  9. </template>
  10. <script setup lang="ts">
  11. import { ref, reactive } from 'vue'
  12. const listRef = ref()
  13. </script>

通过对父元素添加了ref属性,并声明了一个与ref属性名称相同的变量listRef,此时通过listRef.value会获得包含子元素的dom对象 此时可以通过listRef.value.children[index]的形式获取子元素dom

通过:ref将dom引用放到数组中

  1. <template>
  2. <div class="demo2-container">
  3. <div class="list-section">
  4. <div :ref="setRefAction" @click="higherAction(index)" class="list-item" v-for="(item, index) in state.list" :key="index">
  5. <span>{{item}}</span>
  6. </div>
  7. </div>
  8. </div>
  9. </template>
  10. <script setup lang="ts">
  11. import { reactive } from 'vue'
  12. const state = reactive({
  13. list: [1, 2, 3, 4, 5, 6, 7],
  14. refList: [] as Array<any>
  15. })
  16. const setRefAction = (el: any) => {
  17. state.refList.push(el);
  18. }
  19. </script>

通过:ref循环调用setRefAction方法,该方法会默认接收一个el参数,这个参数就是我们需要获取的div元素 此时可以通过state.refList[index]的形式获取子元素dom

通过子组件emit传递ref

  1. <template>
  2. <div ref="cellRef" @click="cellAction" class="cell-item">
  3. <span>{{item}}</span>
  4. </div>
  5. </template>
  6. <script setup lang="ts">
  7. import {ref} from 'vue';
  8. const props = defineProps({
  9. item: Number
  10. })
  11. const emit = defineEmits(['cellTap']);
  12. const cellRef = ref();
  13. const cellAction = () => {
  14. emit('cellTap', cellRef.value);
  15. }
  16. </script>

通过对子组件添加了ref属性,并声明了一个与ref属性名称相同的变量cellRef,此时可以通过emit将cellRef.value作为一个dom引用传递出去

tsx 等 render 组件中获取的方式更简单

  1. import { defineComponent, ref, onMounted } from "@vue/runtime-core";
  2. import { ElForm } from "element-plus";
  3. export default defineComponent({
  4. setup() {
  5. const $form = ref<InstanceType<typeof ElForm>>(null);
  6. onMounted(() => {
  7. $form.value?.validate; // 类型正确
  8. });
  9. return () => <ElForm ref={$form}></ElForm>;
  10. },
  11. });


需要注意的是,如果使用 expose 暴露方法出去,无法获取到对应的类型,您需要自定义类型 github.com/vuejs/rfcs/…[1]

  1. // 组件 MyForm
  2. import { defineComponent, ref, onMounted } from "@vue/runtime-core";
  3. import { ElForm } from "element-plus";
  4. type ELEForm = InstanceType<typeof ElForm>;
  5. // 在外界通过 ref 获取组件实例 请使用这个类型
  6. export interface MyFormExpose {
  7. validate: ELEForm["validate"];
  8. }
  9. export default defineComponent({
  10. name: "MyForm",
  11. setup(props, { expose }) {
  12. const $form = ref<InstanceType<typeof ElForm>>(null);
  13. expose({
  14. validate: (callback) => $form.value?.validate(callback),
  15. } as MyFormExpose);
  16. return () => <ElForm ref={$form}></ElForm>;
  17. },
  18. });
  19. <template>
  20. <MyForm :ref="$form" />
  21. </template>
  22. <script>
  23. import { defineComponent, ref, onMounted } from '@vue/runtime-core'
  24. import MyForm, { MyFormExpose } from '@/components/MyForm'
  25. export default defineComponent({
  26. components: { MyForm }
  27. setup(){
  28. const $form = ref<InstanceType<typeof MyForm> & MyFormExpose>(null)
  29. onMounted(() => {
  30. $form.value?.validate // 类型正确
  31. })
  32. }
  33. })
  34. </script>
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/494369
推荐阅读
相关标签
  

闽ICP备14008679号