Ember Octane vs Vue 2 Comparison

Reactivity

Declare state

Ember Octane

name.hbs

<h1>Hello {{this.name}}</h1>

Vue 2

Name.vue

<script>
export default {
  data() {
    return {
      name: "John",
    };
  },
};
</script>

<template>
  <h1>Hello {{ name }}</h1>
</template>

Update state

Ember Octane

name.hbs

<h1>Hello {{this.name}}</h1>

Vue 2

Name.vue

<script>
export default {
  data() {
    return {
      name: "John",
    };
  },
  created() {
    this.name = "Jane";
  },
};
</script>

<template>
  <h1>Hello {{ name }}</h1>
</template>

Computed state

Ember Octane

double-count.hbs

<div>{{this.doubleCount}}</div>

Vue 2

DoubleCount.vue

<script>
export default {
  data() {
    return {
      count: 10,
    };
  },
  computed: {
    doubleCount() {
      return this.count * 2;
    },
  },
};
</script>

<template>
  <div>{{ doubleCount }}</div>
</template>

Templating

Minimal template

Ember Octane

hello-world.hbs

<h1>Hello world</h1>

Vue 2

HelloWorld.vue

<template>
  <h1>Hello world</h1>
</template>

Styling

Ember Octane

css-style.css

/* using: https://github.com/salsify/ember-css-modules */
.title {
  color: red;
}

Vue 2

CssStyle.vue

<template>
  <div>
    <h1 class="title">I am red</h1>
    <button style="font-size: 10rem">I am a button</button>
  </div>
</template>

<style scoped>
.title {
  color: red;
}
</style>

Loop

Ember Octane

colors.hbs

<ul>
  {{#each (array "red" "green" "blue") as |color|}}
    <li>{{color}}</li>
  {{/each}}
</ul>

Vue 2

Colors.vue

<script>
export default {
  data() {
    return {
      colors: ["red", "green", "blue"],
    };
  },
};
</script>

<template>
  <ul>
    <li v-for="color in colors" :key="color">
      {{ color }}
    </li>
  </ul>
</template>

Event click

Ember Octane

counter.hbs

<p>Counter: {{this.count}}</p>
<button {{on "click" this.incrementCount}}>+1</button>

Vue 2

Counter.vue

<script>
export default {
  data() {
    return {
      count: 0,
    };
  },
  methods: {
    incrementCount() {
      this.count++;
    },
  },
};
</script>

<template>
  <div>
    <p>Counter: {{ count }}</p>
    <button @click="incrementCount">+1</button>
  </div>
</template>

Dom ref

Ember Octane

input-focused.hbs

<input {{this.autofocus}} />

Vue 2

InputFocused.vue

<script>
export default {
  mounted() {
    this.$refs.inputElement.focus();
  },
};
</script>

<template>
  <input ref="inputElement" />
</template>

Conditional

Ember Octane

traffic-light.hbs

<button {{on "click" this.nextLight}}>Next light</button>
<p>Light is: {{this.light}}</p>
<p>
  You must
  {{#if (eq this.light "red")}}
    STOP
  {{else if (eq this.light "orange")}}
    SLOW DOWN
  {{else if (eq this.light "green")}}
    GO
  {{/if}}
</p>

Vue 2

TrafficLight.vue

<script>
export default {
  data() {
    return {
      TRAFFIC_LIGHTS: ["red", "orange", "green"],
      lightIndex: 0,
    };
  },
  computed: {
    light() {
      return this.TRAFFIC_LIGHTS[this.lightIndex];
    },
  },
  methods: {
    nextLight() {
      this.lightIndex = (this.lightIndex + 1) % this.TRAFFIC_LIGHTS.length;
    },
  },
};
</script>

<template>
  <div>
    <button @click="nextLight">Next light</button>
    <p>Light is: {{ light }}</p>
    <p>
      You must
      <span v-if="light === 'red'">STOP</span>
      <span v-else-if="light === 'orange'">SLOW DOWN</span>
      <span v-else-if="light === 'green'">GO</span>
    </p>
  </div>
</template>

Lifecycle

On mount

Ember Octane

page-title.hbs

<p>Page title is: {{(this.pageTitle)}}</p>

Vue 2

PageTitle.vue

<script>
export default {
  data() {
    return {
      pageTitle: "",
    };
  },
  mounted() {
    this.pageTitle = document.title;
  },
};
</script>

<template>
  <p>Page title: {{ pageTitle }}</p>
</template>

On unmount

Ember Octane

time.hbs

<p>Current time: {{this.time}}</p>

Vue 2

Time.vue

<script>
export default {
  data() {
    return {
      time: new Date().toLocaleTimeString(),
      timer: null,
    };
  },
  mounted() {
    this.timer = setInterval(() => {
      this.time = new Date().toLocaleTimeString();
    }, 1000);
  },
  beforeDestroy() {
    clearInterval(this.timer);
  },
};
</script>

<template>
  <p>Current time: {{ time }}</p>
</template>

Component composition

Props

Ember Octane

app.hbs

<UserProfile
  @name="John"
  @age={{20}}
  @favouriteColors={{array "green" "blue" "red"}}
  @isAvailable={{true}}
/>

Vue 2

App.vue

<script>
import UserProfile from "./UserProfile.vue";

export default {
  components: {
    UserProfile,
  },
};
</script>

<template>
  <UserProfile
    name="John"
    :age="20"
    :favorite-colors="['green', 'blue', 'red']"
    is-available
  />
</template>

Emit to parent

Ember Octane

app.hbs

<p>Are you happy?</p>
<AnswerButton @onYes={{this.handleYes}} @onNo={{this.handleNo}} />
<p style="font-size: 50px;">{{if this.isHappy "😀" "😥"}}</p>

Vue 2

App.vue

<script>
import AnswerButton from "./AnswerButton.vue";
export default {
  components: {
    AnswerButton,
  },
  data() {
    return {
      isHappy: true,
    };
  },
  methods: {
    onAnswerNo() {
      this.isHappy = false;
    },
    onAnswerYes() {
      this.isHappy = true;
    },
  },
};
</script>

<template>
  <div>
    <p>Are you happy?</p>
    <AnswerButton @yes="onAnswerYes" @no="onAnswerNo" />
    <p style="font-size: 50px">
      {{ isHappy ? "😀" : "😥" }}
    </p>
  </div>
</template>

Slot

Ember Octane

app.hbs

<FunnyButton> Click me! </FunnyButton>

Vue 2

App.vue

<script>
import FunnyButton from "./FunnyButton.vue";
export default {
  components: {
    FunnyButton,
  },
};
</script>

<template>
  <FunnyButton> Click me! </FunnyButton>
</template>

Slot fallback

Ember Octane

app.hbs

<FunnyButton />
<FunnyButton>I got content!</FunnyButton>

Vue 2

App.vue

<script>
import FunnyButton from "./FunnyButton.vue";
export default {
  components: {
    FunnyButton,
  },
};
</script>

<template>
  <div>
    <FunnyButton />
    <FunnyButton> I got content! </FunnyButton>
  </div>
</template>

Context

Ember Octane

app.hbs

<UserProfile />

Vue 2

App.vue

<script>
import UserProfile from "./UserProfile.vue";

export default {
  components: { UserProfile },
  provide() {
    return {
      user: Object.assign(this.user, {
        updateUsername: this.updateUsername,
      }),
    };
  },
  data() {
    return {
      user: {
        id: 1,
        username: "unicorn42",
        email: "unicorn42@example.com",
      },
    };
  },
  methods: {
    updateUsername(newUsername) {
      this.user.username = newUsername;
    },
  },
};
</script>

<template>
  <div>
    <h1>Welcome back, {{ user.username }}</h1>
    <UserProfile />
  </div>
</template>

Form input

Input text

Ember Octane

input-hello.hbs

<p>{{this.text}}</p>
<input value={{this.text}} {{on "input" this.handleInput}} />

Vue 2

InputHello.vue

<script>
export default {
  data() {
    return {
      text: "Hello World",
    };
  },
};
</script>

<template>
  <div>
    <p>{{ text }}</p>
    <input v-model="text" />
  </div>
</template>

Checkbox

Ember Octane

is-available.hbs

<input
  id="is-available"
  type="checkbox"
  checked={{this.isAvailable}}
  {{on "change" this.handleChange}}
/>
<label for="is-available">Is available</label>

Vue 2

IsAvailable.vue

<script>
export default {
  data() {
    return {
      isAvailable: true,
    };
  },
};
</script>

<template>
  <div>
    <input id="is-available" v-model="isAvailable" type="checkbox" />
    <label for="is-available">Is available</label>
  </div>
</template>

Radio

Ember Octane

pick-pill.hbs

<div>Picked: {{this.picked}}</div>

<input
  id="blue-pill"
  type="radio"
  value="blue"
  checked={{eq this.picked "blue"}}
  {{on "change" this.handleChange}}
/>
<label htmlFor="blue-pill">Blue pill</label>

<input
  id="red-pill"
  type="radio"
  value="red"
  checked={{eq this.picked "red"}}
  {{on "change" this.handleChange}}
/>
<label htmlFor="red-pill">Red pill</label>

Vue 2

PickPill.vue

<script>
export default {
  data() {
    return {
      picked: "red",
    };
  },
};
</script>

<template>
  <div>
    <div>Picked: {{ picked }}</div>

    <input id="blue-pill" v-model="picked" type="radio" value="blue" />
    <label for="blue-pill">Blue pill</label>

    <input id="red-pill" v-model="picked" type="radio" value="red" />
    <label for="red-pill">Red pill</label>
  </div>
</template>

Select

Ember Octane

color-select.hbs

<select {{on "change" this.select}}>
  {{#each this.colors as |color|}}
    <option
      value={{color.id}}
      disabled={{color.isDisabled}}
      selected={{eq color.id this.selectedColorId}}
    >
      {{color.text}}
    </option>
  {{/each}}
</select>

Vue 2

ColorSelect.vue

<script>
export default {
  data() {
    return {
      selectedColorId: 2,
      colors: [
        { id: 1, text: "red" },
        { id: 2, text: "blue" },
        { id: 3, text: "green" },
        { id: 4, text: "gray", isDisabled: true },
      ],
    };
  },
};
</script>

<template>
  <select v-model="selectedColorId">
    <option
      v-for="color in colors"
      :key="color.id"
      :value="color.id"
      :disabled="color.isDisabled"
    >
      {{ color.text }}
    </option>
  </select>
</template>

Webapp features

Render app

Ember Octane

index.html

<!DOCTYPE html>
<html>
  <body>
    <div id="app"></div>
    <script type="module" src="./main.js"></script>
  </body>
</html>

Vue 2

index.html

<!DOCTYPE html>
<html>
  <body>
    <div id="app"></div>
    <script type="module" src="./main.js"></script>
  </body>
</html>

Fetch data

Ember Octane

app.hbs

{{#let (this.fetchUsers) as |request|}}
  {{#if request.isLoading}}

    <p>Fetching users...</p>

  {{else if request.error}}

    <p>An error occurred while fetching users</p>

  {{else}}

    <ul>
      {{#each request.users as |user|}}
        <li>
          <img src={{user.picture.thumbnail}} alt="user" />
          <p>{{user.name.first}} {{user.name.last}}</p>
        </li>
      {{/each}}
    </ul>

  {{/if}}
{{/let}}

Vue 2

App.vue

<script>
export default {
  data() {
    return {
      isLoading: false,
      error: undefined,
      users: undefined,
    };
  },
  mounted() {
    this.fetchData();
  },
  methods: {
    async fetchData() {
      this.isLoading = true;
      try {
        const response = await fetch("https://randomuser.me/api/?results=3");
        const { results: users } = await response.json();
        this.users = users;
        this.error = undefined;
      } catch (error) {
        this.error = error;
      } finally {
        this.users = undefined;
        this.isLoading = false;
      }
    },
  },
};
</script>

<template>
  <p v-if="isLoading">Fetching users...</p>
  <p v-else-if="error">An error ocurred while fetching users</p>
  <ul v-else-if="users">
    <li v-for="user in users" :key="user.login.uuid">
      <img :src="user.picture.thumbnail" alt="user" />
      <p>
        {{ user.name.first }}
        {{ user.name.last }}
      </p>
    </li>
  </ul>
</template>






Decouvrez plus d’Offres de la plateform ItGalaxy.io :

Découvrez notre gamme complète de services et formations pour accélérer votre carrière.

1. Nous contactez

  • Description: Besoin de Formation et des Solutions cloud complètes pour vos applications
  • Links:

2. Infra as a Service

  • Description: Infrastructure cloud évolutive et sécurisée
  • Links:

3. Projets Développeurs


4. Développeurs


5. Formations Complètes


6. Marketplace

7. Blogs


This website is powered by ItGalaxy.io