javascript - 是否有与 ngTemplateOutlet 等效的 vue.js?

标签 javascript vue.js transclusion

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>

我希望能够做这样的事情:

<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>

在 Angular 中,我可以用

<div id="parentComponent">
    <someChildComponent>
        <ng-container *ngTemplateOutlet="independentInstance"></ng-container>
    </someChildComponent>

    <ng-template #independentInstance>
        <independentComponent [firstName]="firstName"></independentComponent>
    </ng-template>
</div>

但看起来 Vue 要求将元素准确写入 DOM 中它在模板中的位置。有什么方法可以内联引用元素并使用它作为插槽传递给另一个组件?

最佳答案

您不能重复使用类似 ngTemplateOutlet 的模板, 但可以结合 $refs 的想法, v-pre和使用 v-runtime-template 编译的运行时模板为达到这个。

首先,创建可重复使用的模板(<ng-template #independentInstance>):

<div ref="independentInstance" v-show="false">
    <template v-pre> <!-- v-pre disable compiling content of template -->
        <div> <!-- We need this div, because only one root element allowed in templates -->
            <h2>Reusable template</h2>
            <input type="text" v-model="testContext.readWriteVar">
            <input type="text" v-model="readOnlyVar">
            <progress-bar></progress-bar>
        </div>
    </template>
</div>

现在,您可以重复使用 independentInstance模板:

<v-runtime-template
    :template="$refs.independentInstance.innerHTML"
    v-if="$refs.independentInstance">
</v-runtime-template>

但请记住,您不能修改 readOnlyVar从里面independentInstance template - vue 会警告你:

[Vue warn]: Avoid mutating a prop directly since the value will be overwritten whenever the parent component re-renders. Instead, use a data or computed property based on the prop's value. Prop being mutated: "readOnlyVar"

但是你可以把它包裹在object中它会起作用:

@Component({
    components: {
        VRuntimeTemplate
    }
})
export default class ObjectList extends Vue {
    reusableContext = {
        readWriteVar: '...'
    };

    readOnlyVar = '...';
}

关于javascript - 是否有与 ngTemplateOutlet 等效的 vue.js?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53090908/

相关文章:

angularjs - 元素包含

javascript - 如何将 jQuery 倒计时附加到代码并使其工作?

javascript - “被 CORS 策略阻止”,但存在 header

java - Angularjs UI Bootstrap Popover 阻止输入提交

vue.js - 带有 Vuetify 的 FontAwesome SVG 图标 - 如何在 v-icon/prepend-icon 中使用?

vue.js - 在 Vuetify 的 v-toolbar 组件上动态添加后退箭头

javascript - ng-transclude 为 : element vs attribute

javascript - ng-transclude 会停止 Angular.js 中的事件传播吗?

javascript - 在 for 循环中设置对象值,然后推送到数组会导致所有最终数组项具有相同的值

javascript - 在函数中运行代码就像在另一个作用域中运行一样