search
HomeWeb Front-endJS TutorialShare example tutorials of vue2.X components

Share example tutorials of vue2.X components

Jul 18, 2017 pm 05:43 PM
Experiencecomponents

Focus: Communication between parent and child components

Look at the picture and talk:

Pass Props

  • The child component itself is isolated from the parent component. It receives the parent component data through the declared props attribute displayed in the child component;

  • When the data of the parent component is updated , the props of the subcomponent will be updated accordingly;

  • This data flow is one-way (watching);

Emit Events

  • The child component uses $.emit(fn) to throw out its own internally triggered events;

  • Does the parent component listen? If the parent component needs to listen, use v-on to bind the listener and trigger the corresponding event;

The above is a common language, detailed analysis

  • The subcomponent can receive a string, and {{label}} can be used inside the subcomponent

<v-input></v-input>
  • Subcomponent can receive dynamic parameters

<input><v-profile></v-profile>

What should I do if the subcomponent accidentally changes the data after receiving it and wants to process it?

  • Create a copy of the prop (deep copy is recommended), process the copy, and leave the prop unchanged;

After the data of the parent component changes, the child The component's prop will be updated automatically, but the copy of this prop will not?

  • Use watch to monitor this prop and update the copy when it changes;

What should I do if I want to notify the parent component if the prop copy of the child component changes?

  • Use watch to monitor this copy and throw out its own internally triggered events when it changes;

. . .

Actually the above? ? ? There is a better method in 2.3, just take a look at the previous one.

.sync modifier

***父组件***
<input>
<v-profile></v-profile>
***子组件***$.emit('update:message',newValue)

I used it and was pleased to see that the modification was successful, but when I opened the console, an error was reported! ! !

vue.esm.js?65d7:434 [Vue warn]: Avoid mutating a prop directly since the value will be overwritten whenever the parent component re-renders. Instead, use a data or computed property based on the prop's value. Prop being mutated: "message"

If the child component wants to trigger the parent component, it can emit (the parent component needs to listen to trigger). What about the parent component triggering the child component event?

  • By using the ref attribute on the referenced child component, the parent component calls the child component's methods and attributes

But! $refs is only populated after the component is rendered, and it is non-responsive. It is only intended as a workaround for direct access to child components - using $refs in templates or computed properties should be avoided.

Focus: Communication between non-parent-child components

Use an empty vue instance as the central event bus

var bus = new Vue();// 触发组件 A 中的事件bus.$emit('id-selected', 1)// 在组件 B 创建的钩子中监听事件bus.$on('id-selected', function (id) {  // ...})

Consider vuex

Concern: Using slots in components

First of all, it is invalid to place content in the middle of the child component label in the parent component. Then the slot appears.

Vernacular version:

<span style="color: #000000">匿名slot:
    slot标签存在与子组件template中;
    子组件在父组件中使用的时候,子组件标签中的结构会在编译后替换子组件的slot标签;<br>    如果子组件中没有slot,则父组件中子组件标签中的内容会消失;
具名slot:
    顾名思义,是具有name属性的slot标签;并有匿名组件的特性(以上);
    子组件在父组件中使用的时候,子组件中的结构中会有某些标签拥有slot属性并赋值,这些会在编译后替换子组件的相应slot标签;</span>

One sentence explanation: The main content is written in the sub-component tag in the parent component, and is inserted into the corresponding position of the sub-component after compilation

To be honest, when it comes to this, I don’t even understand why I want to slot.

Official explanation entrance

The official gave an example of layout:

##
<div>
  <header><slot></slot>
  </header>
  <main><slot></slot>
  </main>
  <footer><slot></slot>
  </footer>
</div>
View Code
<app-layout>
  <h1 id="这里可能是一个页面标题">这里可能是一个页面标题</h1>
  <p>主要内容的一个段落。</p>
  <p>另一个主要段落。</p>
  <p>这里有一些联系信息</p>
</app-layout>
View Code

<br>

Focus: Dynamic component usage

  • By using a reserved

    element and dynamically binding to its is attribute, we allow multiple components to use the same A mount point and dynamic switching: very suitable for creating Tab-like effects

<component>
  <!-- 组件在 vm.currentview 变化时改变! -->
</component>
  • 在methods属性中定义一个函数修改currentView即可。

  • 视情况可以配合 keep-alive 避免重新渲染

  • 在子组件上放置activate钩子做切换时的工作:done() //放到钩子最后,表示执行工作完毕,可以切换组件,配合keep-alive使用,activate钩子只执行一次

  • 子组件接收数据和以往相同,只是这一次都写在了component中,只是如此的话,每个子组件都需要有这些接口(prop)

暂时说到这里,突然得回头看一下react,没时间了,回头会继续。

以上的满基础的(我是新手),有什么不对的求指出,感谢!!!

<br>

 

The above is the detailed content of Share example tutorials of vue2.X components. For more information, please follow other related articles on the PHP Chinese website!

Statement
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
JavaScript Engines: Comparing ImplementationsJavaScript Engines: Comparing ImplementationsApr 13, 2025 am 12:05 AM

Different JavaScript engines have different effects when parsing and executing JavaScript code, because the implementation principles and optimization strategies of each engine differ. 1. Lexical analysis: convert source code into lexical unit. 2. Grammar analysis: Generate an abstract syntax tree. 3. Optimization and compilation: Generate machine code through the JIT compiler. 4. Execute: Run the machine code. V8 engine optimizes through instant compilation and hidden class, SpiderMonkey uses a type inference system, resulting in different performance performance on the same code.

Beyond the Browser: JavaScript in the Real WorldBeyond the Browser: JavaScript in the Real WorldApr 12, 2025 am 12:06 AM

JavaScript's applications in the real world include server-side programming, mobile application development and Internet of Things control: 1. Server-side programming is realized through Node.js, suitable for high concurrent request processing. 2. Mobile application development is carried out through ReactNative and supports cross-platform deployment. 3. Used for IoT device control through Johnny-Five library, suitable for hardware interaction.

Building a Multi-Tenant SaaS Application with Next.js (Backend Integration)Building a Multi-Tenant SaaS Application with Next.js (Backend Integration)Apr 11, 2025 am 08:23 AM

I built a functional multi-tenant SaaS application (an EdTech app) with your everyday tech tool and you can do the same. First, what’s a multi-tenant SaaS application? Multi-tenant SaaS applications let you serve multiple customers from a sing

How to Build a Multi-Tenant SaaS Application with Next.js (Frontend Integration)How to Build a Multi-Tenant SaaS Application with Next.js (Frontend Integration)Apr 11, 2025 am 08:22 AM

This article demonstrates frontend integration with a backend secured by Permit, building a functional EdTech SaaS application using Next.js. The frontend fetches user permissions to control UI visibility and ensures API requests adhere to role-base

JavaScript: Exploring the Versatility of a Web LanguageJavaScript: Exploring the Versatility of a Web LanguageApr 11, 2025 am 12:01 AM

JavaScript is the core language of modern web development and is widely used for its diversity and flexibility. 1) Front-end development: build dynamic web pages and single-page applications through DOM operations and modern frameworks (such as React, Vue.js, Angular). 2) Server-side development: Node.js uses a non-blocking I/O model to handle high concurrency and real-time applications. 3) Mobile and desktop application development: cross-platform development is realized through ReactNative and Electron to improve development efficiency.

The Evolution of JavaScript: Current Trends and Future ProspectsThe Evolution of JavaScript: Current Trends and Future ProspectsApr 10, 2025 am 09:33 AM

The latest trends in JavaScript include the rise of TypeScript, the popularity of modern frameworks and libraries, and the application of WebAssembly. Future prospects cover more powerful type systems, the development of server-side JavaScript, the expansion of artificial intelligence and machine learning, and the potential of IoT and edge computing.

Demystifying JavaScript: What It Does and Why It MattersDemystifying JavaScript: What It Does and Why It MattersApr 09, 2025 am 12:07 AM

JavaScript is the cornerstone of modern web development, and its main functions include event-driven programming, dynamic content generation and asynchronous programming. 1) Event-driven programming allows web pages to change dynamically according to user operations. 2) Dynamic content generation allows page content to be adjusted according to conditions. 3) Asynchronous programming ensures that the user interface is not blocked. JavaScript is widely used in web interaction, single-page application and server-side development, greatly improving the flexibility of user experience and cross-platform development.

Is Python or JavaScript better?Is Python or JavaScript better?Apr 06, 2025 am 12:14 AM

Python is more suitable for data science and machine learning, while JavaScript is more suitable for front-end and full-stack development. 1. Python is known for its concise syntax and rich library ecosystem, and is suitable for data analysis and web development. 2. JavaScript is the core of front-end development. Node.js supports server-side programming and is suitable for full-stack development.

See all articles

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft