【发布时间】:2019-02-13 02:46:14
【问题描述】:
我正在尝试在 Vue.js 中创建一个包含嵌套组件的组件。 结构可以简化为
ParentComponent.vue
|
|__ ChildComponent.vue
只有当我在 ParentComponent 中使用 @click 单击按钮时,是否可以获得 ChildComponent ?我希望组件仅在单击事件后才加载其文件,而不仅仅是显示/隐藏组件,因为我正在尝试优化网络使用并且嵌套可能会更深。
编辑: 我尝试了以下代码
<template>
<div class="home">
<button type="button" @click="isActive=!isActive">SHOW</button>
<async-component v-show="isActive"></async-component>
</div>
</template>
<script>
const AsyncComponent = () => ({
// The component to load (should be a Promise)
component: import('./components/ChildComponent.vue'),
// A component to use while the async component is loading
loading: LoadingComponent,
// A component to use if the load fails
error: ErrorComponent,
// Delay before showing the loading component. Default: 200ms.
delay: 200,
// The error component will be displayed if a timeout is
// provided and exceeded. Default: Infinity.
timeout: 3000
})
export default {
name: "home",
components: {
AsyncComponent
},
data(){
return{
isActive:false
}
}
};
</script>
但是当我单击按钮时,它会显示组件,当我在开发人员工具中检查网络选项卡时,它不会显示如果获取任何内容并且组件已经加载,它只会显示/隐藏它。
【问题讨论】:
-
你读过这个吗:Async Components?您是否尝试将其用于点击处理程序?请添加显示您尝试过的代码。
-
我编辑了帖子并添加了我正在测试的代码,我已经阅读了链接并在页面末尾使用了 sn-p。
标签: javascript vue.js