在变量中按名称添加动态异步组件

发布于 2025-01-30 10:42:24 字数 1378 浏览 0 评论 0原文

de Idea不是为了编程名称,而是将名称作为属性传递。

因此,我可以拥有一个基本的VUE组件,并且通过给出名称和位置,我告诉该vue组件在其中加载了什么。 当我键入字符串(设置名称)时,哪个可以完美,但是当我将名称放入字符串中时,它说明找不到模块。

因此,如果i import('./ yyy.vue')它有效,但是。但是,如果i 导入(var_that_is_the_name)说找不到。我认为这与Laravel混合或弦乐有关。

   <template>
    <div class="xxx" ref="xxx">
            <component :is="dynamicComponent"></component>
            <div class="right">
                right {{ this.DvueLocation }}
            </div>
    </div>
   </template>
   <script>

   import { defineAsyncComponent } from '@vue/runtime-core'

   export default {
    name: 'xxx',
    components: {

    },
    props: {
        DvueLocation: {
            type: String,
            default: "./yyy.vue",
        },
        DvueName: {
             type: String,
             default: "yyy"
        }
    },
    data: function () {
        return {
            isMounted: false,
        }

    },
    computed: {
        dynamicComponent() {
            console.log(this.DvueLocation);
            return defineAsyncComponent(() => import('./yyy.vue'))
       return defineAsyncComponent(() => import(this.DvueLocation))
        }

    },
}
</script>

yyy.vue是打印yyy的简单组件。

第一行起作用,其次不冷,请//保存。 我尝试将“'” +围绕它添加更多,

而如果我输入它,则完全相同。 字符串打印和浏览器的控制台都给出正确的值

De idea is not to program the name, but to pass the name as a property.

So i can have a base vue component and i tell what vue component to load in there dynamicly by giving name and location.
Which works perfect when i type the string (set the name) but not when i put the name in a string then it says module not found.

so if I import('./yyy.vue') it works but if. but if I import(var_that_is_the_name) it says not found. i think it has something to do with laravel mix or stringparsing.

   <template>
    <div class="xxx" ref="xxx">
            <component :is="dynamicComponent"></component>
            <div class="right">
                right {{ this.DvueLocation }}
            </div>
    </div>
   </template>
   <script>

   import { defineAsyncComponent } from '@vue/runtime-core'

   export default {
    name: 'xxx',
    components: {

    },
    props: {
        DvueLocation: {
            type: String,
            default: "./yyy.vue",
        },
        DvueName: {
             type: String,
             default: "yyy"
        }
    },
    data: function () {
        return {
            isMounted: false,
        }

    },
    computed: {
        dynamicComponent() {
            console.log(this.DvueLocation);
            return defineAsyncComponent(() => import('./yyy.vue'))
       return defineAsyncComponent(() => import(this.DvueLocation))
        }

    },
}
</script>

the yyy.vue is a simple component that prints yyy.

first line works, second with not cold not add // for it and save.
i tried putting "'" + around it en lots more

while its exactly the same if i type it.
both the string printed and on console of browser gives the right value

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

素年丶 2025-02-06 10:42:24

与字符串解析有关。

return defineAsyncComponent(() => import('./' + this.DvueName))

现在对我有用!

Something to do with string parsing.

return defineAsyncComponent(() => import('./' + this.DvueName))

This works for me now!

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