asp.net - 如何访问从 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')
);
有什么方法可以实现这一点。提前感谢您的帮助。
解决方案
我的 .aspx 文件中的更改 let noSessionText = '<%= Resources.WebPageResource.SessionSharing_NoSessions_Text %>'; 让 HeaderName = '<%= Resources.WebPageResource.SessionSharing_Header_Name %>';
.jsx 文件访问上述变量只需使用变量名 alert(noSessionText);
需要考虑的步骤: 1. 在标签中添加 let 类型的变量。不应使用 var。2.通过将其添加到脚本标签中,它将在全球范围内可用。3.从其他js文件中,您可以轻松访问在其他js文件中全局定义的变量。
推荐阅读
- javascript - 具有相同自定义指令 vuejs 的多个元素
- node.js - 在 Heroku 上部署 React 应用程序和 Nginx 时出现问题
- powershell - 如何从包含“键”和“值”文本的哈希表中检索所有键?
- excel - 在数组中选择工作表时出现下标超出范围错误
- php - 在数据库中上传和插入图像链接时出现问题。
- android - 为什么 Android Studio Emulator 会扭曲背景图片
- jmeter - Jmeter - 安装插件 WebSocket Sampler
- regex - 如何从列scala中提取字符串?
- angular - Angular Kendo Grid InCell Editing Capture Edited DataItem
- amazon-cognito - 有没有办法立即撤销 Cognito Client Credentials Flow 颁发的访问令牌?