如何在Vue条件绑定下基于嵌套JSON实现点击公司图标渲染对应项目组件
现有代码核心问题梳理
- 数据结构不统一:UNICEF的项目是数组格式,其余合作公司的项目是单对象格式,
v-for遍历单对象时会出现属性取值异常 - 组件传值语法错误:Vue 3中给组件传递props不需要加
v-前缀,原代码里的v-icon/v-icon-color属于自定义指令写法,无法正确传递项目参数 - 状态逻辑冲突:
data里同时把projects初始化为字符串、后续又覆盖为项目数组,直接渲染{{projects}}会出现内容错乱 - 挂载点位置错误:
app.mount('#display-projects')会把应用根节点绑定到项目列表的内部div上,和外层容器的结构产生冲突
调整后可直接运行的实现代码
<!-- 合作公司可点击图标触发区 --> <div class="company-icon-group"> <div class="company-icon" @click="showCompanyProjects('unicef')">UNICEF 触发图标</div> <div class="company-icon" @click="showCompanyProjects('drpc')">DRPC 触发图标</div> <div class="company-icon" @click="showCompanyProjects('fhi')">FHI 触发图标</div> <div class="company-icon" @click="showCompanyProjects('jsi')">JSI 触发图标</div> </div> <!-- 项目展示区,默认隐藏 --> <div class="container" v-if="seen"> <header class="section-header"> <h3>{{ currentCompanyName }}</h3> </header> <div class="row"> <project-executed v-for="project in currentProjects" :icon="project.icon" :icon-color="project.color" :title="project.title" :description="project.description" ></project-executed> </div> </div>
const app = Vue.createApp({ data() { return { seen: false, activeCompany: '', currentCompanyName: '', // 统一把所有公司的项目存为数组结构,兼容单项目/多项目场景 companyProjects: { unicef: [ { "icon": "ion-ios-analytics-outline", "color": "#ff689b", "title": "UNICEF Nigeria – Drivers of Violence Against Children", "description": "VKM in 2017/ 2018 provided consulting services to UNICEF Child Protection section. We conducted the Drivers of Violence Against Children study which required a systematic review of the literature, secondary data analysis and an interventions’ mapping across four focal states (Lagos, Cross River, Gombe and Plateau) of the country." }, { "icon": "ion-ios-bookmarks-outline", "color": "#e9bf06", "title": "UNICEF Nigeria – Time to teach: Determinants of Teacher Absenteeism in Sub-Saharan Africa", "description": "VKM was identified by UNICEF in 2018 to lead the Nigeria component of a multi-country study on the determinants of teacher absenteeism in sub-Saharan Africa. VKM has so far participated in a multi-country training in Ghana and preparing for the data collection exercise which will include key informant interviews, focus group discussions, survey of primary school teachers identified and an assessment of primary schools studied." }, { "icon": "ion-ios-paper-outline", "color": "#3fcdc7", "title": "UNICEF Nigeria – Iron-Folic Acid Supply Chain Assessment in Northern Nigeria", "description": "VKM was contracted by UNICEF in January 2019 to conduct an assessment of the Iron-Folic Acid supply chain system in six states (Jigawa, Katsina, Kebbi, Sokoto, Yobe, Zamfara) in Northern Nigeria." } ], drpc: [ { "icon": "ion-ios-speedometer-outline", "color": "#41cf2e", "title": "Policy and Strategic Plan Development and Research", "description": "VKM has expertise in the development of national level policies and strategic plans. Our chief executive led the development of the Nigeria Health Information System Strategic Plan (2014-2018) and the Sierra Leone Health Information System Strategic Plan (2017-2021)." } ], fhi: [ { "icon": "ion-ios-world-outline", "color": "#d6ff22", "title": "Research on Civil Registration and Vital Statistics (CRVS) System", "description": "VKM has carried out several studies on birth registration in Nigeria collaborating with researchers across the world in the process." } ], jsi: [ { "icon": "ion-ios-clock-outline", "color": "#4680ff", "title": "Health Information Systems Strengthening", "description": "VKM developed the Nigeria Health Facility Registry (HFR) for the Federal Ministry of Health. The project was funded by USAID through the MEASURE Evaluation project under an institutional contract with VKM." } ] } } }, computed: { // 自动匹配当前选中公司的项目数组 currentProjects() { return this.companyProjects[this.activeCompany] || [] } }, methods: { // 通用触发方法,无需为每个公司写单独的展示逻辑 showCompanyProjects(companyKey) { this.activeCompany = companyKey this.currentCompanyName = companyKey.toUpperCase() this.seen = true } } }); app.component('project-executed', { props: ['icon', 'iconColor', 'title', 'description'], template: ` <div class="col-md-6 col-lg-5" data-wow-delay="0.1s" data-wow-duration="1.4s"> <div class="box"> <div class="icon"> <i :class="icon" :style="{ color: iconColor }"></i> </div> <h4 class="title"> <a href="">{{title}}</a> </h4> <p class="description"> {{description}} </p> </div> </div> ` }); // 把挂载点移到页面最外层根容器 app.mount('#app');
关键调整说明
- 统一项目数据格式,不管合作公司对应1个还是多个项目,都包装为数组结构,
v-for遍历不会出现取值错误 - 简化交互逻辑,用通用方法替代重复的单公司展示函数,新增公司时只需要在配置里加对应条目即可
- 修正组件传值规则,使用Vue标准的
:prop名="值"语法传递参数,去掉错误的v-自定义指令写法 - 修复组件模板语法,动态class和样式使用Vue官方绑定方式,替代原字符串插值的不规范写法
内容的提问来源于stack exchange,提问作者Olawoye Omotayo
相关产品推荐
相关产品推荐

