简体   繁体   English

设置模板内的 Vue 3 动态组件

[英]Vue 3 dynamic component inside setup template

I have following problem.我有以下问题。

<script setup lang="ts">
import { RouterView } from "vue-router";
import defaultLayout from "@/layouts/default.vue";
import { useDefaultStore } from "@/stores/default";
let { getLayout } = useDefaultStore();
</script>

<template>
  <component :is="getLayout">
    <RouterView />
  </component>
</template>

I use Pinia as the store.我使用 Pinia 作为商店。 I checked getLayout its getting defaultLayout我检查了getLayout它得到defaultLayout

I know in Vue.js 2 you had to register it:我知道在 Vue.js 2 中你必须注册它:

export default {
   components: {
      defaultLayout
   }
}

How do i do it now?我现在该怎么做?

You can't descructure the store like that, you have to use Pinias storeToRefs() ( https://pinia.vuejs.org/core-concepts/#using-the-store ).你不能像那样破坏商店,你必须使用 Pinias storeToRefs() ( https://pinia.vuejs.org/core-concepts/#using-the-store )。 Maybe it has something to do with that, I can't tell without the code of the store.也许这与它有关,没有商店的代码我无法判断。

I'd recommend using this plugin https://github.com/JohnCampionJr/vite-plugin-vue-layouts if you're using Vite and need layouts.如果您使用 Vite 并需要布局,我建议您使用此插件https://github.com/JohnCampionJr/vite-plugin-vue-layouts

Use defineAsyncComponent使用defineAsyncComponent

defineAsyncComponent(() => import("@/layouts/default.vue"))

or register the component globally in your main.js :或在您的main.js中全局注册组件:

app.component('defaultLayout', defaultLayout)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM