美酒没故事° 2024-02-01 17:12 采纳率: 37.8%
浏览 4

naiveUI dialog content render 怎么实现inpput的数据绑定啊?

这的数据不能删也不能改,而且autofocus自动焦点也没对上。我想在框里输入然后能提交


// 打开添加、修改字典
  editData(data) {
      console.log(data)
      let title, inputValue
      if (data.codeType) {
        title = '修改字典'
        inputValue = data.comment
      } else {
        title = '添加字典'
      }
      const that = this
      $dialog.create({
        title: title,
        showIcon: false,
        content: () => {
          return [
            h(NInput, {
              value: inputValue,
              modelValue: inputValue, // 使用 modelValue 进行双向绑定
              clearable: true,
              autofocus: true,
              'onUpdate:modelValue': (val) => {
                console.log(val)
                inputValue = val
              },
            }),
          ]
        },
        positiveText: '提交',
        negativeText: '关闭',
        onPositiveClick: () => {
          console.log(inputValue)
        },
      })
}

img

naiveUI 有像elementUI那样的js弹框吗?不用html的

img

  • 写回答

4条回答 默认 最新

  • 专家-赤兔[在线] 全栈领域优质创作者 2024-02-01 17:15
    关注

    引自免费微信小程序:皆我百晓生

    在 naiveUI 中,要实现 Dialog 内容区域中 Input 组件的数据绑定,并确保 autofocus 属性生效以及数据提交,您可以按照以下方式调整代码:

    // 首先,在 Vue 实例的 data 属性中定义一个用于双向绑定的变量
    data() {
      return {
        dialogInputValue: '',
        // ... 其他数据项
      };
    },
    
    // 打开添加、修改字典的方法中
    editData(data) {
      console.log(data);
      let title = '';
      if (data.codeType) {
        title = '修改字典';
        this.dialogInputValue = data.comment; // 设置默认值
      } else {
        title = '添加字典';
      }
    
      const that = this;
      $dialog.create({
        title,
        showIcon: false,
        content: () => [
          h(NInput, {
            modelValue: that.dialogInputValue, // 使用 modelValue 进行双向绑定
            clearable: true,
            autofocus: true,
            'onUpdate:modelValue': (val) => { // 监听输入值变化
              that.dialogInputValue = val;
            },
          }),
        ],
        positiveText: '提交',
        negativeText: '关闭',
        onPositiveClick: () => {
          // 在这里处理提交逻辑,比如将 dialogInputValue 提交到服务器或更新到现有数据
          console.log('提交的数据:', that.dialogInputValue);
          // 根据实际情况编写提交函数
          // this.submitDictionary(that.dialogInputValue);
        },
      });
    },
    

    通过上述代码,您已经实现了:

    1. Input 组件与 Vue 实例中的 dialogInputValue 的双向绑定。
    2. Input 组件开启 autofocus,使其自动获取焦点。
    3. 当点击“提交”按钮时,调用 onPositiveClick 回调函数,此时可以通过 dialogInputValue 获取用户输入的内容并进行后续操作。

    naiveUI 本身提供了类似的 Dialog 组件和 Input 组件,可以满足类似于 ElementUI 的弹框功能。不过请注意,实际项目中需要根据 naiveUI 的版本和文档来使用正确的属性名称和事件绑定方式,以上示例代码是基于 naiveUI v2.x 版本编写的。对于最新版,请查阅官方文档:naiveUI 官方文档

    评论

报告相同问题?

问题事件

  • 修改了问题 2月1日
  • 创建了问题 2月1日

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题