【发布时间】: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.
我查看了作用域插槽,这看起来接近我需要的,但不完全是。有没有更好的方法来做我正在尝试的事情?
【问题讨论】:
-
你能用最少的代码提供codesandbox吗?
标签: javascript vue.js vuejs2 nuxt.js