cant add script file to components html

断了今生、忘了曾经 提交于 2020-06-12 04:05:26

问题


I have had a script file reference in index.html(root); index.html:

    <script src="page1.js"></script>
<script src="assets/sliderfunctions.js"></script>
<script src="assets/js/custom.js"></script>

no need sliderfunctions.js here, it contains some specific functions about slider so I carry it to slider.component.html file but as you guess it doesnt work as I expected actually it never loaded to browser..

slider.component.ts:

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

@Component({
    selector:'app-slider',
    templateUrl:'../templates/slider.component.html'
})
export class SliderComponent{
    constructor(){}
}

slider.component.html:

 <script src="page1.js"></script>
<script src="assets/sliderfunctions.js"></script>
<script src="assets/js/custom.js"></script>

(I carried 3 of them because order of scripts is important)so I want to get a specific point that I already make search on net but cant figure out how to add script file within component templates


回答1:


The way you're thinking of is considered as security threat. You could either follow this answer

OR

You can refer script file from the component.ts file, just by doing System.import/require

System.import('filepath.js'); //or below one
require('filepath.js');


来源:https://stackoverflow.com/questions/41401141/cant-add-script-file-to-components-html

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!