【问题标题】:Nuxt/Vue.js - Dynamically loading in a child component based on a propNuxt/Vue.js - 基于道具动态加载子组件
【发布时间】:2020-03-07 16:53:03
【问题描述】:

我有一个sectionHeader.vue 组件,我想在许多不同的页面上使用它。在sectionHeader.vue 内部是一个<canvas> 元素(我用于一些健壮的three.js 动画),我希望通过道具动态更改每个标题内部。我想让这项工作利用 Vue 固有的代码拆分优势,这样每个页面就不会加载每个其他页面的动画 js 代码,只加载与之相关的代码。

我正在尝试使用 dynamic components 动态加载它,但我认为这不是我要寻找的...

我的文件夹结构:

components
  animations
    robust-animation-1.vue
    robust-animation-2.vue
    maybe-like-15-more-of-these.vue
  headings
    heading2.vue
  SectionHeader.vue
pages
  index.vue

pages/index.vue:

<sectionHeader post-title="Menu" class-name="menu" canvas="./animations/robust-animation-1"></sectionHeader>

组件/SectionHeader.vue:

<template>
    <section class="intro section-intro" :class="className">
        <heading2 :post-title="postTitle"></heading2>
        <component v-bind:is="canvas"></component> <!-- this is what i am dynamically trying to load --> 
        {{canvas}} <!-- this echos out ./animations/robust-animation-1 -->
    </section>
</template>

<script>
import heading2 from './headings/heading2';

export default {
    components: {
        heading2
    },
    props: [
        'postTitle', 'className', 'canvas'
    ]
}
</script>

组件/动画/robust-animation-1.vue:

<template>
    <div>
        <canvas class="prowork-canvas" width="960" height="960"></canvas>
    </div>
</template>

<script>
// 200 lines of js here that manipulates the above canvas
</script>

我的heading2 组件运行良好,但无论我尝试什么,我都无法弄清楚如何动态拉入我的动画组件。我收到不同程度的以下错误:

client.js 76 DOMException: Failed to execute 'createElement' on 'Document': The tag name provided ('./animations/robust-animation-1') is not a valid name.

我查看了作用域插槽,这看起来接近我需要的,但不完全是。有没有更好的方法来做我正在尝试的事情?

【问题讨论】:

标签: javascript vue.js vuejs2 nuxt.js


【解决方案1】:

我已成功完成基于prop 的子组件的动态加载。请参考以下代码。

components
  animations
    robust-animation-1.vue
    robust-animation-2.vue
    maybe-like-15-more-of-these.vue
  headings
    heading2.vue
  SectionHeader.vue
pages
  index.vue

vuepages/index.vue:

  <section-header post-title="Menu" class-name="menu" canvas="./animations/robust-animation-1">
  </section-header>
  <script>
    import SectionHeader from "../components/SectionHeader";

    export default {
        name: 'PageIndex',
        components: {SectionHeader}
    }
  </script>

组件/SectionHeader.vue:

<template>
  <div>
    post_title {{postTitle}}
    <br/>
    class-name {{className}}
    <br/>
    canvas {{canvas}}
    <br/>
    <heading2 post-title="postTitle"></heading2>
    <br/>
    <component v-bind:is="stepComponent"></component>
  </div>
</template>

<script>
    import Heading2 from "./headings/heading2";

    export default {
        name: "SectionHeader",
        components: {
            Heading2,
        },
        props: ['postTitle', 'className', 'canvas'],
        computed: {
            stepComponent() {
                let data = this.canvas.split('/')[2];
                return () => import(`./animations/${data}`);
            }
        },
    }
</script>

作为 SectionHeader.vue 组件的一部分,您缺少最后一个 computed 步骤

组件/动画/robust-animation-1.vue

<template>
  <div>
    From - robust-animation-1
  </div>
</template>

<script>
    export default {
        name: "robust-animation-1"
    }
</script>

输出:

post_title Menu 
class-name menu 
canvas ./animations/robust-animation-1 
postTitle 
From - robust-animation-1

【讨论】:

    猜你喜欢
    • 2021-12-21
    • 1970-01-01
    • 1970-01-01
    • 2020-12-09
    • 2020-03-31
    • 1970-01-01
    • 2021-09-09
    • 1970-01-01
    • 2019-07-15
    相关资源
    最近更新 更多