有关Vue3中is,key,ref三个常用内置属性的使用

Ps:本人习惯使用setup语法糖的形式

is

在V3中,is属性用于动态组件的渲染,这个与V2中的用法类似,但是也有一些细微的差距,is属性允许你根据条件的动态渲染不同的组件或者HTML标签。

<template>
  <div>
    <!-- 动态渲染组件 -->
    <component :is="currentComponent"></component>

    <!-- 动态渲染HTML标签 -->
    <component :is="currentTag">
      动态标签内容
    </component>

    <!-- 切换按钮 -->
    <button @click="toggleComponent">切换组件</button>
    <button @click="toggleTag">切换标签</button>
  </div>
</template>

<script setup>
import { ref } from 'vue';
import ComponentA from './ComponentA.vue';
import ComponentB from './ComponentB.vue';

// 定义动态的组件和标签
const currentComponent = ref('ComponentA');
const currentTag = ref('div');

// 切换组件的方法
const toggleComponent = () => {
  currentComponent.value = currentComponent.value === 'ComponentA' ? 'ComponentB' : 'ComponentA';
};

// 切换标签的方法
const toggleTag = () => {
  currentTag.value = currentTag.value === 'div' ? 'h1' : 'div';
};
</script>

key

Vue3中key是一个常用的内置组件,通常用于确保Vue能够正确的管理和渲染元素或者组件列表。key属性的作用是在更新虚拟DOM时为每一个元素或者组件分配的唯一标识,以优化渲染性能和正确性。

使用场景一:

列表渲染

<template>
  <ul>
    <li v-for="item in items" :key="item.id">
      {{ item.text }}
    </li>
  </ul>
</template>

<script setup>
import { ref } from 'vue';

const items = ref([
  { id: 1, text: 'Item 1' },
  { id: 2, text: 'Item 2' },
  { id: 3, text: 'Item 3' }
]);
</script>

使用场景二:

强制组件重新渲染

<template>
  <component :is="currentComponent" :key="currentKey"></component>
  <button @click="toggleComponent">切换组件</button>
</template>

<script setup>
import { ref } from 'vue';
import ComponentA from './ComponentA.vue';
import ComponentB from './ComponentB.vue';

const currentComponent = ref('ComponentA');
const currentKey = ref(0);

const toggleComponent = () => {
  currentComponent.value = currentComponent.value === 'ComponentA' ? 'ComponentB' : 'ComponentA';
  currentKey.value += 1; // 改变 key 强制重新渲染组件
};
</script>

ref

在Vue3中,ref是一个非常重要的内置属性,用于获取和操作DOM元素或者子组件的实例。ref允许你在模版中给某个元素或组件添加一个标识,然后在组件的脚本部分通过该标识直接访问该元素或组件的实例。从而进行一些操作,比如聚焦输入框,调用子组件的方法等。

使用场景一:

获取DOM元素:

<template>
  <input ref="inputElement" type="text" />
  <button @click="focusInput">聚焦输入框</button>
</template>

<script setup>
import { ref, onMounted } from 'vue';

const inputElement = ref(null);

const focusInput = () => {
  inputElement.value.focus();
};

// 如果需要在组件挂载后立即聚焦
onMounted(() => {
  inputElement.value.focus();
});
</script>

使用场景二:

获取子组件的实例

<!-- 子组件 ChildComponent.vue -->
<template>
  <div>子组件内容</div>
</template>

<script setup>
const someMethod = () => {
  console.log('子组件的方法被调用');
};

// 通过 defineExpose 公开的方法和数据可以在父组件中通过 ref 访问
defineExpose({
  someMethod
});
</script>
<!-- 父组件 -->
<template>
  <ChildComponent ref="childComponent" />
  <button @click="callChildMethod">调用子组件方法</button>
</template>

<script setup>
import { ref } from 'vue';
import ChildComponent from './ChildComponent.vue';

const childComponent = ref(null);

const callChildMethod = () => {
  childComponent.value.someMethod();
};
</script>

Vue 3 ,`ref` 是一个新的响应式 API,用于在模板引用一个元素或组件,并可以在 JavaScript 代码访问该元素或组件的属性和方法。 在模板,可以使用 `ref` 指令来创建一个 `ref` 对象,并将其绑定到一个元素或组件上。例如: ```html <template> <div> <input type="text" ref="inputRef" /> <button @click="handleClick">Click Me</button> </div> </template> <script> import { ref } from 'vue'; export default { setup() { const inputRef = ref(null); function handleClick() { inputRef.value.focus(); } return { inputRef, handleClick, }; }, }; </script> ``` 在这个例子,我们使用 `ref` 指令将 `input` 元素绑定到 `inputRef` 变量上。在 `setup` 函数,我们使用 `ref` 函数创建了一个 `ref` 对象,并将其初始值设置为 `null`。然后,我们在 `handleClick` 函数访问了 `inputRef.value`,并调用了 `focus` 方法,以便将焦点设置到 `input` 元素上。 需要注意的是,在 Vue 3 ,`ref` 不再返回一个对象,而是返回一个包含 `value` 属性的普通 JavaScript 对象。因此,在访问 `ref` 对象的属性和方法时,需要使用 `.value` 来访问其值。 另外,在 Vue 3 ,`ref` 还可以用于引用组件,例如: ```html <template> <div> <MyComponent ref="myComponentRef" /> <button @click="handleClick">Click Me</button> </div> </template> <script> import { ref } from 'vue'; import MyComponent from './MyComponent.vue'; export default { components: { MyComponent, }, setup() { const myComponentRef = ref(null); function handleClick() { myComponentRef.value.someMethod(); } return { myComponentRef, handleClick, }; }, }; </script> ``` 在这个例子,我们使用 `ref` 指令将 `MyComponent` 组件绑定到 `myComponentRef` 变量上。在 `setup` 函数,我们使用 `ref` 函数创建了一个 `ref` 对象,并将其初始值设置为 `null`。然后,我们在 `handleClick` 函数访问了 `myComponentRef.value`,并调用了 `someMethod` 方法,以便调用 `MyComponent` 组件的某个方法。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

LJ小番茄

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值