Html Vue-如何更改不同组件中相同元素的样式

Html Vue-如何更改不同组件中相同元素的样式,html,css,vue.js,Html,Css,Vue.js,我正在尝试将特定div的样式从一个组件更改为另一个组件。每个组件都附加到应用程序组件中的模板中,并替换标记。div#应用程序需要在Hello.vue组件中填充172px,在其余组件上填充0px。这可能吗?我如何实现这一点 App.vue <template> <div id="app"> <div> <img src="./assets/logo.png" align="center"> </div>

我正在尝试将特定div的样式从一个组件更改为另一个组件。每个组件都附加到应用程序组件中的模板中,并替换
标记。div#应用程序需要在
Hello.vue
组件中填充172px,在其余组件上填充0px。这可能吗?我如何实现这一点

App.vue

<template>
  <div id="app">
    <div>
      <img src="./assets/logo.png" align="center">
    </div>
    <router-view></router-view>
  </div>
</template>

<script>
export default {
  name: 'app'
}
</script>

<style>
#app {
  font-family: 'Avenir', Helvetica, Arial, sans-serif;
  background-color: hsla(275, 97%, 50%, 0.57);
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 0px;
  height: inherit;
}

body{
  height: inherit;
}

html{
  height: 100%;
}
</style>
<template>
  <div class="hello">
    <h1>{{ msg }}</h1>
    <ul>
      <li><h2><router-link to="/app/login">Login</router-link></h2></li>
      <li><h2><router-link to="/app/about">About Us</router-link></h2></li>
    </ul>
  </div>
</template>

<script>
export default {
  name: 'hello',
  data () {
    return {
      msg: 'Welcome to Vue'
    }
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>

#app{ 
    padding-top: 172px; /*This does not work*/
}

h1, h2 {
  font-weight: normal;
}

ul {
  list-style-type: none;
  padding: 0;
}

li {
  display: inline-block;
  margin: 0 10px;
}

a {
  color: #000000;
}
</style>

导出默认值{
名称:“应用程序”
}
#应用程序{
字体系列:“Avenir”、Helvetica、Arial、无衬线字体;
背景色:hsla(275,97%,50%,0.57);
-webkit字体平滑:抗锯齿;
-moz osx字体平滑:灰度;
文本对齐:居中;
颜色:#2c3e50;
边际上限:0px;
身高:继承;
}
身体{
身高:继承;
}
html{
身高:100%;
}
你好。vue

<template>
  <div id="app">
    <div>
      <img src="./assets/logo.png" align="center">
    </div>
    <router-view></router-view>
  </div>
</template>

<script>
export default {
  name: 'app'
}
</script>

<style>
#app {
  font-family: 'Avenir', Helvetica, Arial, sans-serif;
  background-color: hsla(275, 97%, 50%, 0.57);
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 0px;
  height: inherit;
}

body{
  height: inherit;
}

html{
  height: 100%;
}
</style>
<template>
  <div class="hello">
    <h1>{{ msg }}</h1>
    <ul>
      <li><h2><router-link to="/app/login">Login</router-link></h2></li>
      <li><h2><router-link to="/app/about">About Us</router-link></h2></li>
    </ul>
  </div>
</template>

<script>
export default {
  name: 'hello',
  data () {
    return {
      msg: 'Welcome to Vue'
    }
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>

#app{ 
    padding-top: 172px; /*This does not work*/
}

h1, h2 {
  font-weight: normal;
}

ul {
  list-style-type: none;
  padding: 0;
}

li {
  display: inline-block;
  margin: 0 10px;
}

a {
  color: #000000;
}
</style>

{{msg}}
  • 登录
  • 关于我们
导出默认值{ 姓名:'你好', 数据(){ 返回{ msg:“欢迎来到Vue” } } } #app{ 填充顶部:172px;/*这不起作用*/ } h1,h2{ 字体大小:正常; } 保险商实验室{ 列表样式类型:无; 填充:0; } 李{ 显示:内联块; 利润率:0.10px; } a{ 颜色:#000000; }
通过检查路线路径,有一种快速的方法可以做到这一点(不确定这是否是最佳做法);假设通过路径
/Hello
呈现
Hello.vue

<div id="app" :style="{ padding: $route.path === '/hello' ? '172px' : '0px' }">
  ...
</div>

...

如果它出现两次,则不应具有相同的
id
@RoyJ请仔细阅读,id
应用程序只有一个实例。组件中的模板被注入到App.vue模板中。工作非常完美!谢谢如果你碰巧找到了一种直接在css中实现这一点的方法,请让我知道。我该如何添加这一行呢<代码>左边距:($route.path==('/app/my account'| |'/app/fleet overview'| |'/app/rankings')?'10%:'')
我似乎找不到将其添加到你的答案中的方法。请尝试
左边距:(['/app/my account','/app/fleet overview','/app/fleet overview','/app/rankings')。包括($route.path)?'10%:'')