Load scripts through all components in Angular 2

前端 未结 2 1039
终归单人心
终归单人心 2021-01-07 02:09

I have been using Angular 2 with AdminLTE which needs to run some scripts to load properly. So I have added them in in my .anglular-cli.json:



        
相关标签:
2条回答
  • 2021-01-07 02:46

    A simple solution I found was just to add a window.location.href = '...' to whatever link you are opening after login. Heres my function for the when the login button is clicked.

    this.authService.authenticateMember(member).subscribe(data => {
      if (data.success) {
          ...
          window.location.href = '/'
          ...
      } else {
          ...
      }
    }
    

    So if you are using authentication then you can put that above code at the end of your login success response and it will refresh the page aswell as take the user to wherever they are going after a successful login.

    This is not the best solution but it is the simplest one i can think of that dosent require too many changes. Hope it helps :)

    0 讨论(0)
  • 2021-01-07 02:51

    You can remove and recreate your script elements in DOM every time you need, like this:

    document.getElementById("myScript").remove();
    var testScript = document.createElement("script");
    testScript.setAttribute("id", "myScript");
    testScript.setAttribute("src", "assets/js/script.js");
    document.body.appendChild(myScript);
    

    I posted a question in GitHub where I explain how to do it.

    0 讨论(0)
提交回复
热议问题