簡體   English   中英

使用ASP .NET Core響應智能感知道具

[英]React props intellisense with ASP .NET Core

我正在嘗試獲取對React道具的智能感知。 在此之前,我會將ViewModel傳遞給Razor視圖(.cshtml)。 @model namespace.WeatherVM 然后,我可以輕松訪問所有可用的屬性,例如@Model.TemperatureF Intellisense會彈出,讓我知道可以訪問哪些內容。

在React中,我想做同樣的事情。 在我的VM中,如果我有40個屬性,則需要具有智能感知或可以顯示所有可用屬性的屬性。

假設我有這樣的事情:

function renderForecastsTable(props) {
    return (
        <table className='table'>
            <thead>
                <tr>
                    <th>Date</th>
                    <th>Temp. (C)</th>
                    <th>Temp. (F)</th>
                    <th>Summary</th>
                    <th></th>
                </tr>
            </thead>
            <tbody>
                {props.forecasts.map(forecast =>
                    <tr key={forecast.dateFormatted}>
                        <td>{forecast.dateFormatted}</td>
                        <td>{forecast.temperatureC}</td>
                        <td>{forecast.temperatureF}</td>
                        <td>{forecast.summary}</td>
                    </tr>
                )}
            </tbody>
        </table>
    );
}

我怎樣才能智能告訴我道具有預報? 還是告訴我,每個預測都具有諸如temperatureF之類的屬性?

我嘗試過沒有運氣的原型。
/models/weatherForecast.js

import PropTypes from 'prop-types';
export const WeatherForecast = PropTypes.shape({
    TemperatureC: React.PropTypes.string,
    TemperatureF: React.PropTypes.string,
    Summary: React.PropTypes.string,
    TestProp: React.PropTypes.string
});

然后嘗試智能工作
FetchData.js

import * as Model from '../models/weatherForecast';
class FetchData extends Component {
    componentWillMount() {
        this.props.weatherForecast.TestProp // <---- No intellisense or no suggestion was provided for the existence of 'TestProp'. I had to manually write this in.
    }
}
FetchData.prototype = { weatherForecast: Model.WeatherForecast };

我對此有什么想法嗎? 我知道Typescript是一種解決方案,但這是唯一的解決方案嗎?

如果使用Visual Studio Code,則可以使用React PropTypes Intellisense擴展。

然后,這將為您提供有關PropTypes組件的智能建議。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM