【问题标题】:Cannot Remove Underline From Vue router-link无法从 Vue 路由器链接中删除下划线
【发布时间】:2020-12-11 01:34:26
【问题描述】:

我想我几乎尝试了所有尝试从 router-link 中删除下划线的方法。

这是我的代码:

<router-link :to="{name: 'Plan'}">
   <div>Plan Your Trip</div>
      <div class='expander'>
      <router-link :to="{name: 'Plan'}">COVID-19</router-link>
      <router-link :to="{name: 'Plan'}">Visa</router-link>
      <router-link :to="{name: 'Plan'}">Essentials</router-link>
   </div>
</router-link>

我正在尝试仅从 子链接 中删除下划线。

我尝试过的事情:

内嵌样式

<router-link style="text-decoration: none !important;" :to="{name: 'Plan'}">COVID-19</router-link>

分配类

<router-link class="sub-link" :to="{name: 'Plan'}">COVID-19</router-link>

<style scoped>
   .sub-link{text-decoration: none !important;}
</style>

声明标签

<router-link tag="div" :to="{name: 'Plan'}">COVID-19</router-link>

<style scoped>
   div{text-decoration: none !important;}
</style>

分配单独的标签 + 为该标签声明类

<router-link :to="{name: 'Plan'}">
   <div class="sub-link">COVID-19</div>
</router-link>

这些只是几个列表,我确实尝试了我能想到的所有可能的方法......我是否错过了一些关于自定义 Vue router-link 的内容?

【问题讨论】:

  • 你能检查 DOM 中的那个元素并发布那个片段吗?它在这里工作jsfiddle.net/kxec8s0L
  • 是的,我也已经尝试过了。有截图。 prntscr.com/u3lvix。我不知道为什么在 DOM 中声明了空的“类”?
  • .expander a { text-decoration: none !important} 你可以试试这个css一次
  • 运气不好 :( 但坦率地说,其他声明的样式虽然有效......?只是不是'文本装饰'......prntscr.com/u3lzui
  • 这很奇怪

标签: css vue.js vuejs2 vue-router routerlink


【解决方案1】:

使用display: inline-block;text-decoration:none;,诀窍是display: inline-block;

Css spec 状态

对于建立内联格式化上下文的块容器, 装饰被传播到包装的匿名内联元素 块容器的所有流入内联级子级。对所有人 其他元素它被传播到任何流入的孩子。注意 文本装饰不会传播到浮动和绝对 定位后代,也不是原子内联级别的内容 内联块和内联表等后代。

示例:代码中的链接COVID-19 将删除下划线。

<router-link :to="{name: 'Plan'}">
   <div>Plan Your Trip</div>
      <div class='expander'>
      <router-link :to="{name: 'Plan'}" style="display: inline-block;text-decoration:none;">COVID-19</router-link>
      <router-link :to="{name: 'Plan'}">Visa</router-link>
      <router-link :to="{name: 'Plan'}">Essentials</router-link>
   </div>
</router-link>

下面是一个演示:

let Layout = {
  template: `<div>
  <h4>Layout Page </h4>
  <router-link to="/contact">
  <div>
    <p>Links<p>
    <router-link to="/contact/add" style="display: inline-block;text-decoration:none;">Add1</router-link>
    <router-link to="/addcontact">Add2</router-link>
  </div>
  </router-link>
  <router-view></router-view>
  </div>`
};
let Home = {
  template: '<div>this is the home page. Go to <router-link to="/contact">contact</router-link> </div>'
};

let ContactList = {
  // add <router-view> in order to load children route of path='/contact'
  template: '<div>this is contact list, click <router-link to="/contact/add">Add Contact In sub Router-View</router-link> here to add contact<p><router-view></router-view></p> Or Click <router-link to="/addcontact">Add Contact In Current Router-View</router-link></div>'
};

let ContactAdd = {
  template: '<div>Contact Add</div>'
}

let router = new VueRouter({
  routes: [{
    path: '/',
    redirect: 'home',
    component: Layout,
    children: [{
        path: 'home',
        component: Home
      },
      {
        path: 'contact',
        component: ContactList,
        children: [{
          path: 'add',
          component: ContactAdd
        }]
      },
      {
        path: 'addcontact', // or move ContactAdd as direct child route of path=`/`
        component: ContactAdd,
      }
    ]
  }]
});

new Vue({
  el: '#app',
  components: {
    'App': {
      template: '<div><router-view></router-view></div>'
    },
  },
  router
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<script src="https://unpkg.com/vue-router@3.0.1/dist/vue-router.js"></script>
<section id="app">
  <app></app>
</section>

【讨论】:

  • 这成功了!谢谢!你能解释一下这种方法是如何工作的吗?
  • Css spec 声明下划线将不适用于已设置父元素的内联块text-decoration: underline
  • 我以为是 Vue 的问题,但它是 CSS 哇!谢谢!
【解决方案2】:

外部router-linktext-decoration: underline 应用于其内部文本,内部router-links 也将text-decoration: underline 应用于其内部文本。

此时,您的内心 router-links 基本上已经应用了双下划线。

您需要从两者中删除它。如果您需要另一个元素来拥有text-decoration: underline,请单独为该元素设置它。

【讨论】:

    【解决方案3】:

    当您检查 DOM 中的路由器链接时,您会看到它是一个 a 标记。请记住,即使删除了初始下划线,当您将鼠标悬停在路由器链接文本上时也会出现下划线。

    使用这个sn-p

    <router-link :to="{name: 'Plan'}">
       <div>Plan Your Trip</div>
          <div class='expander'>
          <router-link :to="{name: 'Plan'}">COVID-19</router-link>
          <router-link :to="{name: 'Plan'}">Visa</router-link>
          <router-link :to="{name: 'Plan'}">Essentials</router-link>
       </div>
    </router-link>

    .expander a {
      text-decoration: none;
    }
    
    .expander a:hover {
      text-decoration: none;
    }

    【讨论】:

      猜你喜欢
      • 2017-12-02
      • 1970-01-01
      • 1970-01-01
      • 2015-11-10
      • 2020-01-18
      • 2011-02-16
      • 1970-01-01
      相关资源
      最近更新 更多