首页 > 编程知识 正文

父组件向子组件动态传值,vue父组件获取子组件数据

时间:2023-05-06 11:56:51 阅读:146856 作者:411

1 .父组件将值传递给子组件的@Input文件目录

父组件:

father.template.html

h1父部件/h1cmt-child [ data ]=' data '/CMT-childfather.com ponent.ts

导入{ component,OnInit } from '@angular/core '; @ component (selector : ' CMT-father ),template URL : './father.template.html ' } (exportclassfathercomponenent )

import { Component,OnInit,Input } from '@angular/core '; @ component (选择器: ' CMT-child ', template URL : './child.template.html ' } ) exportclasschildcomponent //接收父组件值的ngOnInit () console.log

2 .子组件向父组件传递值(子组件通过方法通过修饰符@output向父组件传递值)。

childcomponent.ts

import { Component,OnInit,Input,Output,event emitter } from ' @ angular/core '; @ component (选择器: ' CMT-child ', template URL : './child.template.html ' } ) exportclasschildcomponent id : any='我传递给父组件的值' ngon init ({ } }} child.template.html.html

h1部件/button (单击)='checkedCallback ) )单击以将值传递给父部件/button父部件

father.template.html

h1父部件/H1CMT-child(checked )=' checked back ($ event ) )/cmt-child father.component.ts

导入{ component,OnInit } from '@angular/core '; @ component (selector : ' CMT-father ),template URL : './father.template.html ' } (exportclassfathercomponenent )

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。