Styles are not being applied with v-bind class in Vue 3.

huangapple go评论58阅读模式
英文:

Styles are not being applied with v-bind class in Vue 3

问题

我正在尝试对导航栏文本应用下划线,但它不起作用。

英文:
<template>
  <a class="nav-link" :class="activeClasses" :href="page.link.url">
    {{ page.link.text }}
  </a>
</template>

<script>
export default {
  props: ["page", "isActive"],
  computed: {
    activeClasses() {
      return {
        active: this.isActive,
        emphasize: this.isActive,
      };
    },
  },
};
</script>

<style scoped>
.emphasize {
  text-decoration: underline !important;
}
</style>

I am trying to apply an underline to the navbar text but it is not working.

答案1

得分: 1

Your component works as expected. Seems you're passing wrong props to your component. Here it works just fine:

<script setup>
  import Comp from './Comp.vue';
  import { ref } from 'vue';
  const active = ref(false);
</script>

<template>
  <div>
    <comp :is-active="active" :page="{ link: { text: 'example' } }"></comp>
  </div>
  <button @click="active = !active">Toggle</button>
</template>

Vue.js Playground

英文:

Your component works as expected. Seems you're passing wrong props to your component. Here it works just fine:

&lt;script setup&gt;
  import Comp from &#39;./Comp.vue&#39;;
  import {ref} from &#39;vue&#39;;
  const active = ref(false);
&lt;/script&gt;

&lt;template&gt;
  &lt;div&gt;
    &lt;comp :is-active=&quot;active&quot; :page=&quot;{link:{text:&#39;example&#39;}}&quot;&gt;&lt;/comp&gt;
  &lt;/div&gt;
  &lt;button @click=&quot;active = !active&quot;&gt;Toggle&lt;/button&gt;
&lt;/template&gt;

Vue.js Playground

答案2

得分: 0

我同意Alexander的观点。你可能想要像这样修改你的props:

props: {
  page: { type: String, default: '' },
  isActive: { type: Boolean, default: false }
},
英文:

I agree with Alexander. You might want to modify your props like this:

props: {
  page: { type: String, default: &#39;&#39; },
  isActive: { type: Boolean, default: false }
},

huangapple
  • 本文由 发表于 2023年6月9日 04:06:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/76435360.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定