【问题标题】:Unable to async load vue component in nested folder无法在嵌套文件夹中异步加载 vue 组件
【发布时间】:2019-09-28 17:00:33
【问题描述】:

我正在使用 webpack 1.13 和 Vue 2.1

说文件夹结构是

src__
     |
     page.vue
     views___
             |
             mainViews____
                          |
                          mainContainer.vue

我在 page.vue 的什么地方

<template>
    <component v-bind:is='"mainContainer"'></component>
</template>
.
.
.
beforeMount () {
    Vue.component('mainContainer', function (resolve) {
        require(['./views/mainViews/mainContainer'], resolve)
    })
}

组件未加载。

但是当被加载的组件是它加载的组件的兄弟时,它可以工作。

src__
     |
     page.vue
     mainContainer.vue


Vue.component('mainContainer', function (resolve) {
    require(['./mainContainer'], resolve)
})

控制台或构建中没有错误。我想我的问题是为什么它只在组件是文件夹结构中的兄弟时才起作用

【问题讨论】:

    标签: vue.js webpack vue-component


    【解决方案1】:

    你必须导入组件并在page.vue中注册,像这样:

    <script> import MainContainer from "maincontainer's file path"

    export default { ... components: { mainContainer: MainContainer } } </script>

    【讨论】:

    • 但这会导致组件最初被加载,而 vue.component require() 语法允许延迟加载,通过 webpack 将构建拆分为块并仅在需要时通过 ajax 调用组件,减小页面上最初加载的内容的大小。所以你所说的确实可以加载一个组件,但它并不能解释我遇到了什么
    • 啊,我明白你的意思了。造成误会请见谅
    • 不用担心 ^-^ 至少我现在有一个解决方法
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-02-13
    • 2021-05-15
    • 2018-01-23
    • 2021-12-06
    • 2019-03-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多