如何使用<component :is="">在 vue 3 脚本设置中

编程入门 行业动态 更新时间:2024-10-09 17:27:46
本文介绍了如何使用<component :is="">在 vue 3 脚本设置中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我正在使用实验性的 脚本设置 以创建学习环境.我有一个自制的导航栏,打开单个组件.

I am using the experimental script setup to create a learn enviroment. I got a selfmade navigation bar with open a single component.

我在使用 方法.此方法在 component basics -> 下的文档中进行了描述.动态组件

I am having trouble using the <component :is="" /> method. This method is described in the docs under component basics -> dynamic-components

在 Vue 3 Composition API 中,它按预期工作:

In the Vue 3 Composition API, it works as expected:

<template>
  <NavigationBar
    @switchTab="changeTab"
    :activeTab="tab"
  />
  <component :is="tab" />
</template>

<script>
import { ref } from 'vue'
import NavigationBar from './components/NavigationBar.vue'
import TemplateSyntax from './components/TemplateSyntax.vue'
import DataPropsAndMethods from './components/DataPropsAndMethods.vue'

export default {
  components: {
    NavigationBar,
    TemplateSyntax,
    DataPropsAndMethods
  },
  setup () {
    const tab = ref('DataPropsAndMethods')
    function changeTab (newTab) {
      tab.value = newTab
    }

    return {
      changeTab,
      tab
    }
  }
}
</script>


我使用 脚本设置 的方法失败了:


My approach with the script setup fails:

<template>
  <NavigationBar
    @switchTab="changeTab"
    :activeTab="tab"
  />
  <component :is="tab" />
</template>

<script setup>
import NavigationBar from './components/NavigationBar.vue'
import TemplateSyntax from './components/TemplateSyntax.vue'
import DataPropsAndMethods from './components/DataPropsAndMethods.vue'
import { ref } from 'vue'

const tab = ref('DataPropsAndMethods')
function changeTab (newTab) {
  tab.value = newTab
}
</script>

你知道如何用脚本设置方法解决这个问题吗?

do you got any idea how to solve this with the script setup method?

推荐答案

好像用

本文发布于:2023-04-27 01:05:20,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   脚本   lt   amp   component

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!