第7节:Vue3 动态绑定多个属性

可以使用v-bind指令将多个属性动态绑定到元素上。以下是一个简单的实例:

复制代码
<template>
  <view class="container">
    <text v-bind="dynamicProps">{{ message }}</text>
    <button @click="toggleActive">切换激活状态</button>
  </view>
</template>

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

export default {
  setup() {
    const isActive = ref(false);
    const message = ref('Hello, uniapp!');
    const dynamicProps = computed(() => ({
      class: isActive.value ? 'active' : '',
      'aria-label': message.value,
    }));

    function toggleActive() {
      isActive.value = !isActive.value;
    }

    return {
      isActive,
      message,
      dynamicProps,
      toggleActive,
    };
  },
};
</script>

<style scoped>
.container {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 100%;
}

.active {
  color: red;
}
</style>

在这个例子中,我们使用computed函数创建了一个名为dynamicProps的计算属性,它根据isActive和message的值动态生成一个包含多个属性的对象。然后,我们使用v-bind指令将这些动态属性绑定到 元素上。

订阅专栏,每日更新

第8节:Vue3 全局访问

相关推荐
energy_DT22 分钟前
2026年十五五油气田智能增产装备数字孪生,CIMPro孪大师赋能“流动增产工厂”三维可视化管控
前端
龙猫里的小梅啊26 分钟前
CSS(四)CSS文本属性
前端·css
MXN_小南学前端33 分钟前
watch详解:与computed 对比以及 Vue2 / Vue3 区别
前端·javascript·vue.js
饭小猿人39 分钟前
Flutter实现底部动画弹窗有两种方式
开发语言·前端·flutter
让学习成为一种生活方式1 小时前
pbtk v 3.5.0安装与使用--生信工具084
前端·chrome
heimeiyingwang1 小时前
【架构实战】FinOps云成本优化实践
前端·chrome·架构
2601_949814691 小时前
Docker部署Spring Boot + Vue项目
vue.js·spring boot·docker
Mr Xu_2 小时前
从后端数据到前端图表:深入解析 reduce 与 flatMap 的数据整形实战
前端·javascript
玖玖passion2 小时前
Windows 上部署 Hermes Agent 完整指南 - 让你的 AI 助手在 WSL2 中跑起来
前端·后端·github