如何将配置值从Asp.Net Core MVC 2.0配置文件推送到React TypeScript客户端脚本? [英] How to push configuration values from Asp.Net Core MVC 2.0 config file to React TypeScript client script?

查看:99
本文介绍了如何将配置值从Asp.Net Core MVC 2.0配置文件推送到React TypeScript客户端脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 React Template ASP.NET MVC Core 2.0 上有一个应用程序。
我在服务器端的配置文件中有属性 - SiteURL 。我需要将它推送到客户端 TypeScript ,并且我不知道如何制作它?

I have an application on ASP.NET MVC Core 2.0 using React Template. I have propertie in config file on server side - SiteURL. I need to push it to client TypeScript, and don`t understand how can I make it?

我试过通过放入最后一个脚本推送他们抛出 Home / Index 文件

I tried to push them throw Home/Index file by placing in the last script

SiteProps.SiteURL= "http://mysiteurl.org"

在客户端我做

export var SiteProps: { SiteURL: string } = {SiteURL: ""};

并在启动时导入此文件

但是在控制台上测试这个属性我看到了

But testing this property on console I see


未捕获的ReferenceError:未定义SiteProps

Uncaught ReferenceError: SiteProps is not defined

我认为 JavaScript 没有从 TypeScript 上下文中看到我的属性。

I think that JavaScript did't see my propertie from TypeScript context.

问题是如何使这段代码工作,将const值(属性)推送到客户端的最佳做法是什么 TypeScript 代码。

The question is how to make this code work, and what is the best practice to push const values (properties) to client TypeScript code.

推荐答案

我找到解决办法在 Home / Index

<div id="site-props" style="display: none;">@ViewBag.PropsJSON</div>

其中 PropsJSON JSON 我的配置字符串。
之后我使用以下代码初始化我的客户端配置:

Where PropsJSON is JSON string of my configuration. After it i initialize my clientscript configuration using this code:

function ImportProps() {
    var ell = document.getElementById('site-props');
    var jsontext = ell!.innerText;
    SiteProps = JSON.parse(jsontext);
}

这篇关于如何将配置值从Asp.Net Core MVC 2.0配置文件推送到React TypeScript客户端脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆