angular.js javascript typescript Angular学习笔记89: 父子组件内容投影进阶 - 多个投影的内容 -- used
实际开发过程的场景
在实际开发过程中,会遇到讲一个DOM 作为一个Input传给子组件,然后在子组件投影渲染出来。
子组件设置默认投影子组件设置多个投影在父组件中使用投影中的上下文内容
具体实现
创建两个组件,一个父组件ContentShadowUseComponent,一个子组件ContentShadowComponent
子组件设置默认投影
在子组件ContentShadowComponent中设置单插槽投影,可以使用
内容投影
默认内容投影
在父组件ContentShadowUseComponent使用:
默认的投影内容 - used
这种使用方式,即在父组件中使用的子组件中,所有的DOM都属于投影内容,在子组件设置全部渲染出来,这里的只是占位符,元素本身不会创建DOM
子组件从父组件传入的变量进行投影
在子组件ContentShadowComponent的类文件中定义一个变量
import {Component, Input, OnInit, TemplateRef} from '@angular/core';
@Component({
selector: 'app-content-shadow',
templateUrl: './content-shadow.component.html',
styleUrls: ['./content-shadow.component.less']
})
export class ContentShadowComponent implements OnInit {
@Input() inputContainer: TemplateRef;
constructor() {
}
ngOnInit(): void {
}
}
在子组件ContentShadowComponent的模版文件中使用此变量
@Input投影
在父组件ContentShadowUseComponent中使用子组件
默认的投影内容 - used
inputContainer的投影内容 ~~~ -- used
这个时候,DOM inputContainer的投影内容 ~~~ -- used会在子组件的处投影,多使用于按条件投影。
多个插槽的投影
在子组件ContentShadowComponent的模版文件中,给ng-content标签添加一个select属性,select属性支持签名、属性、CSS 类和 :not 伪类的任意组合。
多个插槽投影
这里是给ng-content添加了一个属性为concept,即在使用过程中,带有属性concept会投影在这个ng-content中
在父组件ContentShadowUseComponent使用
默认的投影内容 - used
多个投影的内容 -- used
在父组件中使用投影中的上下文内容
在父组件中使用投影中的默认的上下文内容
修改子组件的模版文件
内容投影
默认内容投影
多个插槽投影
@Input投影
@Input投影添加上下文
在子组件中定义变量containerContext
import {Component, Input, OnInit, TemplateRef} from '@angular/core';
@Component({ 拼多多帮助力入口
selector: 'app-content-shadow',
templateUrl: './content-shadow.component.html',
styleUrls: ['./content-shadow.component.less']
})
export class ContentShadowComponent implements OnInit {
@Input() inputContainer: TemplateRef;
@Input() inputContainerContext: TemplateRef;
public containerContext = {
$implicit: 'ContentShadow: 默认的投影上下文 --- 默认的投影上下文',
};
constructor() {
}
ngOnInit(): void {
}
}
在父组件中使用
[inputContainerContext]="inputContainerContext">
默认的投影内容 - used
多个投影的内容 -- used
inputContainer的投影内容 ~~~ -- used
带有上下文的投影,上下文为: {{content}} -- used
这里在父组件中就可以使用来自子组件的上下文内容了
在父组件中使用投影中的其他的上下文内容
修改子组件的模版文件
内容投影
默认内容投影
多个插槽投影
@Input投影
@Input投影添加上下文
@Input投影添加其他上下文
修改子组件中定义变量containerContext
import {Component, Input, OnInit, TemplateRef} from '@angular/core';
@Component({
selector: 'app-content-shadow',
templateUrl: './content-shadow.component.html',
styleUrls: ['./content-shadow.component.less']
})
export class ContentShadowComponent implements OnInit {
@Input() inputContainer: TemplateRef;
@Input() inputContainerContext: TemplateRef;
@Input() inputContainerContextOther: TemplateRef;
public containerContext = {
$implicit: 'ContentShadow: 默认的投影上下文 --- 默认的投影上下文',
otherContext: 'ContentShadow: 其他的投影上下文 -- -- -- 其他的投影上下文'
};
constructor() {
}
ngOnInit(): void {
}
}
在父组件中使用
[inputContainerContext]="inputContainerContext"
[inputContainerContextOther]="inputContainerContextOther">
默认的投影内容 - used
多个投影的内容 -- used
inputContainer的投影内容 ~~~ -- used
带有上下文的投影,上下文为: {{content}} -- used
带有上下文的投影,上下文为: {{content}} -- used
这里使用let-content="otherContext"来确定使用来自子组件上下文中的otherContext变量
示例