【发布时间】:2017-11-16 10:25:55
【问题描述】:
我使用Vue router 有两个页面:
let routes = [
{
path: '/',
component: require('./components/HomeView.vue')
},
{
path: '/intro',
component: require('./components/IntroView.vue')
}
]
这很好用,只是我的每个组件都有不同的主体样式:
HomeView.vue:
<template>
<p>This is the home page!</p>
</template>
<script>
export default {
}
</script>
<style>
body {
background: red;
}
</style>
IntroView.vue:
<template>
<div>
<h1>Introduction</h1>
</div>
</template>
<script>
export default {
}
</script>
<style>
body {
background: pink;
}
</style>
我的目标是让这两个页面具有不同的背景样式(最终在它们之间进行过渡)。但是在我去home路线的那一刻(带有red背景),然后点击intro路线,背景颜色保持red(我希望它变成pink)。
编辑: index.html:
<body>
<div id="app">
<router-link to="/" exact>Home</router-link>
<router-link to="/intro">Introduction</router-link>
<router-view></router-view>
</div>
<script src="/dist/build.js"></script>
</body>
【问题讨论】:
-
您可以使用 $router 对象来为您的主体添加一个类并在全局工作表中设置它的样式?
-
所有样式将在全球范围内可用。所以你的解决方案就是上面那个
-
编辑了我的问题以包括我的
index.html。当body在应用程序之外时,我看不到如何向它添加一个类。 -
我测试了你的代码,这工作正常...你没有错误吗?是否应用了粉红色背景但可能被红色覆盖? (这没有理由发生)
-
Evan You 建议更改身体等级:
The easy way is just setting document.body.className in a global beforeEach hook.这个解决方案已有两年历史,但我猜仍然可以使用来自:forum-archive.vuejs.org/topic/656/…
标签: javascript vue.js vue-router vue-loader