032_Notification通知

本文详细介绍了Element UI的Notification通知组件,包括其参数、方法、全局方法以及单独引用的使用方式。内容涵盖基本用法、不同类型的提示、自定义位置、偏移量设置、使用HTML片段和隐藏关闭按钮等多个方面,并提供了多个代码示例,帮助开发者理解和应用Notification组件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. Notification通知

1.1. Notification通知, 悬浮出现在页面角落, 显示全局的通知提醒消息。

1.2. Options

参数

说明

类型

可选值

默认值

title

标题

string

message

说明文字

string/Vue.VNode

dangerouslyUseHTMLString

是否将message属性作为HTML片段处理

boolean

false

type

主题样式, 如果不在可选值内将被忽略

string

success/warning/info/error

iconClass

自定义图标的类名。若设置了type, 则iconClass会被覆盖

string

customClass

自定义类名

string

duration

显示时间, 毫秒。设为0则不会自动关闭

number

4500

position

自定义弹出位置

string

top-right

showClose

是否显示关闭按钮

boolean

true

onClose

关闭时的回调函数

function

onClick

点击Notification时的回调函数

function

offset

偏移的距离, 在同一时刻, 所有的Notification实例应当具有一个相同的偏移量

number

0

1.3. 方法

方法名

说明

close

关闭当前的Notification

2. Notification通知全局方法

2.1. 如果你完整引入了Element, 它会为Vue.prototype添加如下全局方法: $notify。因此在vue instance中可以采用本页面中的方式调用Notification。

this.$notify(options)
this.$notify.info(options)
this.$notify.success(options)
this.$notify.warning(options)
this.$notify.error(options)

3. Notification通知单独引用

3.1. 单独引入Notification:

import { Notification } from 'element-ui';

Notification(options);
Notification.info(options);
Notification.success(options);
Notification.warning(options);
Notification.error(options);

3.2. 可以调用Notification.closeAll()手动关闭所有实例。

4. Notification通知例子

4.1. 使用脚手架新建一个名为element-ui-messagebox的前端项目, 同时安装Element插件。

4.2. 编辑index.js 

import Vue from 'vue'
import VueRouter from 'vue-router'
import Notification from '../components/Notification.vue'
import TypeNotification from '../components/TypeNotification.vue'
import PositionNotification from '../components/PositionNotification.vue'
import OffsetNotification from '../components/OffsetNotification.vue'
import HtmlNotification from '../components/HtmlNotification.vue'
import CloseNotification from '../components/CloseNotification.vue'

Vue.use(VueRouter)

const routes = [
  { path: '/', redirect: '/Notification' },
  { path: '/Notification', component: Notification },
  { path: '/TypeNotification', component: TypeNotification },
  { path: '/PositionNotification', component: PositionNotification },
  { path: '/OffsetNotification', component: OffsetNotification },
  { path: '/HtmlNotification', component: HtmlNotification },
  { path: '/CloseNotification', component: CloseNotification }
]

const router = new VueRouter({
  routes
})

export default router

4.3. 在components下创建Notification.vue

<template>
  <div>
    <h1>基本用法-适用性广泛的通知栏</h1>
    <h4>Notification组件提供通知功能, Element注册了$notify方法, 接收一个options字面量参数, 在最简单的情况下, 你可以设置title字段和message字段, 用于设置通知的标题和正文。默认情况下, 经过一段时间后Notification组件会自动关闭, 但是通过设置duration, 可以控制关闭的时间间隔, 特别的是, 如果设置为0, 则不会自动关闭。注意: duration接收一个Number, 单位为毫秒, 默认为4500。</h4>
    <el-button plain @click="open1">可自动关闭</el-button>
    <el-button plain @click="open2">不会自动关闭</el-button>
  </div>
</template>

<script>
export default {
  methods: {
    open1 () {
      const h = this.$createElement

      this.$notify({
        title: '标题名称',
        message: h('i', { style: 'color: teal' }, '这是提示文案')
      })
    },
    open2 () {
      this.$notify({
        title: '提示',
        message: '这是一条不会自动关闭的消息',
        duration: 0
      })
    }
  }
}
</script>

4.4. 在components下创建TypeNotification.vue

<template>
  <div>
    <h1>带有倾向性</h1>
    <h4>Element为Notification组件准备了四种通知类型: success, warning, info, error。通过type字段来设置, 除此以外的值将被忽略。同时, 我们也为Notification的各种type注册了方法, 可以在不传入type字段的情况直接调用。</h4>
    <el-button plain @click="open1">成功</el-button>
    <el-button plain @click="open2">警告</el-button>
    <el-button plain @click="open3">消息</el-button>
    <el-button plain @click="open4">错误</el-button>
  </div>
</template>

<script>
export default {
  methods: {
    open1 () {
      this.$notify({
        title: '成功',
        message: '这是一条成功的提示消息',
        type: 'success'
      })
    },
    open2 () {
      this.$notify({
        title: '警告',
        message: '这是一条警告的提示消息',
        type: 'warning'
      })
    },
    open3 () {
      this.$notify.info({
        title: '消息',
        message: '这是一条消息的提示消息'
      })
    },
    open4 () {
      this.$notify.error({
        title: '错误',
        message: '这是一条错误的提示消息'
      })
    }
  }
}
</script>

4.5. 在components下创建PositionNotification.vue

<template>
  <div>
    <h1>自定义弹出位置</h1>
    <h4>使用position属性定义Notification的弹出位置, 支持四个选项: top-right、top-left、bottom-right、bottom-left, 默认为top-right。</h4>
    <el-button plain @click="open1">右上角</el-button>
    <el-button plain @click="open2">右下角</el-button>
    <el-button plain @click="open3">左下角</el-button>
    <el-button plain @click="open4">左上角</el-button>
  </div>
</template>

<script>
export default {
  methods: {
    open1 () {
      this.$notify({
        title: '自定义位置',
        message: '右上角弹出的消息'
      })
    },
    open2 () {
      this.$notify({
        title: '自定义位置',
        message: '右下角弹出的消息',
        position: 'bottom-right'
      })
    },
    open3 () {
      this.$notify({
        title: '自定义位置',
        message: '左下角弹出的消息',
        position: 'bottom-left'
      })
    },
    open4 () {
      this.$notify({
        title: '自定义位置',
        message: '左上角弹出的消息',
        position: 'top-left'
      })
    }
  }
}
</script>

4.6. 在components下创建OffsetNotification.vue

<template>
  <div>
    <h1>带有偏移</h1>
    <h4>Notification提供设置偏移量的功能, 通过设置offset字段, 可以使弹出的消息距屏幕边缘偏移一段距离。注意在同一时刻, 所有的Notification实例应当具有一个相同的偏移量。</h4>
    <el-button plain @click="open">偏移的消息</el-button>
  </div>
</template>

<script>
export default {
  methods: {
    open () {
      this.$notify({
        title: '偏移',
        message: '这是一条带有偏移的提示消息',
        offset: 100
      })
    }
  }
}
</script>

4.7. 在components下创建HtmlNotification.vue

<template>
  <div>
    <h1>使用HTML片段</h1>
    <h4>将dangerouslyUseHTMLString属性设置为true, message就会被当作HTML片段处理。</h4>
    <el-button plain @click="open">使用 HTML 片段</el-button>
  </div>
</template>

<script>
export default {
  methods: {
    open () {
      this.$notify({
        title: 'HTML 片段',
        dangerouslyUseHTMLString: true,
        message: '<strong>这是 <i>HTML</i> 片段</strong>'
      })
    }
  }
}
</script>

4.8. 在components下创建CloseNotification.vue

<template>
  <div>
    <h1>隐藏关闭按钮</h1>
    <h4>将showClose属性设置为false即可隐藏关闭按钮。</h4>
     <el-button plain @click="open">隐藏关闭按钮</el-button>
  </div>
</template>

<script>
export default {
  methods: {
    open () {
      this.$notify.success({
        title: 'Info',
        message: '这是一条没有关闭按钮的消息',
        showClose: false
      })
    }
  }
}
</script>

4.9. 运行项目, 访问http://localhost:8080/#/Notification

4.10. 运行项目, 访问http://localhost:8080/#/TypeNotification 

4.11. 运行项目, 访问http://localhost:8080/#/PositionNotification 

4.12. 运行项目, 访问http://localhost:8080/#/OffsetNotification

4.13. 运行项目, 访问http://localhost:8080/#/HtmlNotification 

4.14. 运行项目, 访问http://localhost:8080/#/CloseNotification 

<think>好的,用户现在问的是`taskNOT_WAITING_NOTIFICATION`这个宏。我需要先回忆一下之前和用户对话的内容。之前用户问了`taskYIELD_IF_USING_PREEMPTION`,我详细解释了它的定义、行为、应用场景、实现和注意事项。现在用户可能在使用RTOS,特别是FreeRTOS,遇到了任务通知相关的问题。 首先,我需要确认`taskNOT_WAITING_NOTIFICATION`在FreeRTOS中的定义和作用。根据记忆,任务通知是FreeRTOS中的一个功能,允许任务之间直接发送事件或数据,而无需使用队列、信号量等中间对象。每个任务有一个通知值,可以用于同步。 接下来,`taskNOT_WAITING_NOTIFICATION`应该是一个状态宏,表示任务当前不在等待通知的状态。通常在任务调用`ulTaskNotifyTake`或`xTaskNotifyWait`时,会进入等待状态,等待通知的到来。如果任务没有调用这些函数,或者在调用时设置了不阻塞的参数,那么任务的状态就不会是等待通知,此时可能需要检查这个状态。 用户可能是在调试时遇到了任务没有按预期接收通知的问题,或者想了解如何正确使用任务通知机制。他们可能需要知道如何判断任务是否在等待通知,以便在适当的时候发送通知,避免通知被错过或无法唤醒任务。 我需要解释这个宏的用途、使用场景、与相关API的关系,以及注意事项。比如,这个宏可能用于在发送通知前检查任务是否在等待,避免不必要的操作。同时,需要注意多线程环境下的竞争条件,可能需要结合禁用中断或使用临界区来确保状态检查的原子性。 另外,用户可能对FreeRTOS的任务通知机制不太熟悉,所以需要简要介绍一下任务通知的基本概念,但避免过于深入,保持回答的针对性。还要比较`taskNOT_WAITING_NOTIFICATION`和其他相关宏或状态的区别,比如`taskWAITING_NOTIFICATION`,帮助用户理解何时使用哪个宏。 最后,确保回答结构清晰,分点说明,并给出代码示例,这样用户能更好地理解如何在实际应用中使用这个宏。还要提醒用户注意潜在的问题,比如状态检查的时机和并发问题,确保他们不会在应用中遇到意外的行为。</think>以下是关于`taskNOT_WAITING_NOTIFICATION`的详细解析: --- ### **基本定义** `taskNOT_WAITING_NOTIFICATION`是FreeRTOS中用于标识**任务未处于等待通知状态**的宏。其核心用途是: - 判断任务当前是否在等待接收通知(通过`xTaskNotifyWait()`或`ulTaskNotifyTake()`等API) - 常与`xTaskNotifyState()`配合使用,获取任务通知的当前状态 --- ### **行为机制** 1. **任务通知状态分类** FreeRTOS为每个任务维护通知状态,通过以下宏定义: ```c #define taskNOT_WAITING_NOTIFICATION 0 // 未等待通知 #define taskWAITING_NOTIFICATION 1 // 阻塞等待通知 #define taskNOTIFICATION_RECEIVED 2 // 已收到通知但未处理 ``` 2. **典型应用场景** ```c eNotifyState eState = xTaskNotifyState(xTaskHandle); if(eState == taskNOT_WAITING_NOTIFICATION) { // 任务未在等待,可选择其他同步方式 } else { xTaskNotify(...); // 安全发送通知 } ``` --- ### **底层实现原理** 1. **状态跟踪机制** FreeRTOS在任务控制块(TCB)中维护`ucNotifyState`字段: ```c typedef struct tskTaskControlBlock { uint8_t ucNotifyState; // 通知状态标志 uint32_t ulNotifiedValue; // 通知值 // ...其他字段 } TCB_t; ``` 2. **状态转换逻辑** | 操作 | 状态变化 | |---|---| | 调用`xTaskNotifyWait()` | → `taskWAITING_NOTIFICATION` | | 收到通知但未处理 | → `taskNOTIFICATION_RECEIVED` | | 处理完通知 | → `taskNOT_WAITING_NOTIFICATION` | --- ### **使用注意事项** 1. **多线程竞争条件** 检查状态和发送通知之间需保证原子性,推荐模式: ```c taskENTER_CRITICAL(); if(xTaskNotifyState(xTask) != taskNOT_WAITING_NOTIFICATION) { xTaskNotify(...); } taskEXIT_CRITICAL(); ``` 2. **性能优化** - 在频繁发送通知的场景中,先检查状态可避免无效操作 - 对于时间敏感的实时任务,可结合`uxTaskResetNotification()`重置状态 3. **常见误用案例** ```c // 错误:未保护状态检查 if(xTaskNotifyState() == taskWAITING_NOTIFICATION) { // 此处可能被中断打断,导致实际发送时状态已改变 xTaskNotify(...); } ``` --- ### **对比其他同步机制** | 特性 | 任务通知 | 队列 | 事件组 | |---|---|---|---| | 内存占用 | 0额外内存 | 需预分配缓冲区 | 固定32位 | | 延迟 | 直接写入TCB | 需要队列操作 | 位操作 | | 状态跟踪 | 通过`taskNOT_WAITING_NOTIFICATION` | 无内置状态标志 | 通过事件位 | --- ### **最佳实践建议** 1. 在需要轻量级同步时优先使用任务通知 2. 对关键状态检查需配合临界区使用 3. 在任务删除前调用`vTaskNotifyDeleteFromISR()`清理状态 如需进一步分析具体使用场景中的问题,可提供代码片段继续讨论。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值