我有这个模块,它将外部库与其他逻辑组件化,而无需将<script>标记直接添加到index.html中:
import 'http://external.com/path/file.js'
//import '../js/file.js'
@Component({
selector: 'my-app',
template: `
<script src="http://iknow.com/this/does/not/work/either/file.js"></script>
<div>Template</div>`
})
export class MyAppComponent {...}
Run Code Online (Sandbox Code Playgroud)
我注意到importES6规范是静态的,并且在TypeScript转换过程中而不是在运行时解析.
无论如何要使它可配置,以便file.js将从CDN或本地文件夹加载?如何告诉Angular 2动态加载脚本?
如何在Angular 2中重新加载相同的组件?
这是我的代码如下:
import { Component, OnInit, ElementRef, Renderer } from '@angular/core';
import { Router, ActivatedRoute, Params } from '@angular/router';
import { productModel } from '../_models/index';
import { categoryListService } from '../_services/index';
@Component({
selector: 'app-product',
templateUrl: 'product.component.html',
styleUrls: ['product.component.css']
})
export class productComponent implements OnInit {
uidproduct: productModel;
param: number;
constructor(
private elementRef: ElementRef,
private route: ActivatedRoute,
private router: Router,
private categoryListService: categoryListService) { }
ngOnInit() {
this.route.params.subscribe(product => {
console.log('logging sub product obj', product);
});
this.uidproduct = JSON.parse(sessionStorage.getItem('product'));
var …Run Code Online (Sandbox Code Playgroud) 我在index.html(root)中有一个脚本文件引用; index.html的:
<script src="page1.js"></script>
<script src="assets/sliderfunctions.js"></script>
<script src="assets/js/custom.js"></script>
Run Code Online (Sandbox Code Playgroud)
这里不需要sliderfunctions.js,它包含一些关于滑块的特定功能,所以我把它带到slider.component.html文件,但是你猜它不能正常工作,因为我实际上从未加载到浏览器..
slider.component.ts:
import {Component,ViewEncapsulation} from '@angular/core';
@Component({
selector:'app-slider',
templateUrl:'../templates/slider.component.html'
})
export class SliderComponent{
constructor(){}
}
Run Code Online (Sandbox Code Playgroud)
slider.component.html:
<script src="page1.js"></script>
<script src="assets/sliderfunctions.js"></script>
<script src="assets/js/custom.js"></script>
Run Code Online (Sandbox Code Playgroud)
(我带了3个,因为脚本的顺序很重要)所以我想得到一个特定的点,我已经在网上搜索但是无法弄清楚如何在组件模板中添加脚本文件
我有这样的问题。我正在制作一个角度应用程序。在那里,我制作了一个动画登录。这是我的组件 HTML 文件。
<div class="panda" xmlns="">
<div class="ear"></div>
<div class="face">
<div class="eye-shade"></div>
<div class="eye-white">
<div class="eye-ball"></div>
</div>
<div class="eye-shade rgt"></div>
<div class="eye-white rgt">
<div class="eye-ball"></div>
</div>
<div class="nose"></div>
<div class="mouth"></div>
</div>
<div class="body"> </div>
<div class="foot">
<div class="finger"></div>
</div>
<div class="foot rgt">
<div class="finger"></div>
</div>
</div>
<form>
<div class="hand"></div>
<div class="hand rgt"></div>
<h1>Panda Login</h1>
<div class="form-group">
<input required="required" class="form-control"/>
<label class="form-label">Username </label>
</div>
<div class="form-group">
<input id="password" type="password" required="required" class="form-control"/>
<label class="form-label">Password</label>
<p class="alert">Invalid Credentials..!!</p>
<button class="btn">Login </button>
</div>
</form> …Run Code Online (Sandbox Code Playgroud)