Skip to content

Vue.js

This document shows you how to create a Family Tree JS 2 Vue.js project.

  1. Create a new project with the name familytree:
bash
npm init vue@latest
  1. Go to the project root folder:
bash
cd familytree
  1. Install dependencies:
bash
npm install
  1. Download the package, extract it, and edit familytree2.js to add this line at the end:
javascript
export default FamilyTree2;
  1. Add the extracted familytree2.js file to the src/assets folder.

  2. Create FamilyTree.vue in src/components with the following content:

vue
<template>
  <div id="tree" ref="tree"></div>
</template>

<script>
import FamilyTree2 from '../assets/familytree2';

export default {
  name: 'tree',
  data() {
    return {
      nodes: [
        { id: 1, spouseIds: [2], motherId: 3, fatherId: 4 },
        { id: 2 },
        { id: 3 },
        { id: 4 }
      ]
    };
  },
  methods: {
    mytree(domEl, nodes) {
      this.familyTree = new FamilyTree2(domEl);
      this.familyTree.readOnly = false;
      this.familyTree.addFamilyMembers(nodes).draw(1);
    }
  },
  mounted() {
    this.mytree(this.$refs.tree, this.nodes);
  }
};
</script>

<style scoped>
</style>
  1. Change App.vue as follows:
vue
<template>
  <div id="familytree">
    <FamilyTree />
  </div>
</template>

<script>
import FamilyTree from './components/FamilyTree.vue';

export default {
  name: 'app',
  components: {
    FamilyTree
  }
};
</script>

<style>
#app {
  font-family: 'Avenir', Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}

html, body {
  width: 100%;
  height: 100%;
  padding: 0;
  margin: 0;
  overflow: hidden;
  font-family: Helvetica;
}

#tree {
  width: 100%;
  height: 100%;
}
</style>
  1. In main.css add this:
css
#app, #tree {
  height: 100%;
  width: 100%;
  display: block;
}
  1. Start the project:
bash
npm run dev