美文网首页
2020-01-07nuxt.js服务端渲染使用rem

2020-01-07nuxt.js服务端渲染使用rem

作者: Kason晨 | 来源:发表于2020-01-07 10:23 被阅读0次

1,下载flexible.js

可加载阿里的cdn文件 http://g.tbcdn.cn/mtb/lib-flexible/0.3.4/??flexible_css.js,flexible.js
或者将下载好的文件放到static也就是静态资源文件中(就是下面的一整行。)

!function(){var a="@charset \"utf-8\";html{color:#000;background:#fff;overflow-y:scroll;-webkit-text-size-adjust:100%;-ms-text-size-adjust:100%}html *{outline:0;-webkit-text-size-adjust:none;-webkit-tap-highlight-color:rgba(0,0,0,0)}html,body{font-family:sans-serif}body,div,dl,dt,dd,ul,ol,li,h1,h2,h3,h4,h5,h6,pre,code,form,fieldset,legend,input,textarea,p,blockquote,th,td,hr,button,article,aside,details,figcaption,figure,footer,header,hgroup,menu,nav,section{margin:0;padding:0}input,select,textarea{font-size:100%}table{border-collapse:collapse;border-spacing:0}fieldset,img{border:0}abbr,acronym{border:0;font-variant:normal}del{text-decoration:line-through}address,caption,cite,code,dfn,em,th,var{font-style:normal;font-weight:500}ol,ul{list-style:none}caption,th{text-align:left}h1,h2,h3,h4,h5,h6{font-size:100%;font-weight:500}q:before,q:after{content:''}sub,sup{font-size:75%;line-height:0;position:relative;vertical-align:baseline}sup{top:-.5em}sub{bottom:-.25em}a:hover{text-decoration:underline}ins,a{text-decoration:none}",b=document.createElement("style");if(document.getElementsByTagName("head")[0].appendChild(b),b.styleSheet)b.styleSheet.disabled||(b.styleSheet.cssText=a);else try{b.innerHTML=a}catch(c){b.innerText=a}}();!function(a,b){function c(){var b=f.getBoundingClientRect().width;b/i>540&&(b=540*i);var c=b/10;f.style.fontSize=c+"px",k.rem=a.rem=c}var d,e=a.document,f=e.documentElement,g=e.querySelector('meta[name="viewport"]'),h=e.querySelector('meta[name="flexible"]'),i=0,j=0,k=b.flexible||(b.flexible={});if(g){console.warn("灏嗘牴鎹凡鏈夌殑meta鏍囩鏉ヨ缃缉鏀炬瘮渚�");var l=g.getAttribute("content").match(/initial\-scale=([\d\.]+)/);l&&(j=parseFloat(l[1]),i=parseInt(1/j))}else if(h){var m=h.getAttribute("content");if(m){var n=m.match(/initial\-dpr=([\d\.]+)/),o=m.match(/maximum\-dpr=([\d\.]+)/);n&&(i=parseFloat(n[1]),j=parseFloat((1/i).toFixed(2))),o&&(i=parseFloat(o[1]),j=parseFloat((1/i).toFixed(2)))}}if(!i&&!j){var p=(a.navigator.appVersion.match(/android/gi),a.navigator.appVersion.match(/iphone/gi)),q=a.devicePixelRatio;i=p?q>=3&&(!i||i>=3)?3:q>=2&&(!i||i>=2)?2:1:1,j=1/i}if(f.setAttribute("data-dpr",i),!g)if(g=e.createElement("meta"),g.setAttribute("name","viewport"),g.setAttribute("content","initial-scale="+j+", maximum-scale="+j+", minimum-scale="+j+", user-scalable=no"),f.firstElementChild)f.firstElementChild.appendChild(g);else{var r=e.createElement("div");r.appendChild(g),e.write(r.innerHTML)}a.addEventListener("resize",function(){clearTimeout(d),d=setTimeout(c,300)},!1),a.addEventListener("pageshow",function(a){a.persisted&&(clearTimeout(d),d=setTimeout(c,300))},!1),"complete"===e.readyState?e.body.style.fontSize=12*i+"px":e.addEventListener("DOMContentLoaded",function(){e.body.style.fontSize=12*i+"px"},!1),c(),k.dpr=a.dpr=i,k.refreshRem=c,k.rem2px=function(a){var b=parseFloat(a)*this.rem;return"string"==typeof a&&a.match(/rem$/)&&(b+="px"),b},k.px2rem=function(a){var b=parseFloat(a)/this.rem;return"string"==typeof a&&a.match(/px$/)&&(b+="rem"),b}}(window,window.lib||(window.lib={}));

[图片上传失败...(image-50ca79-1578363505086)]

2,下载postcss-px2rem

npm install --save postcss-px2rem

3,配置nuxt.cofig.js

在head里加上

script: [
   { src: '/vipService/js/flexible.js', type: 'text/javascript', charset: 'utf-8'}
],

在build里加上

postcss: [
   require('postcss-px2rem')({
     remUnit: 75
   })
],

完整的nuxt.config.js


module.exports = {
  mode: 'universal',
  /*
  ** Headers of the page
  */
  head: {
    title: process.env.npm_package_name || '',
    meta: [
      { charset: 'utf-8' },
      { name: 'viewport', content: 'width=device-width, initial-scale=1' },
      { hid: 'description', name: 'description', content: process.env.npm_package_description || '' }
    ],
    link: [
      { rel: 'icon', type: 'image/x-icon', href: '/favicon.ico' }
    ],
    script: [
      { src: '/rem/flexible.js', type: 'text/javascript', charset: 'utf-8' }
    ],

  },
  /*
  ** Customize the progress-bar color
  */
  loading: { color: '#fff' },
  /*
  ** Global CSS
  */
  css: [
    'element-ui/lib/theme-chalk/index.css',
    {src:'~assets/css/index.scss',lang:'scss'}
  ],
  /*
  ** Plugins to load before mounting the App
  */
  plugins: [
    '@/plugins/element-ui'
  ],
  /*
  ** Nuxt.js dev-modules
  */
  buildModules: [
  ],
  /*
  ** Nuxt.js modules
  */
  modules: [
  ],
  /*
  ** Build configuration
  */
  build: {
    extractCSS: { allChunks: true },
    transpile: [/^element-ui/],
    postcss: [
      require('postcss-px2rem')({
        remUnit: 75
      })
    ],

    /*
    ** You can extend webpack config here
    */
    extend(config, ctx) {
    }
  }
}

配置成功:
[图片上传失败...(image-7ad102-1578363692702)]

相关文章

  • 2020-01-07nuxt.js服务端渲染使用rem

    1,下载flexible.js 可加载阿里的cdn文件 http://g.tbcdn.cn/mtb/lib-fle...

  • 服务端渲染与客户端渲染

    服务端渲染与客户端渲染 服务端渲染说白了,就是在服务端使用模板引擎末班引擎最早诞生于服务端,后来才到了前端 服务端...

  • react-dom/server

    react-dom/server能够使将组建渲染为静态标记,通常使用与Node服务端做服务端渲染上。 render...

  • 使用 Docker + PM2 + Jenkins 部署服务端渲

    服务端渲染的项目打包后,需要使用 Node 运行服务端的脚本文件。在我的服务端渲染项目中,客户端和服务端打包的代码...

  • 服务端渲染SSR之UmiJS预渲染

    UmiJS 服务端渲染 本文主要介绍 UmiJS 的预渲染功能。 一、什么是服务端渲染? 服务端渲染(Server...

  • 第一个vue项目总结

    最近幸运的参与了公司m站重构项目,项目使用了nuxt、vant nuxt是基于vue的服务端渲染框架,服务端渲染有...

  • webpack4进阶知识点(一)

    1.移动端px自动转rem 下载px2rem-loader 页面渲染时计算根元素的font-size使用手机淘宝的...

  • 服务端渲染和浏览器渲染

    也就是传统的后端渲染以及前端渲染 1. 概念 服务端渲染: 即使用ASP、Java、PHP这种做后端渲染。前端写...

  • SSR服务端同构渲染

    页面渲染历史 服务端框架模板渲染 -> 客户端渲染 -> 服务端同构渲染(Server Side Render) ...

  • 服务端渲染(SSR)

    导读 本文主要是从三个方面学习服务端渲染,内容整理自多个博客。 服务端渲染是什么?什么是服务端渲染?(服务端渲染的...

网友评论

      本文标题:2020-01-07nuxt.js服务端渲染使用rem

      本文链接:https://www.haomeiwen.com/subject/athractx.html