You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Vue+Storybook中动态使用PrimeVue Tooltip指令的方向修饰符

你可以直接使用PrimeVue Tooltip的对象绑定语法实现动态方向控制,无需依赖静态修饰符,修改代码如下:

核心修改点

  • 将指令的绑定值从纯文本改为配置对象,content属性对应提示文本,position属性对应动态传入的方向参数
  • 给默认参数配置默认方向值
import Button from "primevue/button"
import Tooltip from "primevue/tooltip"

export default {
  title: "Components/Tooltip",
  component: { Button },
  argTypes: {
    label: {
      control: "text",
    },
    tooltipMsg: {
      control: "text",
    },
    tooltipDirection: {
      options: ["top", "bottom", "right", "left"],
      control: { type: "select" },
    },
  },
}

const Template = (args) => ({
  components: { Button },
  setup() {
    return { args }
  },
  directives: {
    tooltip: Tooltip,
  },
  // 仅修改此处的指令绑定逻辑
  template: "<Button type='button' v-bind='args' v-tooltip='{content: args.tooltipMsg, position: args.tooltipDirection}' />",
})
export const Primary = Template.bind({})
Primary.args = {
  label: "Primary",
  tooltipMsg: "Enter ur name",
  // 新增默认方向配置
  tooltipDirection: "left"
}

修改完成后即可在Storybook控制面板中切换tooltipDirection的下拉选项,实时修改提示框的弹出方向。

内容的提问来源于stack exchange,提问作者DnD2k21

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.01 04:06:04