🔥 遇见我们的新项目 t0ggles - 你最终的项目管理工具! 🔥

消息 Vue 组件

消息组件将帮助您在应用程序中可视化评论和消息系统。

消息组件

包含以下组件

  • 消息
  • 消息
  • MessagesTitle

消息道具

名称类型默认值描述
组件字符串'div'

组件的 HTML 元素

消息道具

名称类型默认值描述
头像字符串

消息用户的头像 URL

颜色对象
colors.bubbleReceivedIos字符串'bg-[#e5e5ea] dark:bg-[#252525]'
colors.bubbleReceivedMd字符串'dark:bg-md-dark-surface-variant bg-[#e5e5ea]'
colors.bubbleSentIos字符串'bg-primary'
colors.bubbleSentMd字符串'bg-md-light-primary dark:bg-md-dark-primary dark:text-md-dark-on-primary'
colors.messageNameIos字符串'text-black text-opacity-45 dark:text-white dark:text-opacity-45'
colors.messageNameMd字符串'text-md-light-on-surface-variant dark:text-md-dark-on-surface-variant'
colors.messageSent字符串'text-white'
组件字符串'div'

组件的 HTML 元素

页脚字符串

消息页脚的内容

页眉字符串

消息页眉的内容

id字符串

消息 id 属性

姓名字符串

消息名称

文本字符串

消息文本

textFooter字符串

消息页脚文本

textHeader字符串

消息页眉文本

类型字符串'sent'
Message type: sent (default) or received

消息插槽

名称描述
头像

消息用户的头像 URL

页脚

消息页脚的内容

页眉

消息页眉的内容

文本

消息文本

MessagesTitle 道具

名称类型默认值描述
颜色对象
colors.titleMd字符串'text-md-light-on-surface-variant dark:text-md-dark-on-surface-variant'
组件字符串'div'

组件的 HTML 元素

示例

Messages.vue
<template>
<k-page class="ios:bg-white ios:dark:bg-black messages-page">
<k-navbar title="Messages" />
<k-messages>
<k-messages-title
><b>{{ currentDay }}</b
>, {{ currentTime }}</k-messages-title
>
<k-message
v-for="(message, index) in messagesData"
:key="index"
:type="message.type"
:name="message.name"
:text="message.text"
>
<template v-if="message.type === 'received'" #avatar>
<img
alt="avatar"
class="w-8 h-8 rounded-full"
:src="message.avatar"
/>
</template>
</k-message>
</k-messages>
<k-messagebar
placeholder="Message"
:value="messageText"
@input="onMessageTextChange"
>
<template #left>
<k-link toolbar icon-only>
<k-icon>
<template #ios><CameraFill class="w-7 h-7" /></template>
<template #material>
<MdCameraAlt
class="w-6 h-6 fill-black dark:fill-md-dark-on-surface"
/>
</template>
</k-icon>
</k-link>
</template>
<template #right>
<k-link
toolbar
:style="{
opacity: inputOpacity,
cursor: isClickable ? 'pointer' : 'default',
}"
@click="onClick"
>
<k-icon>
<template #ios><ArrowUpCircleFill class="w-7 h-7" /></template>
<template #material>
<MdSend class="w-6 h-6 fill-black dark:fill-md-dark-on-surface" />
</template>
</k-icon>
</k-link>
</template>
</k-messagebar>
</k-page>
</template>
<script>
import { ref, onMounted, watch, nextTick } from 'vue';
import {
kPage,
kNavbar,
kNavbarBackLink,
kMessagebar,
kMessages,
kMessage,
kMessagesTitle,
kIcon,
kLink,
} from 'konsta/vue';
import { CameraFill, ArrowUpCircleFill } from 'framework7-icons/vue';
import MdCameraAlt from '../components/MdCameraAlt.vue';
import MdSend from '../components/MdSend.vue';
export default {
components: {
kPage,
kNavbar,
kNavbarBackLink,
kMessagebar,
kMessages,
kMessage,
kMessagesTitle,
kIcon,
kLink,
CameraFill,
ArrowUpCircleFill,
MdCameraAlt,
MdSend,
},
setup() {
const messageText = ref('');
const isClickable = ref(false);
const inputOpacity = ref(0.3);
const onMessageTextChange = (e) => {
messageText.value = e.target.value;
isClickable.value = messageText.value.trim().length > 0;
};
watch(messageText, (newValue) => {
inputOpacity.value = newValue ? 1 : 0.3;
});
const messagesData = ref([
{
type: 'sent',
text: 'Hi, Kate',
},
{
type: 'sent',
text: 'How are you?',
},
{
type: 'received',
text: 'Hi, I am good!',
avatar: 'https://cdn.framework7.io/placeholder/people-100x100-9.jpg',
},
{
name: 'Blue Ninja',
type: 'received',
text: 'Hi there, I am also fine, thanks! And how are you?',
avatar: 'https://cdn.framework7.io/placeholder/people-100x100-7.jpg',
},
{
type: 'sent',
text: 'Hey, Blue Ninja! Glad to see you ;)',
},
{
type: 'sent',
text: 'How do you feel about going to the movies today?',
},
{
type: 'received',
text: ' Oh, great idea!',
avatar: 'https://cdn.framework7.io/placeholder/people-100x100-9.jpg',
},
{
type: 'received',
text: ' What cinema are we going to?',
avatar: 'https://cdn.framework7.io/placeholder/people-100x100-9.jpg',
},
{
name: 'Blue Ninja',
type: 'received',
text: 'Great. And what movie?',
avatar: 'https://cdn.framework7.io/placeholder/people-100x100-7.jpg',
},
{
name: 'Blue Ninja',
type: 'received',
text: 'What time?',
avatar: 'https://cdn.framework7.io/placeholder/people-100x100-7.jpg',
},
]);
const scrollToBottom = (animate = true) => {
const pageEl = document.querySelector('.messages-page');
pageEl.scrollTo({
top: pageEl.scrollHeight - pageEl.offsetHeight,
behavior: animate ? 'smooth' : 'auto',
});
};
onMounted(() => scrollToBottom(false));
watch(messagesData, () => {
scrollToBottom();
});
const handleSendClick = () => {
const text = messageText.value.replace(/
/g, '<br>').trim();
const type = 'sent';
const messagesToSend = [];
if (text.length) {
messagesToSend.push({
text,
type,
});
console.log(messagesToSend);
}
if (messagesToSend.length === 0) {
return;
}
messagesData.value.push(...messagesToSend);
messageText.value = '';
nextTick(() => {
scrollToBottom();
});
};
const onClick = () => {
if (isClickable.value) {
handleSendClick();
}
};
const dateFormatter = new Intl.DateTimeFormat('en-US', {
weekday: 'long',
month: 'short',
day: 'numeric',
});
const timeFormatter = new Intl.DateTimeFormat('en-US', {
hour12: false,
hour: '2-digit',
minute: '2-digit',
});
const currentDate = new Date();
const currentDay = dateFormatter.format(currentDate);
const currentTime = timeFormatter.format(currentDate);
return {
onClick,
messageText,
messagesData,
onMessageTextChange,
handleSendClick,
inputOpacity,
isClickable,
currentDay,
currentTime,
};
},
};
</script>
代码许可证在 MIT.
2022 © Konsta UI by nolimits4web.