【问题标题】:Vue.js: conflict v-html and a functionVue.js:冲突 v-html 和一个函数
【发布时间】:2018-02-02 15:04:26
【问题描述】:

这是通知组件的模板:

<template>
 <div>
  <li class="g-line-height-1_2">
   <router-link :to="linkFromNotification(item)"
               @click.native="readNotification(item)"
               v-html="item.message"
               :class="activeNotification">
  <br>
  <span class="g-font-size-12 g-color-gray-dark-v5">
    {{ getTime }}
  </span>
   </router-link>
  </li>
  <li>
   <hr class="g-my-0">
  </li>
 </div>
</template>

<script>
import { mapActions, mapGetters } from 'vuex';
import moment from 'moment';


export default {
 props: ['item'],
 computed: {
  ...mapGetters([
    'getLastNotifications',
    'getNotifications',
  ]),
  activeNotification() {
    if (this.item.viewed === true) {
      return 'nav-link g-bg-gray-light-v5--hover g-px-20 g-py-10 u-link-v5'
    } else {
      return 'nav-link g-bg-primary-opacity-x--hover g-bg-primary-opacity-x2 g-px-20 g-py-10 u-link-v5'
    }
  },
  getTime() {
    moment.locale('ru');
    return moment(this.item.created_at, 'YYYY-MM-DD HH:mm:ss Z').fromNow();
  },
},
methods: {
  ...mapActions([
    'notifications',
    'readNotification'
  ]),
  linkFromNotification(item) {
    if (item.notification_type === 'user_subscribed') {
      return {name: 'person', params: {id: item.object_id}}
    } else if (['comment_created', 'answer_selected', 'answer_created'].includes(item.notification_type)) {
      // TODO: link must be constructed with hash
      return `/posts/${item.object_id}#${item.anchor}`;
    } else if (item.notification_type === 'user_coauthored') {
      return {name: 'show_post', params: {id: item.object_id}}
    }
  }
 }
}
</script>

每个通知都使用v-html 从服务器获取文本。现在我可以看到通知的文本,但看不到它的时间。似乎我的函数getTimerouter-link 中的某些内容重叠。当我尝试将&lt;span&gt;getTime 功能放在router-link 下时,会显示通知时间,但我的页面布局正在折叠。请帮忙!

【问题讨论】:

  • According to the documentation v-html 用 html 替换所有子项。
  • 那么有没有办法摆脱这种行为?
  • v-html放在你想要的范围内。
  • 天哪!现在可以了!非常感谢!
  • @RoyJ 您应该将您的解决方案作为答案,而不是评论。 :)

标签: javascript vue.js momentjs vue-component directive


【解决方案1】:

v-html 设置它附加到的元素的内容。包含标签内的任何内容都将被覆盖。要将 HTML 添加到现有内容,请在要插入 HTML 的位置创建 &lt;span&gt;,并将 v-html 放在该范围内。

【讨论】:

  • 我这样做了,现在可以了。谢谢!记住 Vue.js 中的这些细微差别是件好事。
猜你喜欢
  • 2019-02-12
  • 2019-05-07
  • 2015-10-07
  • 2021-07-03
  • 2017-07-05
  • 2017-09-21
  • 2020-07-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多