模板字符串使用点击事件【VUE】

模板字符串使用点击事件

1.在element-UI组件库中使用Message 消息提示,可能会使用到模板字符串点击事件。
代码如下:

import { Message } from 'element-ui';
<template>
  <button type="primary"  @click="getMessage">message</button>
</template>

<script>
export default {
  data () {
    return {}
  },
  methods: {
    getMessage () {
      this.$message({
        dangerouslyUseHTMLString: true,
        message: `<div style=display:flex><div>点击成功</div><button onclick = getDownUp() style=color:#0091fa;cursor:pointer;>立即前往</button></div>`,
        type: 'success',
        duration: 0
      })
    },
    getDownUp () {
      console.log('模板字符串点击事件')
    }
  },
  mounted () {
    // window全局对象上定义一个这样的方法
    window.getDownUp = this.getDownUp
  }
}
</script>

2.vue中在js中模板字符串使用点击事件
此文件为js文件

import Vue from 'vue'

Vue.prototype.$message({
    dangerouslyUseHTMLString: true,
    message: `<div style=display:flex><div>message</div><button onclick ='getDownUp()'>点击按钮</button></div>`,
    type: 'success',
})
// 此函数放在生命周期中挂载阶段或其他
window.getDownUp = function () {
   console.log('模板字符串')
}
总结:为什么需要定义window.getDownUp = this.getDownUp?

1.在字符串里的点击事件是原生的onclick
2.原生的函数getDownUp是定义在window全局对象上的
3.特别注意:触发的事件传参如果是多个参数一定要给每个参数加引号,否则会报错

getDownUp('${a[i]}','${b[i]}')

4.模板字符串中的样式书写(行内样式)

style=display:flex
  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值