v-model
指令将数据绑定到 DOM 元素,实现双向数据绑定。v-on
指令绑定事件监听器,响应用户交互。created
、mounted
等生命周期钩子在组件的不同阶段执行相应的操作。下面列举了 Vue 3 中常用的模板指令,并为每个指令提供一个简单的示例,帮助你更好地理解它们的应用场景。
v-if
和 v-else
:用于条件渲染,根据条件决定是否显示元素。<template>
<div v-if="showGreeting">
<h1>Hello, World!h1>
div>
<div v-else>
<p>Greeting is hidden.p>
div>
template>
<script setup>
import { ref } from 'vue';
const showGreeting = ref(true);
script>
v-else-if
: 与 v-if
和 v-else
配合使用,用于添加更多条件判断。<template>
<div v-if="age < 18">
<p>You are a minor.p>
div>
<div v-else-if="age >= 18 && age < 65">
<p>You are an adult.p>
div>
<div v-else>
<p>You are a senior citizen.p>
div>
template>
<script setup>
import { ref } from 'vue';
const age = ref(25);
script>
v-for
:用于循环渲染列表数据。<template>
<ul>
<li v-for="item in items" :key="item.id">
{{ item.name }} - {{ item.price }}
li>
ul>
template>
<script setup>
import { ref } from 'vue';
const items = ref([
{ id: 1, name: 'Apple', price: 1.5 },
{ id: 2, name: 'Banana', price: .8 },
{ id: 3, name: 'Orange', price: 1.2 }
]);
script>
v-bind
:用于动态绑定属性,例如 class
、style
、src
等。<template>
<img :src="imageUrl" :alt="imageDescription" />
template>
<script setup>
import { ref } from 'vue';
const imageUrl = ref('https://example.com/image.jpg');
const imageDescription = ref('An example image');
script>
v-model
:用于双向数据绑定,将输入框的值与数据模型同步。<template>
<input type="text" v-model="message" />
<p>You typed: {{ message }}p>
template>
<script setup>
import { ref } from 'vue';
const message = ref('');
script>
v-on
:用于绑定事件监听器,例如 click
、mouseover
等。<template>
<button v-on:click="handleClick">Click mebutton>
template>
<script setup>
import { ref } from 'vue';
const count = ref();
const handleClick = () => {
count.value++;
console.log('Button clicked!');
};
script>
v-show
:用于控制元素的显示与隐藏,它会保留元素在 DOM 中,适合频繁切换显示状态的场景。<template>
<div v-show="isVisible">
<p>This content is visible.p>
div>
template>
<script setup>
import { ref } from 'vue';
const isVisible = ref(true);
script>
v-html
:用于将字符串渲染为 HTML 内容。<template>
<div v-html="htmlContent">div>
template>
<script setup>
import { ref } from 'vue';
const htmlContent = ref('This is HTML content.
');
script>
v-text
:用于将字符串渲染为文本内容,它会解析 HTML 标签。<template>
<div v-text="textContent">div>
template>
<script setup>
import { ref } from 'vue';
const textContent = ref('This is text content.
');
script>
v-slot
:用于定义和使用插槽,允许父组件向子组件传递内容。
<template>
<div>
<slot name="header">slot>
<p>This is the child component's content.p>
<slot>slot>
div>
template>
<template>
<ChildComponent>
<template #header>
<h1>This is the headerh1>
template>
<p>This is the parent component's content.p>
ChildComponent>
template>
v-pre
: 指示 Vue.js 忽略该元素的内容,用于避免 Vue.js 尝试解析模板语法。<template>
<div v-pre>
<p>{{ message }}p>
div>
template>
<script setup>
import { ref } from 'vue';
const message = ref('This is a message without interpolation.');
script>
v-once
: 指示 Vue.js 只渲染元素一次,之后不再更新,用于优化性能。<template>
<div v-once>
<p>{{ message }}p>
div>
template>
<script setup>
import { ref } from 'vue';
const message = ref('This message will not update.');
script>
setup
函数,将数据、方法和生命周期钩子组织到一起,更灵活地管理组件逻辑。ref
、reactive
和 computed
等 API,实现数据的响应式更新,自动更新视图。Vue 3 作为最新的框架版本,拥有更强大的性能、更灵活的开发方式和更完善的生态系统。通过学习本篇文章,你将掌握 Vue 3 的核心知识和功能,开启你的 Vue 3 开发之旅。
记住,学习 Vue 3 的最佳途径是不断实践,并积极参与社区交流!