Mixins are JS objects that can be used to distribute reusable code among components. When a component uses mixin, all options of mixin become a part of the component options.

- Example:
// define a mixin object
var mixin_1 = {
 //function automatically called when Vue instance is created
 created: function(){
 this.hello();
 },
 methods: {
 hello: function(){
 console.log('hello from mixin.')
 }
 }
}

// define a component that uses this mixin
var component = Vue.extend({
 mixins: [mixin_1]
})

var comp = new component() // hello from mixin.
When a mixin and a component contain overlapping options, they are merged. If they contain same function or data object with same key, data from main Vue instance takes priority and will replace mixin's function and data with same key.

- In the following example we have mixin and Vue instance with data object that have same "msg" key.
// define a mixin object
var mixin_2 = {
 //function automatically called when Vue instance is created
 data: function(){
 return {msg: 'Hello from Mixin', str:'String from mixin'};
 }
}

// vue instance
var vm = new Vue({
 mixins: [mixin_2],
 data: {msg:'Msg replaced from component.', txt:'I`m a good person'},
 created: function(){
 console.log(this.$data);
 }
})

// {"msg": "Msg replaced from component.", "txt": "I`m a good person", "str": "String from mixin"}
- In the following example we have a method (here called "same_met") with same name in mixin and Vue instance.
// define a mixin object
var mixin_3 = {
 methods: {
 met_mixin: function(){
 console.log('Just wish and vision frequently its effect')
 },
 same_met: function(){
 console.log('The past not exists')
 }
 }
}

// define a component that uses this mixin
var vm = new Vue({
 mixins: [mixin_3],
 methods: {
 same_met: function(){
 console.log('I am my life')
 },
 met_inst: function(){
 console.log('Miracle is possible')
 }
 }
})

vm.met_inst(); // Miracle is possible
vm.met_mixin(); // Just wish and vision frequently its effect
vm.same_met(); // I am my life
Hook functions with the same name are merged into an array so that all of them will be called. Mixin hooks will be called before the component's hooks.
var mixin_4 ={
 created: function(){
 console.log('mixin - The wish is the request')
 }
}

new Vue({
 mixins: [mixin_4],
 created: function () {
 console.log('component - The vision is to receive')
 }
})

// mixin - The wish is the request
// component - The vision is to receive

Daily Test with Code Example

HTML
CSS
JavaScript
PHP-MySQL
Which HTML5 tag defines marked text? (can be used to highlight parts of text)
<mark> <embed> <span>
<p>Free corses: <mark>coursesweb.net</mark> for Web Development.</p>
Which CSS pseudo-class adds a style to an element when the mouse is over it?
:focus :hover :active
a:hover {
  font-weight: bold;
  color: #00da01;
}
Click on the function which returns a string value that represents the number rounded to the x digits after the decimal point.
toPrecision(x) toFixed(x) floor(x)
var num = 12.34567;
num = num.toFixed(2);
alert(num);       // 12.35
Indicate the PHP function which reads an entire file into an array.
[) file() readfile()
$arr = file("a_file.txt", FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
var_export($arr);
Mixins

Last accessed pages

  1. Animating in Flash - Frame-by-Frame Animation (2031)
  2. JpGraph - Create Graph, Charts, Plots in PHP (1321)
  3. Contact page - CoursesWeb (21725)
  4. Tabs effect with CSS (22432)
  5. Making DIV Contents Scroll Horizontally, with multiple Div`s inside (28750)

Popular pages this month

  1. Making DIV Contents Scroll Horizontally, with multiple Div`s inside (1270)
  2. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (1241)
  3. Tabs effect with CSS (1227)
  4. Contact page - CoursesWeb (1225)
  5. Insert, Select and Update NULL value in MySQL (729)