当前位置: 代码网 > it编程>前端脚本>Vue.js > Vue使用jsmind实现生成脑图的示例代码

Vue使用jsmind实现生成脑图的示例代码

2024年05月15日 Vue.js 我要评论
项目部分参数:vue:2.6.10node:16.20.01、使用命令行安装jsmindnpm i jsmind -s2、在文件中引入jsmind,并编写渲染jsmind的代码<template

项目部分参数:

vue:2.6.10

node:16.20.0

1、使用命令行安装jsmind

npm i jsmind -s

2、在文件中引入jsmind,并编写渲染jsmind的代码

<template>
	<!-- jsmind容器 -->
    <div
      id="jsmindcontainer"
      ref="jsmindcontainer"
    ></div>
</template>

<script>
// 引入jsmind方法和样式
import "jsmind/style/jsmind.css";
import jsmind from "jsmind";

// 引入获取数据的接口
import { getprojectmind } from "@/api/projectmanagement";

export default {
  data() {
    return {
      treedata: {},
      jsmind: ""
    };
  },
  created() {
    this.initdata();
  },
  beforedestroy() {
    if (this.jsmind) {
      this.jsmind.destroy();
    }
  },
  methods: {
    initdata() {
      let projectid = this.$route.query.projectid;
      getprojectmind(projectid)
        .then(res => {
          if (res.code === 200) {
            this.treedata = res.data;
            const options = {
              container: "jsmindcontainer", // 必选,容器id
              editable: false, // 可选,是否启用编辑
              theme: "asphalt", // 可选,主题
              shortcut: {
                enable: true // 禁用快捷键
              },
              layout: {
                hspace: 50, // 节点之间的水平间距
                vspace: 20, // 节点之间的垂直间距
                pspace: 13 // 节点与连接线之间的水平间距(用于容纳节点收缩/展开控制器)
              },
              mode: "full" // 显示模式,子节点只分布在根节点右侧
            };
            // 初始化jsmind对象
            this.jsmind = new jsmind(options);

			// 让jsmind对象显示具体的脑图数据
            this.jsmind.show({
              meta: {
                name: "jsmind",
                author: "",
                version: "0.1"
              },
              format: "node_tree",// 数据格式官方文档有介绍
              data: this.treedata
            });
          }
        })
        .catch(err => {});
    }
  }
};
</script>

重启项目,报错unexpected token...,我还以为是引入jsmind的方法不对,后来我直接引入jsmind/es6/jsmind文件还是不行,也没有搜索到任何解决办法,我就尝试安装低版本的jsmind,安装了0.5.0版本的jsmind,重启项目ok了。

3、点击脑图部分节点,展示详情弹窗

在准备脑图数据的时候,我们将部分节点数据设计成了这样:

topic里面是a标签,当点击a标签的时候,会触发showtask事件,展示详情弹窗。

但是在脑图渲染出来之后,才发现在method里面注册的showtask事件,是触发不了的。后面尝试着将topic数据里面的onclick换为@click,看vue能否正常将其渲染为a标签的点击事件,结果还是不行,估计是因为jsmind使用了canvas,vue没有将canvas里面的元素变为模板标签。

怎么解决呢?我使用了window来帮忙注册事件,这样全局就有了showtask事件,点击a标签能够正常触发了。

// 因为window里面的this不是指向当前组件,所以需要提前注册使用
window.$$this = this;
window.showtask = function(id) {
  // 展示详情弹窗
  window.$$this.detailshow = true;
  // 详情弹窗获取数据
  window.$$this.$nexttick(() => {
    window.$$this.$refs.taskdetail.init(id);
  });
};

最后要注意在组件的beforedestroy生命周期中,清除数据:

beforedestroy() {
  if (this.jsmind) {
    this.jsmind.destroy();
    window.showtask = undefined;
    window.$$this = undefined;
  }
}

到此这篇关于vue使用jsmind实现生成脑图的示例代码的文章就介绍到这了,更多相关vue jsmind生成脑图内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com