gpt4 book ai didi

css - 在 vue.js 中,我只能在当前路由器下更改父元素的 css 吗?

转载 作者:行者123 更新时间:2023-11-28 01:05:51 26 4
gpt4 key购买 nike

我想在组件中更改父级的样式。而且我不想更改其他组件的 CSS。看看我的代码。

App.vue

<div class="page-content">
<router-view ref="routeview"></router-view>
</div>

router/index.js

  routes: [
{ path: '/home', name: 'Home', component: HomeView },
{ path: '/heroes', name: 'Heroes', component: HeroesView },
]

主视图

<style>
.page-content {
background-color: red;
}
</style>
<style scoped>
</style>

如果我像上面那样把 .page-content CSS 放在全局 CSS 区域,它也会改变 HerosView 的样式。那不是我想要的。我想更改当前 HomeView 页面下的 .page-content 背景。

这可能吗?

最佳答案

router.beforeEach 钩子(Hook)中添加 styled

router.beforeEach((to, from, next) => {
if(...) {
document.getElementsByClassName('page-content')[0].classList.toggle('styled');
}
})

<style>
.page-content.styled {
background-color: red;
}
</style>

关于css - 在 vue.js 中,我只能在当前路由器下更改父元素的 css 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52365593/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com