解决初次使用webpack+antd-mobile时css不生效的问题

  前端这块,最火的是angular、react、vue。根据你具体的业务场景,选择合适的框架或者类库。以react为例,新建一个项目时,css

css组件按钮,图片轮播等组件,最好不要重复造轮子,选择业内规范的,拿来用就行。这里选了一个比较火的antd-mobile。相似移动端的html

bootstrap,可是UI组件要比bootstr强大丰富,且专一于结合react使用。前端

    具体使用,能够看官网教程:https://mobile.ant.design/docs/react/introduce。我直接拷贝过来:vue

 

  一、安装相关包node

$ npm install antd-mobile --save $

npm install babel-plugin-import --save-dev

 

二、在.babelrc 中加入:react

{"plugins": [["import", { "style": "css", "libraryName": "antd-mobile" }]]}

 

3.webpack.config.js里面css 加载写成以下(这里是重点,这一步没写好,css样式没法正常导入):webpack

在 webpack loaders 配置的时候须要把 css 和 cssmodules 分开处理,
并加上 exclude or include, 不去处理 am 样式 。git

{
    test: /\.css$/,
    loader: 'css?sourceMap&modules&localIdentName=[local]___[hash:base64:5]!!',
    exclude: /node_modules/
},
{
    test: /\.css$/, 
    loader: 'style!css'
},

 



4.模板页面设置以下:
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>xxxx</title>
    <script>!function(e){function t(a){if(i[a])return i[a].exports;var n=i[a]={exports:{},id:a,loaded:!1};return e[a].call(n.exports,n,n.exports,t),n.loaded=!0,n.exports}var i={};return t.m=e,t.c=i,t.p="",t(0)}([function(e,t){"use strict";Object.defineProperty(t,"__esModule",{value:!0});var i=window;t["default"]=i.flex=function(e,t){var a=e||100,n=t||1,r=i.document,o=navigator.userAgent,d=o.match(/Android[\S\s]+AppleWebkit\/(\d{3})/i),l=o.match(/U3\/((\d+|\.){5,})/i),c=l&&parseInt(l[1].split(".").join(""),10)>=80,p=navigator.appVersion.match(/(iphone|ipad|ipod)/gi),s=i.devicePixelRatio||1;p||d&&d[1]>534||c||(s=1);var u=1/s,m=r.querySelector('meta[name="viewport"]');m||(m=r.createElement("meta"),m.setAttribute("name","viewport"),r.head.appendChild(m)),m.setAttribute("content","width=device-width,user-scalable=no,initial-scale="+u+",maximum-scale="+u+",minimum-scale="+u),r.documentElement.style.fontSize=a/2*s*n+"px"},e.exports=t["default"]}]);
    flex(100, 1);</script>
</head>
<body>
    <div id="root"></div>
    <script>
        if(!window.Promise) {
            document.writeln('<script src="https://as.alipayobjects.com/g/component/es6-promise/3.2.2/es6-promise.min.js"'+'>'+'<'+'/'+'script>');
        }
    </script>
</body>
</html>

 

5.具体参考官网的demo。他们是怎么写的。重点注意css loader写法。参考来源:https://github.com/ant-design/babel-plugin-import/issues/61
相关文章
相关标签/搜索