【发布时间】:2019-05-15 21:53:24
【问题描述】:
我需要为页面的<title></title> 添加动态值。为此,我正在使用 next/head 提供的 Head 组件。
标题需要在某些用户操作时更改(因此不能放在 _app.js 或 _document.js 中)。
但是,如果我放置此代码:
<Head><title>Page title</title></Head>
_app.js 或 _document.js 以外的任何地方,都会出现在页面的 <body> 标记中,这是不需要的。
// 模块/about_us/index.js
import Head from 'next/head'
export default class AboutUs extends Component {
state = {
title: 'About you'
}
someEvent1(){
this.setState({title : 'About team'})
}
someEvent2(){
this.setState({title : 'About company'})
}
render(){
return (
<div>
<Head>
<title>{this.state.title}</title>
</Head>
<div>This is one of the module used to display the page.</div>
<button onClick={this.someEvent1}>About team</button>
<button onClick={this.someEvent2}>About company</button>
</div>
)
}
}
【问题讨论】:
-
你找到答案了吗?
-
也遇到这种情况