$ npx create-next-app my-app $ cd my-app $ yarn dev
標籤跳轉css
<Link>
標籤進行跳轉。新版Next.js規定,在<Link>
內需嵌套<a>
標籤import Link from 'next/link' <Link href="/index"><a>跳轉</a></Link>
.js
API跳轉
使用Router.pushhtml
import Router from 'next/router' <button onClick={()=>{Router.push('/index')}}></button>
標籤傳參前端
//傳參頁面 <Link href="/index.js?number=1"><a>傳參</a></Link> //也能夠寫成 //<Link href={{pathname:'/index',query:{number:1}}}> // <a>傳參</a> // </Link> ... //接收頁面 import {withRouter} from 'next/router' const CPN=({router})=>{ return ( <div>{router.query.number}</div> ) }
API傳參node
Router.push({ pathname:'/index', query:{ number: 1 } })
語法ios
Router.events.on('routeChangeStart',()=>{ doSomething... })
全部遠端獲取的數據都要放在getInitialProps裏json
const Cpn.getInitialProps = ()=>{ const promise = new Promise((resolve)=>{ axios(url,(res)=>{ console.log(res.data.xxx) resolve(res.data.xxx) }) }) return promise }
這樣操做後,xxx就會成爲Cpn的參數對象上的屬性了axios
const Cpn = ({xxx})=>{ return <div>{xxx}</div> }
return ( <div></div> <style jsx> {` div { width: 20px; height: 20px;} `} </style> )
{}
和 `
包起來$ yarn add @zeit/next-css
//next.config.js const withCSS = require('@zeit/next-css') if(typeof require !== 'undefined'){ require.extensions['.css']=file=>{} } module.exports = withCSS({})
import Head from 'next/head' function Header()=>( <div> <Head> <title>React,前端,JavaScript</title> <meta name="xxx" /> </Head> <div>首頁<div> </div> ) export default Header