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

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

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

问题

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

英文:
  1. <template>
  2. <a class="nav-link" :class="activeClasses" :href="page.link.url">
  3. {{ page.link.text }}
  4. </a>
  5. </template>
  6. <script>
  7. export default {
  8. props: ["page", "isActive"],
  9. computed: {
  10. activeClasses() {
  11. return {
  12. active: this.isActive,
  13. emphasize: this.isActive,
  14. };
  15. },
  16. },
  17. };
  18. </script>
  19. <style scoped>
  20. .emphasize {
  21. text-decoration: underline !important;
  22. }
  23. </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:

  1. <script setup>
  2. import Comp from './Comp.vue';
  3. import { ref } from 'vue';
  4. const active = ref(false);
  5. </script>
  6. <template>
  7. <div>
  8. <comp :is-active="active" :page="{ link: { text: 'example' } }"></comp>
  9. </div>
  10. <button @click="active = !active">Toggle</button>
  11. </template>

Vue.js Playground

英文:

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

  1. &lt;script setup&gt;
  2. import Comp from &#39;./Comp.vue&#39;;
  3. import {ref} from &#39;vue&#39;;
  4. const active = ref(false);
  5. &lt;/script&gt;
  6. &lt;template&gt;
  7. &lt;div&gt;
  8. &lt;comp :is-active=&quot;active&quot; :page=&quot;{link:{text:&#39;example&#39;}}&quot;&gt;&lt;/comp&gt;
  9. &lt;/div&gt;
  10. &lt;button @click=&quot;active = !active&quot;&gt;Toggle&lt;/button&gt;
  11. &lt;/template&gt;

Vue.js Playground

答案2

得分: 0

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

  1. props: {
  2. page: { type: String, default: '' },
  3. isActive: { type: Boolean, default: false }
  4. },
英文:

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

  1. props: {
  2. page: { type: String, default: &#39;&#39; },
  3. isActive: { type: Boolean, default: false }
  4. },

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:

确定