我试图将着陆页面的背景颜色设置为橙色,但问题在于当前的设置会使得所有页面都变成橙色。
我尝试给着陆页面添加scoped
,这样它就只会对该页面进行样式设置,但这么做之后整个页面都不再是橙色了。
最终目标是只影响着陆页面。
我已经尝试过使用 HTML 和 Body 代替 ' * ',但它们在这种情况下也无法正常工作。
landingpage.vue
:
<template>
<div class="container">
<div class=landing>
<h1>Hello.</h1>
<router-link to="/work">
<button id="work" type="button">See my work</button>
</router-link>
<router-link to="/home">
<button id="home" type="button">Go home</button>
</router-link>
</div>
</div>
</template>
<script>
export default {
name: 'Landing',
};
</script>
<style scoped>
* {
background: orange;
}
h1 {
margin-top: 100px;
text-align: center;
font-size: 80px;
color: green;
}
#work {
color: green;
border: none;
font-size: 25px;
text-decoration: none;
margin: 0;
position: absolute;
top: 50%;
left: 50%;
-ms-transform: translate(-50%, -50%);
transform: translate(-50%, -50%);
}
#work:hover {
color: white;
}
#home{
color: green;
border: none;
font-size: 15px;
text-decoration: none;
margin: 0;
position: absolute;
top: 70%;
left: 50%;
-ms-transform: translate(-50%, -50%);
transform: translate(-50%, -50%);
}
#home:hover {
color: white;
}
</style>
created: function () { ... }
在VueConstructor
中的确等同于created() { ... }
。我个人更喜欢第二种语法,因为它更短,但由于它们都产生相同的结果,我很少推荐其中任何一种。这两种语法对于任何hook
、computed
或method
都是有效的(包括computed
中的 setter/getter 函数)。 - taodestroyed
中将值设置为任何内容(“white”,“transparent”或“initial”)在技术上是错误的。假设某些CSS应用了body
的background-color
的属性为red
,您的组件在mounted
中将其设置为orange
,然后在destroyed
中将其设置为transparent
。body
现在具有与以前不同的背景颜色。您的组件应仅取消应用的规则。最清洁的方法是在组件内部添加一个类,并将其样式化,然后在destroyed
中删除该类。否则,在某些情况下,您可能无法恢复body
的CSS初始状态。 - tao