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
isattribute, 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!

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.

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.

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

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 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 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.

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.

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.


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Atom editor mac version download
The most popular open source editor

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SublimeText3 Chinese version
Chinese version, very easy to use

WebStorm Mac version
Useful JavaScript development tools

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft