Where should I include a script for a view component?

前端 未结 5 1887
故里飘歌
故里飘歌 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:54

    I also had problems with sections tag in viewcomponents. Turns out, to the best of my knowledge, there is no support for it in viewcomponents. See https://github.com/aspnet/Home/issues/2037

    Jake Shakesworth has implemented a tag helper as shown in: Javascript in a View Component

    On the other hand you could just include it in your viewcomponent as an

    
    

    My requirement was to show a google map from a viewcomponent. Problem was that the script was getting called before the jquery, jquery.ui stuff.
    By using defer you are telling the parser not to execute it until the document had loaded thus avoiding the problem of the having to put it in the layout for proper execution. Defer is supported by chrome, safari, and ie(10+), ff(3.6+), o(15+)

    Hope this helps

    This is an example of my code:

    @using MobileVet.WebApp.Services;
    @inject ISettingsService SettingsService
    @{
         var Options = SettingsService.Value();
    
        
        
    Site Navigation

    }

    Note that you would probably need to write some logic to prevent the script to be included multiple times if the view component is present more than once on a page, which was not my case

提交回复
热议问题