What is MVVM? How does it differ from MVC?

MVC stands for Model-View-Controller. It organizes code by separating business logic, data, and user interface. Communication is one-way: the view sends instructions to the controller. After completing the business logic, the controller instructs the model to change its state. The model then sends the new data to the view, and the user receives feedback.

MVVM stands for Model-View-ViewModel. Like the MVC pattern, its primary purpose is to separate the view and model. However, the MVVM controller doesn’t listen to browser events. Instead, it listens to a property table. Browser events modify properties, triggering methods in the controller. This adds a layer of business control, called the VM. When data in the ViewModel changes, the View layer is updated. When a view declares two-way data binding (usually to form elements), the framework also monitors changes in the View layer (form). Once the value changes, the data in the ViewModel bound to the View layer is automatically updated.

New Vue is the viewmodel

How to chain filters and pass parameters?

Series

{{ message | filterA | filterB }}

Accepts parameters

{{ message | filterA(‘arg1’, arg2) }}

Here, filterA is defined as a filter function that accepts three parameters: the value of message as the first parameter, the plain string ‘arg1’ as the second parameter, and the value of the expression arg2 as the third parameter.

What is the difference between computed properties and methods? What is the difference between computed properties and listener properties?

The difference between computed properties and methods is that computed properties are cached based on dependencies and are only evaluated when the relevant dependencies change.

Listeners allow us to perform asynchronous operations (access an API) and set intermediate states (like loading…).

Computed properties have only getters by default, but you can also provide a setter if needed.

How does $watch observe instance expressions? How does it observe changes within an object? How does it trigger a callback immediately?

Observe Expression

vm.$watch(
  function () {
    return this.a + this.b
  },
  function (newVal, oldVal) {
  }
)

Changes within the object

vm.$watch(‘someObject’, callback, { deep: true }) vm.someObject.nestedValue = 123

The callback is triggered immediately.

vm.$watch(‘a’, callback, { immediate: true })

What is the difference between v-show and v-if?

v-if supports v-else and v-else-if syntax, and also supports