动态添加dom元素,并绑定vue事件

背景:后管系统配置一个产品后,前端(vue.js框架)取到这些产品信息并展示出来,产品经理要求在文本内容中添加链接。

例如:
本保险不承保前往处于战争状态或已被宣布为紧急状态的国家或地区,最新信息以登陆<a href="javascript:void(0)" style="color: rgb(55, 105, 252);">http://baoxian.pingan.com/dangerous_zone/war.shtml</a>的查询结果为准。

如果上面内容中是简单的链接跳转,用v-html把上面段内容展示出来即可。

问题关键是,不仅仅是简单的链接,还要有一些逻辑判断,所以要用“动态添加dom元素,并绑定vue事件”

解决办法一:

因为VUE是先编译,后执行,所以元素上的方法要预先绑定。
这是我们定义的VUE组件,放在最外层:

var periodDiv = Vue.extend({
        template: "<div @click='toLink($event);' class='class-item' data-url='http://baoxian.pingan.com/dangerous_zone/war.shtml'>" +
            "Available" +
            "</div>",
        methods: {
            toLink: function ($event) {
                var urlStr = $event.target.getAttribute("data-url").trim();
               // 一些复杂的逻辑判断
                myApp.doLink(urlStr); // 调用myApp的方法
            }
        }
    });

var myApp= new Vue({
        el: "#app",
        data: {
            book: null
        },
        mounted () {
          var component = new periodDiv().$mount();  // 每次添加需要重新new一个
          var $dom = $(component.$el);  // 获取动态元素的jquery对象
        },
        methods: {
            doLink: function (url) {
                openPdf(url);
            },
            openPdf () {
let attributes = e.target.getAttributeNames()
        if (attributes.includes('pdf-title') && attributes.includes('pdf-url')) {
          common.openPDF({title: e.target.getAttribute('pdf-title').trim(), file: e.target.getAttribute('pdf-url').trim()})
        }
          }
        }
    });

这个方法感觉有点麻烦,后面又找到一个超级简单的方法,

解决办法二:

var myApp= new Vue({
        el: "#app",
        data: {
            book: null
        },
        mounted () {
          this.$nextTick(() => {
            // 动态添加dom元素,并绑定VUE事件(打开pdf)(.pdf-item为动态添加的元素,放置在父元素.pdf-body下)
             $('.pdf-body').on('click', '.pdf-item', this.openPdf)
          }
        },
        methods: {
            openPdf () {
let attributes = e.target.getAttributeNames()
        if (attributes.includes('pdf-title') && attributes.includes('pdf-url')) {
          common.openPDF({title: e.target.getAttribute('pdf-title').trim(), file: e.target.getAttribute('pdf-url').trim()})
        }
            }
        }

这对产品内容配置有一定要求:
<span class="pdf-body">本保险不承保前往处于战争状态或已被宣布为紧急状态的国家或地区,最新信息以登陆<span class="pdf-item" pdf-title="xxx" pdf-url="http://baoxian.pingan.com/dangerous_zone/war.shtml" style="color: rgb(55, 105, 252);">战争状态地区</span>的查询结果为准。</span>

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • UI组件 element- 饿了么出品的Vue2的web UI工具套件 Vux- 基于Vue和WeUI的组件库 m...
    柴东啊阅读 15,903评论 2 140
  • UI组件 element- 饿了么出品的Vue2的web UI工具套件 Vux- 基于Vue和WeUI的组件库 m...
    随行者pgl阅读 8,687评论 0 15
  • UI组件 element- 饿了么出品的Vue2的web UI工具套件 Vux- 基于Vue和WeUI的组件库 m...
    流觞小菜鸟阅读 5,823评论 2 8
  • UI组件 element- 饿了么出品的Vue2的web UI工具套件 Vux- 基于Vue和WeUI的组件库 m...
    王喂马_阅读 11,524评论 1 77
  • UI组件 element- 饿了么出品的Vue2的web UI工具套件 Vux- 基于Vue和WeUI的组件库 m...
    你猜_3214阅读 13,825评论 0 118