Table of Contents
{ title || "" }
Home Web Front-end JS Tutorial riot.js learning [2] mixin

riot.js learning [2] mixin

Jan 16, 2017 pm 04:01 PM

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>
Copy after login

The running effect is as follows:

riot.js learning [2] mixin

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"
});
Copy after login

Use in custom tags:

[code]this.mixin("defaultData"); // 现在this拥有了author和email属性了
Copy after login


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)!



Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Tips for using mixins to implement CRUD (add, delete, modify, check) operations in Vue Tips for using mixins to implement CRUD (add, delete, modify, check) operations in Vue Jun 25, 2023 pm 07:42 PM

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

Tips on using mixin, extend, component and other APIs to implement component customization in Vue Tips on using mixin, extend, component and other APIs to implement component customization in Vue Jun 25, 2023 pm 03:28 PM

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 Using mixins in Vue to improve application code reusability and performance Jul 18, 2023 am 11:13 AM

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 implement component code reuse in Vue How to use mixins to implement component code reuse in Vue Jun 11, 2023 pm 12:30 PM

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

Tips for using mixins in Vue to reuse components such as lists, tables, forms, etc. Tips for using mixins in Vue to reuse components such as lists, tables, forms, etc. Jun 25, 2023 am 08:36 AM

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

What is the difference between mixin and component in vue What is the difference between mixin and component in vue Dec 13, 2022 pm 06:34 PM

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.

Mixin usage and application scenarios in Vue Mixin usage and application scenarios in Vue Jun 11, 2023 pm 12:32 PM

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

Tips on using mixin, slot, scoped CSS and other technologies to achieve high degree of component customization in Vue Tips on using mixin, slot, scoped CSS and other technologies to achieve high degree of component customization in Vue Jun 25, 2023 am 11:45 AM

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.

See all articles