riot.js learning [2] mixin
Mixin Introduction
In riot.js, there is a very important concept, which is mixin. As the name suggests, its approximate function is "mixing".
Mix the properties and methods of the object into the current context. The common understanding is that it is the this object.
Look at a "chestnut":
[code]<!Doctype html> <html> <head> <meta charset="utf-8" /> <title>Riot.js测试02, Mixin</title> <script type="text/javascript" src="riot.js"></script> <script type="text/javascript" src="compiler.js"></script> </head> <body> <todo title="da宗熊"></todo> </body> <script type="riot/tag"> <todo> <h1 id="nbsp-title-nbsp-nbsp-nbsp">{ title || "" }</h1> <p>年龄: { this.getAge() } </p> <p>身高:{ height }cm</p> // 这里可以省略script标签 this.title = opts.title || ""; // 调用mixin,mixin拿到的,是window.mixinObj // 也可以混合多个 this.mixin(a, b...); this.mixin(mixinObj); </todo> </script> <script type="text/javascript"> var mixinObj = { age: 10, height: 180, getAge: function(){ return this.age; } }; riot.mount("todo"); </script> </html>
The running effect is as follows:
You see, through this.mixin(mixinObj); window The properties and methods of .mixinObj are all reflected in this.
Note: mixin only shallowly copies the object, so when multiple custom tags share the mixin object, be careful of mutual influence
Declarative mixin
Parameters of mixin, Not just objects, but also strings. But when using strings, you must register a mixin in riot in advance.
Registration method:
[code]// 如果要跨项目共享 mixin,可以考虑在riot里注册一个,而不是使用window级对象 riot.mixin("defaultData", { author: "da宗熊", email: "1071093121@qq.com" });
Use in custom tags:
[code]this.mixin("defaultData"); // 现在this拥有了author和email属性了
Small pitfalls encountered
Pay attention to the number sequence of mixin. The following attributes will overwrite the previous attributes.The attributes of mixin will even overwrite the attributes of this.
Do not overwrite them. The properties and methods that come with riot.js, such as: opts, update, on, off, trigger, etc.
The above is the content of riot.js learning [2] mixin, please pay attention to more related content PHP Chinese website (www.php.cn)!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Mixin in Vue is a very useful feature. It can encapsulate some reusable code in a mixin object, and then use mixin to introduce it in the components that need to use these codes. This method greatly improves the reusability and maintainability of the code, especially in some repeated CRUD (add, delete, modify) operations. This article will introduce how to use mixins to implement CRUD operations in Vue. First, we need to understand how to create a

Vue.js is a popular front-end framework that provides many APIs for component customization. This article will introduce the mixin, extend, component and other APIs in Vue to help you master the skills of component customization. Mixin Mixin is a way to reuse component code in Vue. It allows us to reuse already written code into different components, thereby reducing the need to write duplicate code. For example, we can use mixins to help us combine multiple groups

Using mixins in Vue to improve application code reusability and performance Introduction: As the complexity of front-end applications continues to increase, code reusability and performance optimization have become the focus of developers. As a popular JavaScript framework, Vue provides the function of using mixins to help us simplify the code and improve performance. This article will introduce what mixins are and how to use mixins in Vue to improve application code reusability and performance. 1. What is a mixin? Mixin in programming

How to use mixins to achieve component code reuse in Vue As applications become more and more complex, we need more componentization and code reuse to improve development efficiency. In Vue, mixin is a very simple and very useful tool that can help us reuse component code. Mixins are a mixin-like concept that allow the same code to be shared between multiple components. In Vue, we can think of a mixin as an object that contains some reusable properties and methods that can be used by multiple

Vue is a popular JavaScript framework with many powerful features and tools for building modern, efficient web applications. One of them is mixins. Mixin is an advanced mechanism in Vue. It allows us to extract reusable functional parts from components so that these functions can be effectively reused. This is very useful when we develop common components such as lists, tables, and forms. it works. The working principle of Mxin mixin can be understood as an object

The difference between mixin and component: After the component is referenced, it is equivalent to opening up a separate space in the parent component to perform corresponding operations based on the values from the parent component's props. In essence, the two are still distinct and relatively independent; while mixins are in After the component is introduced, various attribute methods equivalent to the parent component have been expanded, and the internal content of the component, such as data and other methods, method and other attributes, will be merged with the corresponding content of the parent component.

With the continuous development of front-end technology, Vue has become one of the preferred frameworks for many front-end developers. In Vue, mixin is a very important and practical function. By using mixins, we can extract some commonly used logic codes, encapsulate them into an object, and then reuse them in components, thus greatly improving the reusability and maintainability of the code. 1. The use of mixin In Vue, we can create a mixin by defining an object. In this object, we can

Vue is a very flexible and powerful front-end framework. It has some very important but less common technologies, such as mixin, slot and scopedCSS. These technologies can not only help us build components better, but also enable high customization of components. and reuse. This article will introduce in detail how to use these technologies to achieve high customization of components in Vue. 1. Using mixinMixin is a way to reuse and share code in Vue. It can mix some reusable code in components.
