You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

TSParticles仅加载背景不显示粒子效果(Laravel Vue TS环境)

问题描述

全屏背景场景下接入粒子效果,页面仅渲染配置的蓝色背景,粒子完全不显示,浏览器控制台无报错输出。项目技术栈为Laravel + Vue2 + TypeScript + vue-class-component,初始代码如下:

ExampleComponent.vue

<template>
    <Particles id="tsparticles"
               :options="{
      fpsLimit: 60,
      background: {
            color: {
                value: '#0d47a1'
            }
        },
      particles: {
        color: {
          value: '#000'
        },
        move: {
          enable: true
        },
        links: {
          enable: true,
          color: '#000'
        }
      }
    }" />

</template>

<script lang="ts">
import Vue from "vue";
import Component from "vue-class-component";

@Component
export default class ExampleComponent extends Vue {}
</script> 

app.ts

declare module "particles.vue";

import "./bootstrap";
import Vue from "vue";
import ExampleComponent from "./components/ExampleComponent.vue";

import Particles from "particles.vue";

Vue.use(Particles);

Vue.component("example", ExampleComponent);

new Vue({
    el: "#app",
});
问题原因
  1. 粒子配置缺失核心参数:没有配置particles.number.value粒子数量,该参数默认值为0,会直接导致无粒子渲染。
  2. 根实例渲染逻辑缺失:初始化Vue实例时没有指定要渲染的根组件,组件树无法正常挂载。
  3. 内联配置存在解析风险:模板内直接写复杂对象作为prop值,容易出现引号转义、响应式解析异常问题。
修复方案
  • 调整粒子配置,将配置抽离为组件实例属性,补全粒子数量、全屏配置等必填参数,修正后的ExampleComponent.vue代码:
<template>
    <Particles id="tsparticles" :options="particlesOptions" />
</template>

<script lang="ts">
import Vue from "vue";
import Component from "vue-class-component";

@Component
export default class ExampleComponent extends Vue {
  particlesOptions = {
    fpsLimit: 60,
    fullScreen: {
      enable: true,
      zIndex: 0
    },
    background: {
      color: {
        value: '#0d47a1'
      }
    },
    particles: {
      number: {
        value: 80,
        density: {
          enable: true,
          area: 800
        }
      },
      color: {
        value: '#000000'
      },
      move: {
        enable: true,
        speed: 2
      },
      links: {
        enable: true,
        color: '#000000',
        distance: 150,
        width: 1
      }
    }
  }
}
</script>
  • 补全Vue根实例的渲染逻辑,显式指定要渲染的根组件,修正后的app.ts代码:
declare module "particles.vue";

import "./bootstrap";
import Vue from "vue";
import ExampleComponent from "./components/ExampleComponent.vue";
import Particles from "particles.vue";

Vue.use(Particles);

new Vue({
  el: "#app",
  render: h => h(ExampleComponent)
});
  • 额外校验:确认安装的particles.vue版本适配Vue2,若安装了Vue3对应版本会出现组件渲染异常且控制台无报错的问题;非全屏模式下需要给Particles组件的父容器设置明确宽高,避免canvas高度为0导致内容不可见。

内容的提问来源于stack exchange,提问作者Joe Scotto

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.29 01:12:31