美文网首页angular2与vue的那些事
ng2投影,父组件向子组件投影代码片段。

ng2投影,父组件向子组件投影代码片段。

作者: 阿踏 | 来源:发表于2018-04-05 15:29 被阅读0次

1、定义子组件模板 child.component.html

<h4>子组件</h4>
<!-- 使用ng-content标记子组件位置,使用select区分不同的ng-conent -->
<!-- 使用标签形式 -->
<ng-content select="header"></ng-content>
<ng-content select="content"></ng-content>
<ng-content select="footer"></ng-content>
<!-- 使用class形式 -->
<ng-conent select=".class-select"></ng-content>

2、定义子组件 child.component.ts

import {Component} from "@angular/core";
@Component({
  selector: "child",
  templateUrl: "../templates/child.component.html"
})
export class AboutComponent {
  constructor() {
    console.log("child");
  }
}

3、定义父组件 home.component.ts

import {Component} from "@angular/core";
@Component({
  selector: "my-home",
  templateUrl: "../templates/home.component.html"
})
export class HomeComponent {
  name: string = "zxc";
  constructor() {
    console.log("home");
  }
}

4、定义组件模板 home.component.html

<my-contact>
  <!--这是传递给子组件的内容-->
  <header><h4>传递过来的内容,ng-content接受, {{name}}</h4></header>
  <footer>底部</footer>
  <div class="class-select>class传递信息</div>
</my-contact>

相关文章

  • ng2投影,父组件向子组件投影代码片段。

    1、定义子组件模板 child.component.html 2、定义子组件 child.component.ts...

  • vue父子组件相互传值

    一、父组件向子组件传值 1、父组件: 2、子组件: 二、子组件向父组件传值 1、父组件 2、子组件: 详情代码地址...

  • VUE 父子组件互相传值

    父组件代码 子组件代码 父组件向子组件传值 通过v-bind: 子组件向父组件传值 通过事件绑定,比如这个@del...

  • vue中子组件向父组件传值

    子组件a.vue部分代码片段 父组件App.vue部分代码片段

  • ReactNative组件间的通信

    父组件向子组件通信 父组件向子组件传值 父组件向子组件传递方法 子组件向父组件通信 子组件向父组件传值 子组件向父...

  • React refs和onRefs的使用

    父组件代码块 子组件代码块 父组件向子组件传值主要依靠父组件中parentMethods将方法和变量传递到子组件中...

  • 一个故事讲懂vue父子组件传值

    讲故事前先讲代码 父组件向子组件传值 父组件数据传递给子组件可以通过props属性来实现父组件: 子组件: 父向子...

  • vue父子组件传值-故事讲解

    讲故事前先讲代码父组件向子组件传值父组件数据传递给子组件可以通过props属性来实现 父组件: 子组件: 父向子传...

  • vue echarts的使用

    1、子组件 commomEcharts.vue 完整代码 父组件引用 2、父组件 主要代码片段 运行结果 代码需稍...

  • Angular5 父子组件之间的通信

    一、父组件向子组件通信 父组件: 子组件: 二、子组件向父组件通信 父组件: 子组件:

网友评论

    本文标题:ng2投影,父组件向子组件投影代码片段。

    本文链接:https://www.haomeiwen.com/subject/adapfftx.html