gpt4 book ai didi

javascript - 在 Angular 2 项目中添加 .js 脚本

转载 作者:技术小花猫 更新时间:2023-10-29 12:32:32 25 4
gpt4 key购买 nike

我正在使用 Angular 2 框架构建网络应用程序,我想使用外部模板 ( https://freehtml5.co/preview/?item=splash-free-html5-bootstrap-template-for-any-websites )。

我需要在我的组件模板中运行一些脚本(jquery.min.js、bootstrap.js 等),但是如果我将它放入脚本标记中则不起作用。

如果我在 index.html 中运行脚本标签,它可以工作,但是当我浏览到另一个页面时,脚本不会重新加载。

如何在模板中加载没有脚本标签的脚本?

index.html

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Angular2App</title>
<base href="/">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" type="image/x-icon" href="favicon.ico">

<!-- Facebook and Twitter integration -->
<meta property="og:title" content="" />
<meta property="og:image" content="" />
<meta property="og:url" content="" />
<meta property="og:site_name" content="" />
<meta property="og:description" content="" />
<meta name="twitter:title" content="" />
<meta name="twitter:image" content="" />
<meta name="twitter:url" content="" />
<meta name="twitter:card" content="" />

<!--<link href="build/main.css" rel="stylesheet">-->
<link href="https://fonts.googleapis.com/css?family=Roboto:300,400,700" rel="stylesheet">

<!-- Animate.css -->
<link rel="stylesheet" href="assets/css/animate.css">
<!-- Icomoon Icon Fonts-->
<link rel="stylesheet" href="assets/css/icomoon.css">
<!-- Themify Icons-->
<link rel="stylesheet" href="assets/css/themify-icons.css">
<!-- Bootstrap -->
<link rel="stylesheet" href="assets/css/bootstrap.css">

<!-- Magnific Popup -->
<link rel="stylesheet" href="assets/css/magnific-popup.css">

<!-- Owl Carousel -->
<link rel="stylesheet" href="assets/css/owl.carousel.min.css">
<link rel="stylesheet" href="assets/css/owl.theme.default.min.css">

<!-- Theme style -->
<link rel="stylesheet" href="assets/css/style.css">


// SCRIPTS THAT I NEED TO LOAD
<!-- Modernizr JS -->
<script src="assets/js/modernizr-2.6.2.min.js"></script>
<!-- FOR IE9 below -->
<!--[if lt IE 9]>
<script src="assets/js/respond.min.js"></script>
<![endif]-->
<!-- jQuery -->
<script src="assets/js/jquery.min.js"></script>
<!-- jQuery Easing -->
<script src="assets/js/jquery.easing.1.3.js"></script>
<!-- Bootstrap -->
<script src="assets/js/bootstrap.min.js"></script>
<!-- Waypoints -->
<script src="assets/js/jquery.waypoints.min.js"></script>
<!-- Carousel -->
<script src="assets/js/owl.carousel.min.js"></script>
<!-- countTo -->
<script src="assets/js/jquery.countTo.js"></script>
<!-- Magnific Popup -->
<script src="assets/js/jquery.magnific-popup.min.js"></script>
<script src="assets/js/magnific-popup-options.js"></script>
<!-- Main -->
<script src="assets/js/main.js"></script>
// SCRIPTS THAT I NEED TO LOAD


</head>
<body>
<app-root></app-root>
</body>
</html>

应用程序模块.ts

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http';
import { routing } from './app.routing.module';

import { AppComponent } from './app.component';
import { HomeComponent } from './home/home.component';
import { LoginComponent } from './login/login.component';

@NgModule({
declarations: [
AppComponent,
HomeComponent,
LoginComponent
],
imports: [
BrowserModule,
FormsModule,
HttpModule,
routing
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule {
}

app.routing.module.ts

import { Routes, RouterModule } from '@angular/router';
import { HomeComponent } from './home/home.component';
import { LoginComponent } from './login/login.component';

const routes: Routes = [
{
path: 'login',
component: LoginComponent
},
{
path: 'home',
component: HomeComponent
},
{
path: '',
component: LoginComponent
},
{
path: '**',
component: LoginComponent
}
];

export const routing = RouterModule.forRoot(routes);

应用程序组件.ts

import { Component } from '@angular/core';

@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})

export class AppComponent {
}

app.component.html

<router-outlet></router-outlet>

最佳答案

如果您使用的是 Angular CLI,您可以将 node_modules 中的 .js 文件包含在 .angular-cli.json 中。

styles 数组下方和 environmentSource 上方,您应该会看到 scripts 数组。我已经包括了一点(我已经 chop 了)JSON 作为您的引用。

{
"apps": [
{
"root": "src",
"outDir": "dist",
"assets": [
"assets",
"favicon.ico"
],
"index": "index.html",
"main": "main.ts",
"styles": [
"styles.scss"
],
"scripts": [
"../node_modules/hammerjs/hammer.min.js"
],
"environmentSource": "environments/environment.ts",
"environments": {
"dev": "environments/environment.ts",
"prod": "environments/environment.prod.ts"
}
}
]
}

关于javascript - 在 Angular 2 项目中添加 .js 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41652742/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com