反派冷酷小狗 發表於 2020-6-17 13:43:00

初步学习next.js-5-编写css样式和lazyloading

<p><span style="font-size: 18pt">CSS</span></p>
<div class="cnblogs_code">
<pre>import React, {useState} from 'react'

<span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> Jspang(){
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">关键代码----------start-------</span>
    const = useState('blue'<span style="color: rgba(0, 0, 0, 1)">)

    const changeColor</span>=()=&gt;<span style="color: rgba(0, 0, 0, 1)">{

      setColor(color</span>=='blue'?'red':'blue'<span style="color: rgba(0, 0, 0, 1)">)
    }
   </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">关键代码----------end-------</span>

    <span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> (
      </span>&lt;&gt;
            &lt;div&gt;技术胖免费前端教程&lt;/div&gt;
            &lt;div&gt;&lt;button onClick={changeColor}&gt;改变颜色&lt;/button&gt;&lt;/div&gt;
            &lt;style jsx&gt;<span style="color: rgba(0, 0, 0, 1)">
                {`
                  div { color:${color};}
                `}
            </span>&lt;/style&gt;
      &lt;/&gt;
<span style="color: rgba(0, 0, 0, 1)">    )
}
export </span><span style="color: rgba(0, 0, 255, 1)">default</span> Jspang</pre>
</div>
<p>&nbsp;</p>
<p><span style="font-size: 18pt">懒加载</span></p>
<p><span style="font-size: 18pt">  <span style="font-size: 14pt">懒加载模块</span></span></p>
<p><span style="font-size: 18pt"><span style="font-size: 14pt"> </span></span></p>
<div class="cnblogs_code">
<pre>import React, {useState} from 'react'
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">删除import moment</span>
<span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> Time(){

    const </span>=<span style="color: rgba(0, 0, 0, 1)"> useState(Date.now())

    const changeTime</span>= <span style="color: rgba(0, 0, 255, 1)">async</span> ()=&gt;{ <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">把方法变成异步模式</span>
       <span style="color: rgba(0, 0, 255, 1)"> const moment = await import('moment')</span> <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">等待moment加载完成</span>
      setTime(moment.<span style="color: rgba(0, 0, 255, 1)">default</span>(Date.now()).format()) <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">注意使用defalut</span>
<span style="color: rgba(0, 0, 0, 1)">    }
    </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> (
      </span>&lt;&gt;
            &lt;div&gt;显示时间为:{nowTime}&lt;/div&gt;
            &lt;div&gt;&lt;button onClick={changeTime}&gt;改变时间格式&lt;/button&gt;&lt;/div&gt;
      &lt;/&gt;
<span style="color: rgba(0, 0, 0, 1)">    )
}
export </span><span style="color: rgba(0, 0, 255, 1)">default</span> Time</pre>
</div>
<p>  <span style="font-size: 14pt">懒加载组件</span></p>
<div class="cnblogs_code">
<pre>export <span style="color: rgba(0, 0, 255, 1)">default</span> ()=&gt;&lt;div&gt;Lazy Loading Component&lt;/div&gt;</pre>
</div>
<div class="cnblogs_code">
<pre>import React, {useState} from 'react'<span style="color: rgba(0, 0, 0, 1)">
import dynamic from </span>'next/dynamic'<span style="color: rgba(0, 0, 0, 1)">

const One </span>= dynamic(import('../components/one'<span style="color: rgba(0, 0, 0, 1)">))

</span><span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> Time(){

    const </span>=<span style="color: rgba(0, 0, 0, 1)"> useState(Date.now())

    const changeTime</span>= async ()=&gt;<span style="color: rgba(0, 0, 0, 1)">{
      const moment </span>= await import('moment'<span style="color: rgba(0, 0, 0, 1)">)

      setTime(moment.</span><span style="color: rgba(0, 0, 255, 1)">default</span><span style="color: rgba(0, 0, 0, 1)">(Date.now()).format())
    }
    </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> (
      </span>&lt;&gt;
            &lt;div&gt;显示时间为:{nowTime}&lt;/div&gt;
            &lt;One/&gt;
            &lt;div&gt;&lt;button onClick={changeTime}&gt;改变时间格式&lt;/button&gt;&lt;/div&gt;
      &lt;/&gt;
<span style="color: rgba(0, 0, 0, 1)">    )
}
export </span><span style="color: rgba(0, 0, 255, 1)">default</span> Time</pre>
</div>
<p>&nbsp;</p><br><br>
来源:https://www.cnblogs.com/lxz-blogs/p/13152017.html
頁: [1]
查看完整版本: 初步学习next.js-5-编写css样式和lazyloading