Where should I include a script for a view component?

前端 未结 5 1808
故里飘歌
故里飘歌 2021-02-20 04:26

I have tried adding a section script inside a view component\'s view.

@section scripts {
    

        
5条回答
  •  北海茫月
    2021-02-20 04:42

    @section scripts { } in viewcomponents is ignored and not rendered by Asp.Net rendering engine. So just use at the end of the view component. Also if your jquery scripts are at specified at the end in your layout, then jquery will not be available in your viewcomponents. Of course moving the jquery script to the head section in layout will solve the problem but it is recommended to load the js files at the end.

    So if you want to keep jquery scripts at the end of layout and still use jquery in viewcomponents, you could use javascript domcontentloaded and any jquery can be written inside domcontentloaded. Not a permanent good approach but works for me.

    
    

    Or as mentioned by @Alberto L. Bonfiglio you could also try to move your script to another JS file and defer load it in your viewcomponent:

    
    

提交回复
热议问题