vue.js - How to check component id, if id same then hide other component
问题描述
i try to hide other component based on id that store a slug data, if id of the component is not same as props id, i want to hide the other component
i cannot access the id of the component, i already try using $refs.componentName.id
// html
<div class="box-portfolio" ref="boxPortfolio" v-for="item in data" v-
bind:key="item.slug">
<div class="wrap-the-portfolio"
ref="portfolioList"
:id="item.slug">
{{item.slug}}
</div>
//js
data() {
return {
idRoute: '',
data: []
}
},
props: ['id'],
watch: {
$route: function() {
if(this.$router.currentRoute.name=='UserPortfolioDetail'){
const paramsNow = this.$route.params.id;
console.log(this.$refs.portfolioList); // get the component
console.log(id); // can get the id
if(this.refs.portfolioList.id == id){
// do hide other $refs.portfolioList component
}
}
}
},
解决方案
You can not get id directly by using this.$refs
. Since this.$refs.componentName
will return a vue instance, it is not a HTML DOM structure. Instead, you can use v-if
to show a specific DOM.
<div class="wrap-the-portfolio"
v-if="item.slug === id && $router.currentRoute.name !=='UserPortfolioDetail'">
{{item.slug}}
</div>
推荐阅读
- javascript - 电子新手。如何为浏览器窗口文档中的按钮设置事件侦听器?
- python - 如何找到在python中发生continue-sattement的迭代
- php - 如何在 php 中修复此无效字符错误?
- firefox - 在 Firefox(开发者版)中禁用 sameSite cookie 策略
- python - 在语义分割中查找验证集中特定数据点的准确性/损失
- c# - “HttpRequest”不包含“Url”的定义
- php - 照片上传代码不起作用,对于之前已经起作用的代码
- php - 如何显示在请求服务器期间执行的所有函数的名称和 PHP 脚本的文件名?
- javascript - 如何使用相同的 console.log 在不同的行上打印多个数组?
- javascript - Can't figure out why it isn't working, anymore