All Useful Properties when Initializing Vue

Struggling to copy/paste all useful properties when initializing Vue? Looking to see what else Vue can do for you? Here’s the answer.

We have seen how Vue can be used from CDN. Initializing Vue is simple enough -

//  main.js
new Vue({
  el: "#app"
  }
});

The above code block will initialize Vue and load it to element with id app.

As we have seen previously, we can use a bunch of properties during initialization and make Vue super useful.

new Vue({
  el: "#app",
  data() {
    return {
      name: "World!"
    };
  }
});

data is used to store the static variable values / states of the component in Vue. Similar to data there are a host of other properties that you can use.

Consider the below rundown a cheat-sheet if you will.

new Vue({
  el: "#app",
  data: {
    name: "world",
    nums: Number
  }, // state
  // props: [], // input parameters
  props: {
    // input parameters for component - object
    awesomeProp: {
      type: String,
      default: "user"
    }
  },
  computed: {
    // computed values :)
    sum() {
      return 0;
    }
  },
  methods: {
    sayHello() {
      console.log("hello");
    }
  },
  watch: { a(val, oldVal) {} },

  template: "<template></template>",

  // lifecycle events
  mounted() {
    console.log("i mount, i saw, i conquered");
    this.sayHello();
  }, // every time component mounts

  created() {}, // loaded only once
  ready() {}, // first time
  attached() {},
  detached() {},
  beforeDestroy() {}, // component unload - before
  destroyed() {}, // component unload - before

  // rest of the options
  components: {}, // don't foget to include!
  filters: {},
  directives: {},
  partials: {},
  transitions: {}
});
comments powered by Disqus