vue.js<组件>标签不加载导入的组件;使用构图API

发布于 2025-02-05 13:13:49 字数 1678 浏览 1 评论 0原文

我正在尝试制作一个具有少数选项卡的简单页面,这些选项卡使用<组件> 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或其他任何一个。另外,changetaballtabs 返回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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

女皇必胜 2025-02-12 13:13:49

动态组件应该是组件实例

由于组件被称为变量,而不是在字符串键下注册,因此我们应该使用Dynamic :IS在使用&lt; script; script&gt;。。。。 P>

尝试将dummyarr转换为对象组件,该将组件名称作为键及其实例作为值:

 const components= {'main':MainTab, 'chart':ChartTab,'feedback': FeedbackTab};

const activeTab = ref(MainTab);
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 = components[selection];//<-- get the component instance
}


The dynamic component should be component instances not strings:

Since components are referenced as variables instead of registered under string keys, we should use dynamic :is binding when using dynamic components inside <script setup>.

Try to convert the dummyArr to an object components that takes the component name as key and its instance as value:

 const components= {'main':MainTab, 'chart':ChartTab,'feedback': FeedbackTab};

const activeTab = ref(MainTab);
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 = components[selection];//<-- get the component instance
}


~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文