【发布时间】: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 从服务器获取文本。现在我可以看到通知的文本,但看不到它的时间。似乎我的函数getTime 与router-link 中的某些内容重叠。当我尝试将<span> 与getTime 功能放在router-link 下时,会显示通知时间,但我的页面布局正在折叠。请帮忙!
【问题讨论】:
-
According to the documentation
v-html用 html 替换所有子项。 -
那么有没有办法摆脱这种行为?
-
把
v-html放在你想要的范围内。 -
天哪!现在可以了!非常感谢!
-
@RoyJ 您应该将您的解决方案作为答案,而不是评论。 :)
标签: javascript vue.js momentjs vue-component directive