首页 > 解决方案 > 如何访问从 aspx 到 .jsx 的变量

问题描述

我目前正在尝试将 ASPX 中的 AngularJS 代码迁移到 React。我想将一些 aspx 变量传递给 .jsx 文件,它可以被反应组件使用。

Mainpage.aspx 包含一些需要传递给 Client.jsx(在 React 中)的变量。

我正在尝试从 .aspx 文件访问此变量 noSessionText 到 .jsx,但我无法实现它。

aspx 代码

<asp:Content ID="BodyContent" ContentPlaceHolderID="ContentPlaceHolderMiddlePanel" runat="Server">
  var noSessionText = '<%= Resources.WebPageResource.SessionSharing_NoSessions_Text %>';
      var HeaderName = '<%= Resources.WebPageResource.SessionSharing_Header_Name %>';
  <!--test code-->
      <script src="https://unpkg.com/react@15/dist/react.js"></script>
      <script src="https://unpkg.com/react-dom@15/dist/react-dom.js"></script>
      <script src="https://unpkg.com/babel-standalone@6.26.0/babel.min.js"></script>
      <script src="https://unpkg.com/axios/dist/axios.min.js"></script>
      <script  type="text/babel" src="includes/scripts/Client.jsx"></script>

  <div id="SessionClient_root" ></div>       

    </div>
  </div>
</asp:Content>

jsx代码

class Client extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            activeSessions: {},
            headerUserSession: props.HeaderName
        };
    }
    renderSessionDetails() {
        console.log("inside rendersessiondetails");
        console.log("headerusersession from aspx->react" + this.state.HeaderName);
    }

    render() {

        return (
            <div>
                <h3 className="sessionmanager__headline">SESSIONCLIENT IN REACT</h3>
                {this.renderSessionDetails()}
            </div>
            );
    }
}
ReactDOM.render(
    <Client />,
    document.getElementById('SessionClient_root')
);

有什么方法可以实现这一点。提前感谢您的帮助。

标签: asp.netreactjswebforms

解决方案


我的 .aspx 文件中的更改 let noSessionText = '<%= Resources.WebPageResource.SessionSharing_NoSessions_Text %>'; 让 HeaderName = '<%= Resources.WebPageResource.SessionSharing_Header_Name %>';

.jsx 文件访问上述变量只需使用变量名 alert(noSessionText);

需要考虑的步骤: 1. 在标签中添加 let 类型的变量。不应使用 var。2.通过将其添加到脚本标签中,它将在全球范围内可用。3.从其他js文件中,您可以轻松访问在其他js文件中全局定义的变量。


推荐阅读