How to pass data from ASP.NET WebForms to Aurelia Global Scope

不羁岁月 提交于 2019-11-29 21:09:19

问题


I am bootstrapping Aurelia from a Web-forms based legacy application. My authentication related information is maintained in the web-forms application in the Custom Base Page class.

How do I pass & maintain this authentication information to Aurelia's global scope? So that I can use it while building the menus using routes to show/hide certain menu items based on the user/role?


回答1:


You could put logic your custom base page to add a <script> tag to the head of the document that makes all the information available to javascript applications:

<head>
  ...
  <script>
    window.appInfo = {
      user: 'foo',
      bar: 'baz'
    };
  </script>
</head>
<body aurelia-app="main">
  ...

Then in your aurelia app you can access this info as needed:

export class App {
  constructor() {
    let info = window.appInfo;
    // do something with the app info...
  }
  ...
}

You could even register the object in the container, enabling you to declare it as a dependency. This will make your code more portable and testable.

In main.js: aurelia.container.registerInstance('app-info', window.appInfo);

@inject('app-info')
export class App {
  constructor(info) {
    // do something with the app info...         
  }
  ...
}


来源:https://stackoverflow.com/questions/36602231/how-to-pass-data-from-asp-net-webforms-to-aurelia-global-scope

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