ant-design / antd-mobile-samples

antd-mobile samples
1.15k stars 1.09k forks source link

rn-custom-ui项目中主题更改不起作用 #62

Open suwu150 opened 6 years ago

suwu150 commented 6 years ago

克隆的是antd-mobile-samples直接运行的,运行后不起作用 image

- antd-mobile 版本:
    "dependencies": {
        "antd-mobile-rn": "next",
        "react": "^15.6.1",
        "react-native": "0.41.2"
    },

还有如下代码:

const path = require('path');
const fs = require('fs');

const defaultVars = require('antd-mobile-rn/lib/style/themes/default.native');
const customVars = require('../theme');
const themePath = path.resolve(require.resolve('antd-mobile-rn'), '../style/themes/default.native.js');

const themeVars = Object.assign({}, defaultVars, customVars);

if (fs.statSync(themePath).isFile()) {
  fs.writeFileSync(
    themePath,
    'var brandPrimary = "#108ee9"; var brandPrimaryTap = "#1284d6";module.exports = ' + JSON.stringify(themeVars)
  );
}

请问是不是还需要在其他地方进行配置?这种修改代码样式的原理是什么呢

suwu150 commented 6 years ago

请问这种还有其它方式进行更改样式吗

suwu150 commented 6 years ago

覆盖成功了,将antd-mobile-rn修改成antd-mobile,因为我的版本是2.1.8,在node_modules中还是antd-mobile形式的

const path = require('path');
const fs = require('fs');
// for 1.x
// const defaultVars = require('antd-mobile/lib/style/themes/default');
// for 2.x
const defaultVars = require('antd-mobile/lib/style/themes/default.native');
const customVars = require('../theme');
// for 1.x
// const themePath = path.resolve(require.resolve('antd-mobile'), '../style/themes/default.js');
// for 2.x
const themePath = path.resolve(require.resolve('antd-mobile'), '../style/themes/default.native.js');
const themeVars = Object.assign({}, defaultVars, customVars);

if (fs.statSync(themePath).isFile()) {
  fs.writeFileSync(
    themePath,
    'var brandPrimary = "#108ee9"; var brandPrimaryTap = "#1284d6";module.exports = ' + JSON.stringify(themeVars)
  );
}

我理解了一下,这种覆盖之后,你自己自定义的样式就是默认的样式,官方的样式已经不存在了,也就是说要想再次使用官方样式,你就得删掉node_modules并且重新install

warmhug commented 6 years ago

看这里 Object.assign({}, defaultVars, customVars) 只是覆盖你自己设置的同名样式,其他不覆盖

suwu150 commented 6 years ago

version: "antd-mobile": "^2.1.8", 我重新install之后,

const defaultVars = require('antd-mobile/lib/style/themes/default.native');

default.native中的default.native不见了,我修改为default.less之后,报这样的错误

/Users/jkwu/WebStormProject/FLS_MW_APP/node_modules/antd-mobile/lib/style/themes/default.less:1
(function (exports, require, module, __filename, __dirname) { @hd: 1px; // 基本单位
                                                              ^

SyntaxError: Invalid or unexpected token
    at createScript (vm.js:74:10)