模版字符串拼接点击事件在vue项目中不生效问题

下面的点击事件没有任何效果,但是如果换成onclick绑定事件则会提示没有该方法。主要原因是:

模版字符串中拼接的html片段中的方法调不到vue中this.methods里的东西,因为methods里的代码是编译后在浏览器里运行的,这个时候拼接的方法绑定的click 被当成字符串解析了。

 但是问题来了,模版字符串中onclick的点击事件,无法调用vue的methods中的方法,需要按照原生方式来写,所以就需要将goRuleCreat方法绑定到全局对象window上

  mounted() {
    window.goRuleCreat = this.goRuleCreat
  },



 methods:{

    errTipsFun(error) {
      let index = error.message.lastIndexOf('_')
      let msg = error.message.substring(0, index)
      let ruleId = error.message.substring(index + 1, error.message.length)
      this.tipRuleId = ruleId.toString()
      console.log(this.tipRuleId)
      return this.$confirm(
        '<p>当前规则与以下规则冲突,无法设置</p><p style="color:#333;font-weight:bold;text-algin:center">' +
          `${msg}` +
          '<span  onclick="goRuleCreat()" style=" color: var(--theme, #137dee);margin:10px;cursor:pointer">前往调整</span></p>',
        '规则冲突提醒',
        {
          showConfirmButton: false,
          showCancelButton: false,
          type: 'warning',
          iconClass: 'el-icon-message-solid',
          dangerouslyUseHTMLString: true,
        }
      ).then(() => {})
    },

    goRuleCreat() {
      this.$router.push({ path: `/rule/create?id=${this.tipRuleId}&isEdit=2` })
    },

}

以上代码还有一个问题,就是这个confirm弹窗在点击规则前往调整到时候,怎么关闭弹窗。

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值