如何在Angular 18项目中使用Spline Viewer?报错解决及支持咨询
在Angular中使用Spline Viewer的解决方案
当前Spline官方并未提供Angular原生的NgModule或独立组件支持,你需要通过Web Component的方式来集成Spline Viewer,具体步骤如下:
安装依赖
确保已安装Spline Viewer包:npm install @splinetool/viewer允许Angular识别自定义元素
如果是独立组件,在组件装饰器中添加CUSTOM_ELEMENTS_SCHEMA:import { Component, CUSTOM_ELEMENTS_SCHEMA } from '@angular/core'; import { CommonModule } from '@angular/common'; @Component({ selector: 'app-spline-wrapper', standalone: true, imports: [CommonModule], schemas: [CUSTOM_ELEMENTS_SCHEMA], template: `<spline-viewer url="你的Spline场景URL"></spline-viewer>`, }) export class SplineWrapperComponent {}如果是NgModule项目,在对应模块的装饰器中添加
CUSTOM_ELEMENTS_SCHEMA:import { NgModule, CUSTOM_ELEMENTS_SCHEMA } from '@angular/core'; import { BrowserModule } from '@angular/platform-browser'; import { AppComponent } from './app.component'; @NgModule({ imports: [BrowserModule], declarations: [AppComponent], schemas: [CUSTOM_ELEMENTS_SCHEMA], bootstrap: [AppComponent], }) export class AppModule {}导入Spline Viewer的Web Component定义
在项目入口文件(如main.ts)中导入Spline Viewer,让Angular能识别<spline-viewer>标签:import '@splinetool/viewer'; // 其他启动代码 import { bootstrapApplication } from '@angular/platform-browser'; import { AppComponent } from './app/app.component'; bootstrapApplication(AppComponent);在模板中使用
直接在组件模板里使用<spline-viewer>标签,传入你的Spline场景URL即可:<spline-viewer url="https://prod.spline.design/xxx.splinecode"></spline-viewer>
关于Angular原生支持:目前Spline官方未推出专门的Angular集成方案,后续是否支持需关注官方更新。
内容的提问来源于stack exchange,提问作者Nikita Smirnov
相关产品推荐
相关产品推荐

