单击组件时向主体添加类? [英] Add a class to body when component is clicked?

查看:15
本文介绍了单击组件时向主体添加类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 vue 中有一个组件,我希望在点击时切换主体上的一个类.

I have a component in vue, I wish to toggle a class on the body on click.

我该怎么做?我是否只需要使用 JS 来定位正文并添加一个类?

How can I do this? Would I just have to use JS to target the body and add a class?

或者有更多的vue方式吗?

Or is there more of a vue way?

对于上下文,我需要向正文添加一个无滚动类,以防止滚动覆盖菜单.

For context I need to add a no scroll class to the body to prevent scrolling for an overlay menu.

推荐答案

我认为对主体的反应式绑定通常是不受欢迎的.请参阅此 Vue 团队成员他链接到的文章.这让我觉得没有一种vue 方式"可以在单击组件时更改主体的类.

I think that reactive binding to the body is generally frowned upon. See this forum response by Vue team member and the article he links to. This makes me think there is not a "vue way" to change the body's class when a component is clicked.

所以我认为,就像你说的,用 JS 定位身体是最好的选择.

So I think, like you say, targeting the body with JS is the best option.

这篇关于单击组件时向主体添加类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆