如何在Angular 13项目中引入JavaScript文件?——Visual Studio模板迁移后JS代码失效问题咨询
Hey there, let's get that spinner JavaScript code working in your Angular 13 project. Since your code relies on jQuery, I'll walk you through a few solid approaches—including a jQuery-free method that's more Angular-friendly:
Solution 1: Global Script Inclusion (For Wide-Spread Use)
If you plan to use this spinner (or other jQuery-based code) across multiple components, setting up global imports is the way to go:
Install jQuery and its TypeScript types (since Angular uses TypeScript, we need type declarations to avoid errors):
npm install jquery --save npm install @types/jquery --save-devAdd scripts to your
angular.json(this tells Angular to load these files globally):
Open your project'sangular.json, find thearchitect > build > options > scriptsarray, and add these entries:"scripts": [ "node_modules/jquery/dist/jquery.min.js", "src/assets/js/spinner.js" // Save your spinner code in this file first ]Note: Create the
src/assets/jsfolder if it doesn't exist, then paste your spinner code intospinner.js.Update
tsconfig.json(to let TypeScript recognize jQuery):
Intsconfig.json, add"jquery"to thecompilerOptions > typesarray:"compilerOptions": { // ... other settings "types": ["jquery"] }Restart your Angular dev server—changes to
angular.jsonrequire a restart to take effect.
Solution 2: Component-Level Integration (Only For This Component)
If the spinner is only needed in app.component, you can load the code directly in the component:
Install jQuery and types (same as Step 1 in Solution 1):
npm install jquery --save npm install @types/jquery --save-devModify
app.component.ts:
Use thengAfterViewInitlifecycle hook (to ensure the DOM is fully loaded before accessing elements) and wrap your spinner code inside it:import { Component, AfterViewInit } from '@angular/core'; declare var $: any; // Declare jQuery to avoid TypeScript errors @Component({ selector: 'app-root', templateUrl: './app.component.html', styleUrls: ['./app.component.css'] }) export class AppComponent implements AfterViewInit { ngAfterViewInit(): void { // Your spinner code here (function ($) { "use strict"; var spinner = function () { setTimeout(function () { if ($('#spinner').length > 0) { $('#spinner').removeClass('show'); } }, 1); }; spinner(); })(jQuery); } }
Solution 3: Angular-Native Approach (Recommended, No jQuery Needed)
Angular has built-in tools to manipulate the DOM without relying on jQuery. This is the cleanest approach for Angular projects:
Add a template reference to your spinner in
app.component.html:<div #spinnerElement id="spinner" class="show"> <!-- Your spinner content here --> </div>Modify
app.component.tsto use Angular'sViewChildand native DOM methods:import { Component, ViewChild, ElementRef, AfterViewInit } from '@angular/core'; @Component({ selector: 'app-root', templateUrl: './app.component.html', styleUrls: ['./app.component.css'] }) export class AppComponent implements AfterViewInit { @ViewChild('spinnerElement') spinnerElement!: ElementRef; ngAfterViewInit(): void { setTimeout(() => { if (this.spinnerElement.nativeElement) { this.spinnerElement.nativeElement.classList.remove('show'); } }, 1); } }
Quick Troubleshooting Tips
- Double-check that the
#spinner(or#spinnerElement) DOM element exists inapp.component.html—if it's missing, the code won't find it. - If you're using global scripts, make sure the file paths in
angular.jsonare correct relative to your project root. - Always use
ngAfterViewInitinstead ofngOnInitwhen manipulating DOM elements—ngOnInitruns before the component's DOM is fully rendered.
内容的提问来源于stack exchange,提问作者BADR_ Ah

