vue.js<组件>标签不加载导入的组件;使用构图API
我正在尝试制作一个具有少数选项卡的简单页面,这些选项卡使用<组件>
tag的动态组件。我最近了解了构图API,并想尝试一下,但这给了我一些问题。
app.vue:
<template>
<page-header :tabs="allTabs" @change-tab="changeTab"></page-header>
<component :is="activeTab"></component>
</template>
<script setup>
import { ref, reactive } from "vue";
import PageHeader from "./components/PageHeader.vue";
import MainTab from "./components/views/MainTab.vue";
import ChartTab from "./components/views/ChartTab.vue";
import FeedbackTab from "./components/views/FeedbackTab.vue";
const dummyArr = [MainTab, ChartTab, FeedbackTab];
const activeTab = ref("main-tab");
const allTabs = [
{ name: "main", selected: true },
{ name: "chart", selected: false },
{ name: "feedback", selected: false }
];
function changeTab(selection) {
let currTab = allTabs.findIndex((tab) => tab.selected === true),
nextTab = allTabs.findIndex((tab) => tab.name === selection);
allTabs[currTab].selected = false;
allTabs[nextTab].selected = true;
activeTab.value = `${selection}-tab`;
}
</script>
dummyarr
仅仅是为了防止编译器不使用Mainterab
或其他任何一个。另外,changetab
从alltabs
返回navbar选择的名称
每个导入的组件看起来像这样,用于测试目的:
<template>
<h1> Main Tab </h1>
</template>
我的问题是,当我加载Dev服务器时,标头(和内部NAV栏)呈现良好,没有错误,但是没有任何组件呈现到页面上。我一直在网上找到有关动态组件和打字稿的大量信息,但它们并没有为我的问题添加任何澄清。 (我也不使用Typescript,因此进一步混淆了事物...)
什么导致我的组件不动态加载到页面?
I am trying to make a simple page that has a handful of tabs which show different content, using dynamic components with the <component>
tag. I recently learned about Composition API and wanted to try it out, but it is giving me some problems.
App.vue:
<template>
<page-header :tabs="allTabs" @change-tab="changeTab"></page-header>
<component :is="activeTab"></component>
</template>
<script setup>
import { ref, reactive } from "vue";
import PageHeader from "./components/PageHeader.vue";
import MainTab from "./components/views/MainTab.vue";
import ChartTab from "./components/views/ChartTab.vue";
import FeedbackTab from "./components/views/FeedbackTab.vue";
const dummyArr = [MainTab, ChartTab, FeedbackTab];
const activeTab = ref("main-tab");
const allTabs = [
{ name: "main", selected: true },
{ name: "chart", selected: false },
{ name: "feedback", selected: false }
];
function changeTab(selection) {
let currTab = allTabs.findIndex((tab) => tab.selected === true),
nextTab = allTabs.findIndex((tab) => tab.name === selection);
allTabs[currTab].selected = false;
allTabs[nextTab].selected = true;
activeTab.value = `${selection}-tab`;
}
</script>
dummyArr
is simply to prevent the compiler from yelling at me for not using MainTab
or any of the other ones. Also, changeTab
returns the name
of the navbar selection from allTabs
Each of the imported components look something like this, for testing purposes:
<template>
<h1> Main Tab </h1>
</template>
My problem is that when I load my dev server, the header (and internal nav bar) render fine, and there are no errors, but none of the components render to the page. I have been finding a lot of information online about dynamic components and typescript, but they don't add any clarification to my problem. (I also don't use typescript, so that further confuses things...)
What is causing my components not to load dynamically to the page?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
动态组件应该是组件实例:
尝试将
dummyarr
转换为对象组件
,该将组件名称作为键及其实例作为值:The dynamic component should be component instances not strings:
Try to convert the
dummyArr
to an objectcomponents
that takes the component name as key and its instance as value: