ask*_*660 15 javascript transclusion vue.js
vue.js是否具有Angular *ngTemplateOutlet
指令的等效项?假设我有一些这样定义的组件:
<template>
<div id="independentComponent">
Hello, {{firstName}}!
</div>
</template>
<script>
export default {
name: "independentComponent",
props: ['firstName']
}
</script>
...
<template>
<div id="someChildComponent">
<slot></slot>
<span>Let's get started.</span>
</div>
</template>
<script>
export default {
name: "someChildComponent"
}
</script>
Run Code Online (Sandbox Code Playgroud)
我希望能够做这样的事情:
<template>
<div id="parentComponent">
<template #indepdentInstance>
<independentComponent :firstName="firstName" />
</template>
<someChildComponent>
<template #indepdentInstance></template>
</someChildComponent>
</div>
</template>
<script>
export default {
name: "parentComponent",
components: {
someChildComponent,
independentComponent
},
data() {
return {
firstName: "Bob"
}
}
}
</script>
Run Code Online (Sandbox Code Playgroud)
在Angular中,我可以使用
<div id="parentComponent">
<someChildComponent>
<ng-container *ngTemplateOutlet="independentInstance"></ng-container>
</someChildComponent>
<ng-template #independentInstance>
<independentComponent [firstName]="firstName"></independentComponent>
</ng-template>
</div>
Run Code Online (Sandbox Code Playgroud)
但是看起来Vue要求将元素确切地写入到DOM在模板中的位置。有什么方法可以内联引用元素并将其作为插槽传递给另一个组件?
cha*_*ila -3
不太确定我是否理解您的问题,但如果我想在一个模板中添加两个组件,我会尝试为您提供一些我会选择做的事情。
标题部分.vue
<template>
<div id="header_id" :style="'background:'+my_color">
welcome to my blog
</div>
</template>
<script>
export default {
props: ['my_color']
}
</script>
Run Code Online (Sandbox Code Playgroud)
BodySection.vue
<template>
<div id="body_id">
body section here
</div>
</template>
<script>
export default {
}
</script>
Run Code Online (Sandbox Code Playgroud)
主页.vue
<template>
<div id="parentComponent">
<header-section :color="my_color" />
<body-section />
</div>
</template>
<script>
import HeaderSection from "./components/HeaderSection.vue"
import BodySection from "./components/BodySection.vue"
export default {
name: "home",
components: {
HeaderSection,
BodySection
},
data() {
return {
my_color: "red"
}
}
}
</script>
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
447 次 |
最近记录: |