Traits are a mechanism that lets you create reusable code in PHP and similar languages where multiple inheritances are not supported. It’s not possible to instantiate it on its own. A trait is intended to reduce the limitations of single inheritance by enabling a developer to reuse sets of methods freely in many independent classes living in different hierarchies of class.
Vue.js is a framework for creating user interfaces that are built on JavaScript. It's a framework that's progressive. Vue.js is responsible for the view layer of an application. Vue.js may be used to create single-page applications. Vue is a framework that handles higher-level issues like fast DOM rendering, reactive state management, tools, client-side routing, and server-side rendering.
A single-file component is a file with a .vue extension that contains a Vue component. It contains the component's template, logic, and styles all bundled together in one file. It consists of one <script>
block, optional <template>
and <style>
blocks, and possible additional custom blocks.
To use one, you need to set up Vue Loader for parsing the file (usually done as part of a webpack building pipeline). But this then also supports using non-default languages such as Sass or HTML templating languages with pluggable pre-processors.
All properties defined in a Vue instance’s data option are reactive, meaning that if they change, the component is automatically updated and re-rendered as needed. All such properties are converted to getters and setters during initialization, thus allowing Vue to detect when those properties are accessed or changed. The following limitations must be accounted for when designing a Vue app:
- Vue cannot detect object property addition or deletion due to a JavaScript limitation, so the Vue.set method must be used to add new root-level reactive properties.
- Similarly, Vue cannot detect when an array item is modified using an index. Vue.set must be used here as well.
Vue uses an unobtrusive reactivity system which is one its most distinct features. But what is reactivity? Reactivity is a programming paradigm that allows us to adjust to changes in a declarative manner.
We can pass data between components in the following ways- Props: It is like an argument that is given to the component. The term used to define this is an attribute. This is registered on the component and it allows to pass of the data from the parent component to the child component. To pass data through props, we can define the prop on the child component, and then bind the data to the prop on the parent component. Vue uses a one-way data flow. Parents can pass data to child components using a prop and bind the data using the v-bind directive. When the parent component updates the prop value, it's automatically updated in the child component. You should avoid mutating the property inside the child component and it will not affect the property in the parent component (unless it's an array or object). Using Events the child component can communicate back to the parent. Vue provides the v-model directive for two-way data binding of form inputs. v-model is just syntax sugar for v-bind combined with v-on:input. But you can also implement v-model for your custom components, take a look at "Using v-model on Components" for more details.
Lifecycle hooks are functions that every Vue instance runs through. Each vue has eight lifecycle hooks. An instance of js
- Before Create - This is the first lifecycle hook that is called when a Vue instance has been created.
- Created - It's called immediately after the 'Before creates' hook, but the Vue instance has already set initial properties, data, etc.
- Before mount - invoked right before the instance is mounted on the DOM. The template has been completed at this time.
- Mounted - This is the name given to the template once it has been filled with data and is fully functional.
- Before Updated - When any changes to data require the DOM to be changed, this method is called before the update.
- Updated - It's called after the DOM has been updated.
- Before destroy - It's a location where you can clean up your resources before terminating the Vue instance.
- Destroyed - When all Vue instances have been destroyed, this method is called. When you call the destruct method on an object in code, it will be activated.
v-for, v-show, v-if, v-else, v-bind, v-model, and so on are some of the inbuilt directives present in Vue.js. v-html
The items are shown or hidden using v-show and v-if. However, there are some distinctions. The v-if directive is used to render a block conditionally. It features lazy behaviour, which means that if the initial condition is false, the block will not be rendered until it changes. During a condition change, v-if destroys and recreates the elements. When your condition does not frequently change at runtime, use the v-if directive because it has a lower initial render cost but a higher toggling cost. The v-show directive can also be used to render a block conditionally. The element is always rendered by v-show. Instead of removing the element or block from the DOM, it simply sets the CSS show attribute. It has a high initial rendering cost but a low toggle cost, so use the v-show directive if you need to toggle frequently.
v-for directive is basically an inbuilt directive, which is used to iterate over a particular array or an object
The v-model directive is used to create the two-way bindings in Vue.js.
A slot is a placeholder in a child component that is filled with content passed from the parent. Content of a regular slot is compiled in the parent’s scope and then passed to the child component. Thus you can’t use child component properties in a slot’s content. But scoped slots allow you to pass child component data to the parent scope and then use that data in slot content.
Similar to the $parent property, the $child property enables the developer to access the child instance.
These are used to indicate a reference to other HTML elements or child elements.
Computed properties should be used to remove as much logic as possible from the templates as otherwise the template gets bloated and is harder to maintain. If you have complex logic including reactive data in your template you should use a computed property instead. Instead of methods, computed properties are cached based on their reactive dependencies. A computed property will only re-evaluate when some of its reactive dependencies have changed.
Watchers are a more generic way to react to data changes instead of using computed properties. They should be used when asynchronous or expensive operations need to be executed in response to changing data.
Mixin support is a feature that allows code reuse between components in a Vue.js application and a software composition tool.
What are the different modes that Nuxt.js can be run in and why might you choose one over the other?
Nuxt.js operates in three modes: Universal, Single Page Application (SPA), and Static. Universal mode is server-side rendering (SSR) where the server pre-renders HTML files for each page. This improves SEO and initial page loading speed as users don’t have to wait for all JavaScript to be downloaded and executed.