swiper.js用作幻灯片时不渲染组件
我正在添加虚拟幻灯片用swiper.js。根据示例,包含幻灯片的数组使用以下字符串(文档说幻灯片:任何,但我尝试了一个组件和其他东西,但没有其他作用):
virtual: {
// cache: true,
slides: (function() {
const slides = [];
for (let i = 0; i < 1; i += 1) {
slides.push('<img src="https://picsum.photos/600/600/?image=1/>';
}
return slides;
})()
},
问题 - 我想使用自定义组件'app-mard-card'作为幻灯片而不是字符串,因此我将其添加为如下的字符串,但是当它在屏幕上呈现时,它就不会't插值/transpile/compile/render/等组件代码或html,调试器显示我传递的字符串。
我想这样做:
slide.push('&lt; app-member-card class =“路由器链接” [memberCard] =“ member”&gt;&lt;&lt;/app-member-card&gt;');
下面的内容是我在屏幕上看到的:
<div class="card member-card">
<div class="row">...other tags left out here for verbosity</div>
</div>
问题:我可以在Angular或swiper.js中做些什么,可以将此字符串转换为组件时,将其添加为幻灯片?我在Swiper Doc中找不到任何东西,也不熟悉所有角度的东西。
我尝试的 - 我尝试在.ts文件中动态创建组件并添加属性,但是当我尝试从ViewContainerRef中提取InnerHTML作为字符串时,所有数据都是空的。 这是我指向该帖子的链接
I'm adding virtual slides to my swiper with swiper.js. The array that contains the slides, according to the examples, use strings like below (docs say slides: any, but I tried a component and other things, but nothing else worked):
virtual: {
// cache: true,
slides: (function() {
const slides = [];
for (let i = 0; i < 1; i += 1) {
slides.push('<img src="https://picsum.photos/600/600/?image=1/>';
}
return slides;
})()
},
Problem - I want to use a custom component 'app-member-card' as my slide instead of a string, so I add it as a string like below, but when it's rendered on the screen it doesn't interpolate/transpile/compile/render/etc the components code or HTML, the debugger shows the string I passed it.
I'd like to do this:
slides.push('<app-member-card class="router-link" [MemberCard]="member"></app-member-card>');
Where this below is what I'd see on the screen:
<div class="card member-card">
<div class="row">...other tags left out here for verbosity</div>
</div>
Question: Is there something I can do in Angular or swiper.js that will translate this string into the component when adding it as a slide? I couldn't find anything in the swiper doc and not that familiar with all of the Angular stuff.
What I tried - I tried dynamically creating the component in my .ts file and adding the properties, but when I try and extract the innerHTML as a string from the viewContainerRef all data is empty. Here is my link to that post on SO
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在过度使用它。有一个理由使其在Angular中“难以”注入HTML。
我在另一个问题上给您的答案应该足以满足您所需的结果。但是,最好的方法是不要存储HTML。而是存储要渲染的
app-member-card
所需的内容。与此类似的内容:
然后,在您的html中,您可以直接渲染
app-member-card
:这正是要使用的角度,我在另一个问题中发布的hacky-thing可以。只要您不关心可维护性。通用/动态代码可能会很快变得凌乱。
You're overdoing it. There's a reason for it to be "that hard" in Angular to inject HTML in it.
The answer I gave you at the other question should be enough to fulfill your desired result. However, the best approach would be to NOT store HTML. Instead, store what is needed for
app-member-card
to be rendered.Something similar to this:
Then in your HTML you can render
app-member-card
directly:That's just how Angular is meant to be used, the hacky-thing I posted in the other question is ok to be used as long as you don't care about maintainability. Generic/dynamic code can get messy really quick.