利用vue3仿苹果系统侧边消息提示效果实例

yizhihongxing

下面我就给您详细讲解利用vue3仿苹果系统侧边消息提示效果的完整攻略。

1. 概述

本文将介绍如何利用vue3实现仿苹果系统侧边消息提示效果。为了达到这个目的,我们将使用一些vue3的特性,如Teleport,Composition API等。

2. 准备工作

在正式开始实现之前,我们需要完成一些准备工作。

2.1 创建项目

首先,我们需要创建一个新的vue3项目。您可以使用vue-cli来完成这个任务。在终端中输入以下命令创建新的vue3项目:

npm install -g @vue/cli
vue create message-notification

2.2 安装依赖

接下来,我们需要安装几个必要的依赖,包括tailwindcss,@headlessui/vue,autoprefixerpostcss。在终端中输入以下命令安装依赖:

npm install tailwindcss @headlessui/vue autoprefixer postcss --save

2.3 配置样式

由于我们使用了tailwindcss,我们需要在项目中初始化它,为此我们需要创建一个tailwind.config.js文件:

npx tailwindcss init

2.4 组件及相关配置

现在我们已经完成了大部分的准备工作。接下来,我们需要创建两个组件,一个是MessageNotification组件,另一个是MessageList组件,用于显示通知。同时,还需要定义一些相关的vuex状态和方法。

具体代码如下:

<!-- src/components/MessageNotification.vue -->
<template>
  <div>
    <Transition
      enter-active-class="transition ease-out duration-150"
      enter-from-class="transform opacity-0 translate-y-2"
      enter-to-class="transform opacity-100"
      leave-active-class="transition ease-in duration-100"
      leave-from-class="transform opacity-100"
      leave-to-class="transform opacity-0 scale-95"
    >
      <div
        v-show="show"
        class="fixed bottom-0 right-0 mb-4 mr-4 z-50 shadow-lg rounded-lg overflow-hidden"
      >
        <div class="bg-white px-4 py-3 flex items-start">
          <div class="flex-shrink-0">
            <svg
              class="h-6 w-6 text-green-400"
              fill="none"
              stroke-linecap="round"
              stroke-linejoin="round"
              stroke-width="2"
              viewBox="0 0 24 24"
              stroke="currentColor"
            >
              <path d="M5 13l4 4L19 7"></path>
            </svg>
          </div>
          <div class="ml-3">
            <p class="text-sm font-medium text-gray-900">New message from {{ message.from }}</p>
            <p class="mt-1 text-sm text-gray-500">{{ message.content }}</p>
          </div>
        </div>
      </div>
    </Transition>
  </div>
</template>

<script>
import { defineComponent } from 'vue'
import { Transition } from '@headlessui/vue'

export default defineComponent({
  name: 'MessageNotification',
  props: {
    message: {
      type: Object,
    },
  },
  setup(props) {
    const show = true

    return {
      show,
    }
  },
})
</script>
<!-- src/components/MessageList.vue -->
<template>
  <div class="max-h-96 overflow-auto pt-3">
    <div v-for="(message, index) in messages" :key="index">
      <MessageNotification :message="message" />
    </div>
  </div>
</template>

<script>
import { defineComponent } from 'vue'
import MessageNotification from '@/components/MessageNotification.vue'

export default defineComponent({
  name: 'MessageList',
  components: {
    MessageNotification,
  },
  computed: {
    messages() {
      return this.$store.getters.getMessages
    },
  },
})
</script>
// src/store/modules/message.js
const state = {
  messages: [],
}

const getters = {
  getMessages: state => state.messages,
}

const mutations = {
  ADD_MESSAGE: (state, message) => {
    state.messages.push(message)
  },
  REMOVE_MESSAGE: (state, index) => {
    state.messages.splice(index, 1)
  },
}

const actions = {
  addMessage: ({ commit }, message) => {
    commit('ADD_MESSAGE', message)
  },
  removeMessage: ({ commit }, index) => {
    commit('REMOVE_MESSAGE', index)
  },
}

export default {
  namespaced: true,
  state,
  getters,
  mutations,
  actions,
}
// src/main.js
import { createApp } from 'vue'
import App from './App.vue'
import store from './store'

import 'tailwindcss/dist/tailwind.css'

const app = createApp(App)

app.use(store)

app.mount('#app')
// src/store/index.js
import { createStore } from 'vuex'
import message from './modules/message'

export default createStore({
  modules: {
    message,
  },
})

3. 示例说明

现在我们已经完成了所有的准备工作。接下来,我们将展示两个例子,说明如何使用我们的MessageNotification组件。

3.1 添加新消息

我们可以在组件挂载后,通过调用store.dispatch()方法,来添加新的消息:

<template>
  <div>
    <MessageList />
  </div>
</template>

<script>
import { defineComponent, onMounted } from 'vue'
import MessageList from '@/components/MessageList.vue'
import { getRandomMessage } from '@/utils/message.js'

export default defineComponent({
  name: 'App',
  components: {
    MessageList,
  },
  setup() {
    onMounted(() => {
      const interval = setInterval(() => {
        const message = getRandomMessage()
        store.dispatch('message/addMessage', message)
      }, 1000)

      return () => clearInterval(interval)
    })
  },
})
</script>

在这个例子中,我们使用了onMounted()钩子,在组件挂载后,每隔一秒钟添加一条随机的消息到消息列表。

3.2 移除消息

我们可以使用v-for来遍历消息列表,并通过点击事件来移除指定的消息:

<template>
  <div class="max-h-96 overflow-auto pt-3">
    <div v-for="(message, index) in messages" :key="index">
      <button
        class="px-2 py-1 text-white bg-red-500"
        @click="removeMessage(index)"
      >
        X
      </button>
      <MessageNotification :message="message" />
    </div>
  </div>
</template>

<script>
import { defineComponent, computed } from 'vue'
import MessageNotification from '@/components/MessageNotification.vue'

export default defineComponent({
  name: 'MessageList',
  components: {
    MessageNotification,
  },
  computed: {
    messages() {
      return this.$store.getters.getMessages
    },
  },
  methods: {
    removeMessage(index) {
      this.$store.dispatch('message/removeMessage', index)
    },
  },
})
</script>

在这个例子中,我们在每条消息前添加了一个删除按钮,当点击按钮时,会触发removeMessage()方法,来移除指定的消息。

4. 总结

本文详细介绍了如何利用vue3实现仿苹果系统侧边消息提示效果。我们通过使用vue3的Teleport和Composition API等特性,来创建组件和定义状态,并展示了两个实例,说明如何使用这个组件。希望这篇文章能够帮助你更好地使用vue3。

本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:利用vue3仿苹果系统侧边消息提示效果实例 - Python技术站

(0)
上一篇 2023年5月28日
下一篇 2023年5月28日

相关文章

  • 详解Vue生命周期的示例

    首先,我们需要了解Vue生命周期的概念。Vue实例有一个完整的生命周期,从开始创建、初始化数据、编译模板、挂载DOM、渲染、更新、销毁等一系列过程,我们称之为Vue的生命周期。 为了更全面地了解Vue生命周期,我们推荐先查看官方文档,并结合以下示例进行学习。 示例一:理解钩子函数的执行时机 Vue生命周期中有一些钩子函数,可以在不同阶段触发相关的行为,比如c…

    Vue 2023年5月29日
    00
  • vue集成kindeditor富文本的实现示例代码

    下面详细讲解一下“Vue集成KindEditor富文本的实现示例代码”的完整攻略: 1. 安装KindEditor 首先,我们需要在项目中安装KindEditor,可以通过以下命令进行安装: npm install @xdhuxc/kindeditor –save 2. 在main.js中引入和配置KindEditor 在main.js文件中引入KindE…

    Vue 2023年5月27日
    00
  • Vue实现时间轴效果

    下面是“Vue实现时间轴效果”的完整攻略。 简介 时间轴是一种用于展示时间进程的方式,通常由一条水平线和上面分布的时间事件点组成。在网站设计中,时间轴常用于展示历史时间线、流程进程等内容。本次攻略将介绍如何使用Vue来实现基础的时间轴效果。 示例代码 首先我们来看一个简单的示例代码,它展示了一个简单的时间轴效果。 <template> <d…

    Vue 2023年5月28日
    00
  • 聊聊jenkins部署vue/react项目的问题

    下面我将为你详细讲解如何在Jenkins上进行Vue/React项目的部署,具体攻略如下: 环境准备 安装 Jenkins:根据所部署服务器的操作系统选择相应安装方式,安装 Jenkins 服务。 安装 Node.js:本文以 Node.js 为 JavaScript 运行环境。 安装 NPM:NPM 是 Node.js 的包管理工具,可以用它来引入项目依赖…

    Vue 2023年5月28日
    00
  • Vue-cli框架实现计时器应用

    Vue-cli是一款能够快速搭建Vue项目的脚手架工具。在这篇攻略中,我们将详细讲解如何通过Vue-cli框架实现一个计时器应用。 1. 创建Vue项目 首先,我们需要通过Vue-cli创建一个新的Vue项目。打开命令行工具,执行以下命令: vue create timer-app 其中timer-app为项目名称。执行上述命令后,Vue-cli会自动下载所…

    Vue 2023年5月29日
    00
  • Vue 2.0中生命周期与钩子函数的一些理解

    Vue 2.0中生命周期与钩子函数的一些理解 Vue.js是一个非常受欢迎的渐进式JavaScript框架,其中生命周期和钩子函数是Vue.js的重要组成部分。 什么是生命周期? 生命周期是指Vue.js实例从创建到销毁的整个过程中所经过的各种阶段。这个过程可以被称为Vue的“生命周期”。 Vue 2.0中的生命周期可以分为8个阶段,分别是: beforeC…

    Vue 2023年5月28日
    00
  • vue3.0 vue.config.js 配置基础的路径问题

    配置Vue.js项目的构建后的静态文件的发布路径是非常必要的。在Vue.js 3.0中,配置静态资源的发布路径的方式有所改变。本文将提供Vue.js 3.0中的vue.config.js配置多个基础路径的示例说明。 创建Vue.js 3.0项目 使用Vue CLI 3创建Vue.js 3.0项目: $ vue create my-project vue.co…

    Vue 2023年5月28日
    00
  • Vue.js之VNode的使用

    下面就为大家详细讲解如何使用Vue.js中的VNode。 1. 什么是VNode VNode是Vue.js中的一种虚拟节点,它是Vue.js中的一个核心概念。 在Vue.js中,每个组件的视图分为模板(template)和渲染函数(render function)两种实现方式。而VNode就是在渲染函数中构建的虚拟DOM节点。 与真实的DOM节点不同,VNo…

    Vue 2023年5月28日
    00
合作推广
合作推广
分享本页
返回顶部