首页 > 解决方案 > 如何在 app.js - React JS 中导入 react-document-title?

问题描述

我正在尝试在 MVC 4.x 框架中使用 REACT JS。我可以使用 REACT 设置标题标签。但是,我无法在 ASP.NET 中使用 REACT.JS 设置页面标题。

我尝试导入 DocumentTitle 来设置页面的标题。

从 'react-document-title' 导入 DocumentTitle;

应用程序.JS -

类 Hello 扩展 React.Component {

componentDidMount() {
    document.title = "Home Page"
}
render() {
    return (
        <DocumentTitle title='Home'>
        <h1>
        Hello {this.props.name}  {this.props.time}
            </h1>
        </DocumentTitle>
    );
}

}

索引.cshtml

@using React.Web.Mvc

<h2>Index</h2>

<!-- React Component -->
<!-- Hello is react component-->
<!-- name is props -->
@Html.React("Hello", new
{
    name = "BC",
    time = DateTime.Now.Year,
    websiteTitle = ViewBag.message
})

我希望根据我在控制器中设置的 ViewBag.message 设置页面标题。但是,它显示的是默认值 -

标签: c#reactjs.net

解决方案


推荐阅读