title: next.js入門php
yarn next react react-dom --save
{ "scripts": { "dev": "next", "build": "next build", "start": "next start" } }
默認狀況下:html
// ./page/index.js var num = 1 num ++ console.log(num) function click () { console.log(num++) } export default () => ( <div onClick={click}>Hello Next.js</div> )
yarn dev yarn dev -p 8080 // 指定端口號
pathname - URL 的 path 部分
query - URL 的 query 部分,並被解析成對象
asPath - 顯示在瀏覽器中的實際路徑(包含查詢部分),爲String類型
req - HTTP 請求對象 (只有服務器端有)
res - HTTP 返回對象 (只有服務器端有)
jsonPageRes - 獲取數據響應對象 (只有客戶端有)
err - 渲染過程當中的任何錯誤前端
import {Component} from 'react' export default class App extends Component { static async getInitialProps(obj) { console.log(obj) console.log('where called') var fetch = (url) => { return new Promise((res, rej) => { res('獲取數據而後渲染') }) } let response = await fetch('/static/demo.json') console.log(response) return {response} } state = { num: 1 } add = () => { this.setState((state) => { return state.num++ }) } render () { return ( <div> <div>{this.props.response}</div> <div>{this.state.num}</div> <button onClick={this.add}>++</button> </div> ) } }
如下是主要部分,能夠看出數據是被預先渲染好的。react
<div>獲取數據而後渲染</div><div>1</div><button>++</button></div></div>
Next.js 支持 IE11 和全部的現代瀏覽器使用了@babel/preset-env。爲了支持 IE11,Next.js 須要全局添加Promise的 polyfill。有時你的代碼或引入的其餘 NPM 包的部分功能現代瀏覽器不支持,則須要用 polyfills 去實現。git
原文連接es6
【完】 喜歡的歡迎 star && issuegithub
做者簡介:葉茂,蘆葦科技web前端開發工程師,表明做品:口紅挑戰網紅小遊戲、服務端渲染官網、微信小程序粒子系統。擅長網站建設、公衆號開發、微信小程序開發、小遊戲、公衆號開發,專一於前端領域框架、交互設計、圖像繪製、數據分析等研究。 一塊兒並肩做戰: yemao@talkmoney.cn 訪問 www.talkmoney.cn 瞭解更多web