This article mainly introduces the communication between parent and child components in Vue. It has certain reference value. Now I share it with you. Friends in need can refer to
Vue. js is a progressive framework for building user interfaces. Unlike other heavyweight frameworks, Vue fundamentally adopts a minimal-cost, incremental design. Vue's core library focuses solely on the view layer and is easy to integrate with other third-party libraries or existing projects. On the other hand, Vue is also perfectly capable of powering complex single-page applications when combined with single-file components and libraries supported by the Vue ecosystem.
1. Running environment construction
There are generally three ways to introduce Vue:
The first CDN introduction
<script></script>
The second type uses NPM to install
$ npm install vue//安装最新稳定版
The third type is to build scaffolding CLI
The so-called scaffolding is a development environment built through webpack, used for Quickly build large single-page applications. It can bring a durable and powerful infrastructure to modern front-end development workflow. In just a few minutes, you can have a project up and running with hot reloading, code checking on save, and a production-ready build configuration.
You must first install node.js. Node.js has built-in npm since version 0.6.3, so after node.js is installed, npm will be installed. Then operate the following command line through git bash:
$ node -v//检查是否已经装好了node $ npm -v//检查是否已经装好了npm $ npm install --global vue-cli //安装 vue-cli $ vue init webpack project//进入目标文件夹创建一个新项目 $ cd project//进入新项目 $ npm install//安装package.json中依赖的node_modules $ npm run dev//运行该项目
For mainland users, it is recommended to set the npm registry source to a domestic mirror, which can greatly improve the installation speed. This type of installation scaffolding is recommended.
npm config set registry https://registry.npm.taobao.org//配置淘宝镜像 npm config get registry//验证是否成功 npm install -g cnpm --registry=https://registry.npm.taobao.org//安装cnpm cnpm install -g vue-cli//cnpm安装脚手架 vue init webpack my-project cd my-project cnpm install cnpm run dev
Finally open http://localhost:8080, and the following page will appear, indicating that the scaffolding is completed.
2. Introduce the SRC file process and root component App
After the scaffolding is completed, each folder and file in the project is as shown below:
1.src file process introduction
index.html (entry file)=>main.js=>App.vue (root component), root component The content of the template is directly inserted into the entry file at #app, and then the page is presented.
2. Root component App introduction
It mainly consists of three parts, namely template (html structure), behavior (processing logic) and style (solving style)
3. Vue component nesting
Vue component nesting refers to how the written sub-component is associated with the root component. It can usually be divided into component global definition and component local definition. The latter is more common.
1. Component global definition
Generally the following two steps:
①main.js introduces sub-components
②template in App.vue component Call
//main.js import Vue from 'vue' import App from './App' import Users from "./components/Users";//引入子组件Users Vue.config.productionTip = false Vue.component("users",Users);//自定义名字便于App.vue组件调用,后者为组件名 new Vue({ el: '#app', components: { App }, template: '<app></app>' })rrree
2. Component local definition
Generally the following three steps:
①import introduce sub-component
②export default registration sub-component
③Add sub-components to the template
4. Parent components pass values to sub-components
Next we use an example to illustrate how the parent component transfers values to the sub-components. Subcomponent transfer value: How to get the data in the parent component App.vue in the subcomponent Users.vue users:["Henry","Bucky","Emily"]
1. Create the subcomponent, in Create a new Users.vue subcomponent under the src/components/ folder
2. Register the Users.vue component in App.vue and add the users tag to the template
//App.vue组件 <template> <p> <users></users>//在这里调用,自定义名字是小写的 </p> </template>
3.Users Create props in .vue, and then create a users attribute
//App.vue父组件 <template> <p> <users></users>//前者自定义名称便于子组件调用,后者要传递数据名 </p> </template> <script> import Users from "./components/Users" export default { name: 'App', data(){ return{ users:["Henry","Bucky","Emily"] } }, components:{ "users":Users } }</script>
5. Subcomponent passes value to parent component (through event form)
Next we use an example to illustrate the subcomponent How to pass a value to the parent component:
When we click "Vue.js Demo", the child component passes the value to the parent component, and the text changes from the original "A value is passed" to "The child passes a value to the parent component" , to realize the transfer of value from the child component to the parent component.
1. Bind a click event to the text part of the subcomponent (header component)
//users子组件 <template> <p> </p> <ul> <li>{{user}}</li>//遍历传递过来的值,然后呈现到页面 </ul> </template> <script> export default { name: 'HelloWorld', props:{ users:{ //这个就是父组件中子标签自定义名字 type:Array, required:true } } } </script>
2. The function in the subcomponent that responds to the click event Use $emit to trigger a custom event and pass a parameter
<script></script> {{title}}
//绑定一个点击事件
3. Listen to the custom event in the child tag in the parent component (App root component) and add a handler that responds to the event Method
<script> export default { name: 'app-header', data() { return { title:"Vue.js Demo" } }, methods:{ changeTitle() { this.$emit("titleChanged","子向父组件传值");//自定义事件 传递值“子向父组件传值” } } } </script>
6. Summary
In communication, whether a child component passes a value to a parent component or a parent component passes a value to a child component, they all have one thing in common. There are intermediate media. The media from the child to the parent is a custom event, and the media from the parent to the child is the attribute in props.
The above is the entire content of this article. I hope it will be helpful to everyone's study. For more related content, please pay attention to the PHP Chinese website!
related suggestion:
Use c v to return vue to the top component
The above is the detailed content of Communication between parent and child components in Vue. For more information, please follow other related articles on the PHP Chinese website!

html5的div元素默认一行不可以放两个。div是一个块级元素,一个元素会独占一行,两个div默认无法在同一行显示;但可以通过给div元素添加“display:inline;”样式,将其转为行内元素,就可以实现多个div在同一行显示了。

html5中列表和表格的区别:1、表格主要是用于显示数据的,而列表主要是用于给数据进行布局;2、表格是使用table标签配合tr、td、th等标签进行定义的,列表是利用li标签配合ol、ul等标签进行定义的。

固定方法:1、使用header标签定义文档头部内容,并添加“position:fixed;top:0;”样式让其固定不动;2、使用footer标签定义尾部内容,并添加“position: fixed;bottom: 0;”样式让其固定不动。

HTML5中画布标签是“<canvas>”。canvas标签用于图形的绘制,它只是一个矩形的图形容器,绘制图形必须通过脚本(通常是JavaScript)来完成;开发者可利用多种js方法来在canvas中绘制路径、盒、圆、字符以及添加图像等。

html5中不支持的标签有:1、acronym,用于定义首字母缩写,可用abbr替代;2、basefont,可利用css样式替代;3、applet,可用object替代;4、dir,定义目录列表,可用ul替代;5、big,定义大号文本等等。

html5废弃了dir列表标签。dir标签被用来定义目录列表,一般和li标签配合使用,在dir标签对中通过li标签来设置列表项,语法“<dir><li>列表项值</li>...</dir>”。HTML5已经不支持dir,可使用ul标签取代。

html5是指超文本标记语言(HTML)的第五次重大修改,即第5代HTML。HTML5是Web中核心语言HTML的规范,用户使用任何手段进行网页浏览时看到的内容原本都是HTML格式的,在浏览器中通过一些技术处理将其转换成为了可识别的信息。HTML5由不同的技术构成,其在互联网中得到了非常广泛的应用,提供更多增强网络应用的标准机。

3种取消方法:1、给td元素添加“border:none”无边框样式即可,语法“td{border:none}”。2、给td元素添加“border:0”样式,语法“td{border:0;}”,将td边框的宽度设置为0即可。3、给td元素添加“border:transparent”样式,语法“td{border:transparent;}”,将td边框的颜色设置为透明即可。


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

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version
Visual web development tools

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

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