当在VUE3中的特定页面中,有条件的渲染div类在负载上的div类

发布于 2025-02-01 01:56:27 字数 1569 浏览 0 评论 0原文

我是Vue的新手,并且在加载页面时,我有一个由子组件管理的类和页面管理的类别的有条件渲染问题,

我有两个部分,一个组件(navbar)和页面(在我的情况下)。 通过添加或不是特定的类,我只想在主页上加载Navbar。

因此,我在纳维尔博尔模板中写了这部分:

<navbar class="normalclass" :class={transparent:istransparent}>Something</navbar>

export default {

    name: 'Navbar',
    data() {
        return {
            istransparent:false, // By default is false

        }

    }
}

在视图主页中

import { ref } from 'vue'
export default {
name: 'Home',
 
setup () {
    const istransparent = ref(true)
    return {
        istransparent
    }
}}
// I also tried to insert just this code but it doesn't change the result.
  setup () {
     return {
        istransparent:true
     }
  }

<Navbar />

<router-view />

<Footer />

js 我错了,最好的解决方案是做我需要的事情。

我问那些比我有更多经验的人寻求帮助。

##########编辑

###########

而不是使用要在单个组件(或在这种情况下)中读取的变量,例如Home.Vue,

我使用Vue-Router来找出我所处的页面。因此,由于我只希望在家中的透明标头,因此在navbar.vue组件中,我添加了此代码以说如果我在家中,则变量为真,因此我添加了所需的类。 在这里使用的代码:

在navbar.vue:

<navbar class="normalclass" :class= 
{transparent:isHome}>Something</navbar>

js:

export default {

    name: 'Navbar',
        computed: {
            isHome() {
                 return this.$route.name == 'Home' // Check if router name is 'Home' 
        
            }
         }

    }
}

我不知道这是最好的方法,但是它有效并且感觉稳定,而代码很少。为了知识,我与您分享。

I'm new in Vue and I have a problem with conditional rendering of a class managed by a child component and a page

When I load a page I have two sections, a component (navbar) and the page (in my case Home).
By adding or not a specific class I would like the navbar in transparent, only when it is loaded on the Home page.

So I wrote this part in the navbar template:

<navbar class="normalclass" :class={transparent:istransparent}>Something</navbar>

JS:

export default {

    name: 'Navbar',
    data() {
        return {
            istransparent:false, // By default is false

        }

    }
}

In the views Home.vue I have instead inserted this code:

import { ref } from 'vue'
export default {
name: 'Home',
 
setup () {
    const istransparent = ref(true)
    return {
        istransparent
    }
}}
// I also tried to insert just this code but it doesn't change the result.
  setup () {
     return {
        istransparent:true
     }
  }

In App.vue I have this part of the template that manages the rendering of the application

<Navbar />

<router-view />

<Footer />

I didn't understand exactly where I am wrong and what would be the best solution to do what I need.

I ask those who have more experience than me for help.

########## EDIT #########

Ok, I solved the problem with a different approach.

Instead of using a variable that is to be read in individual components (or in this case view), such as home.vue

I used vue-router to find out what page I was on. So since I want the transparent header only in the home, in the Navbar.vue component I have added this code to say that if I am in home then the variable is true, so I add the classes I need.
Here the code used:

In navbar.vue:

<navbar class="normalclass" :class= 
{transparent:isHome}>Something</navbar>

JS:

export default {

    name: 'Navbar',
        computed: {
            isHome() {
                 return this.$route.name == 'Home' // Check if router name is 'Home' 
        
            }
         }

    }
}

I don't know if that's the best approach, but it works and feels stable, with very little code. I share it with you for the sake of knowledge.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文