我有一个sectionHeader.vue
组件,我想在许多不同的页面上使用。在sectionHeader.vue
里面是一个<canvas>
元素(我正在使用一些健壮的three.js动画),我希望通过props动态地在每个标头内部进行更改。我想使这项工作能够充分利用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>
components / 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>
components / animations / 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.
我查看了作用域内的插槽,它看上去与我需要的位置很接近,但不完全相同。有没有更好的方法可以做我正在尝试的事情?
答案 0 :(得分: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>
components / 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
步骤
components / animations / 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