所谓定制呢,就是将封装一些跟页面整体设计色值或者特殊设计相关的通用的颜色或者圆角等信息封装成变量然后存储成一个文件,这样我们就可以直接引用文件,使用变量,从而降低代码冗余度
https://www.antdv.com/docs/vue/customize-theme-cn/
这是Antd已经封装好的所有的变量,可以直接使用
https://github.com/vueComponent/ant-design-vue/blob/master/components/style/themes/default.less
vue.config.js
less: {
modifyVars: {
// 配置主题
'primary-color': '#1DA57A',
// 链接颜色
'link-color': '#1DA57A',
// 圆角设计
'border-radius-base': '2px'
},
javascriptEnabled: true
}
const path = require("path"); // 引入path模块
const AntDesignThemePlugin = require("antd-theme-webpack-plugin");
const options = {
antDir: path.join(__dirname, "./node_modules/ant-design-vue"),
stylesDir: path.join(__dirname, "./src"),
varFile: path.join( // 使用默认ant提供的默认的变量库
__dirname,
"./node_modules/ant-design-vue/lib/style/themes/default.less"
),
mainLessFile: "",
themeVariables: ["@primary-color"],
generateOnce: false,
customColorRegexArray: [] // An array of regex codes to match your custom color variable values so that code can identify that it's a valid color. Make sure your regex does not adds false positives.
};
// 实例化一个主题对象
const themePlugin = new AntDesignThemePlugin(options);
module.exports = {
configureWebpack: {
plugins: [themePlugin]
},
// 使用插件
}
<link rel="stylesheet/less" type="text/css" href="/color.less" />
<script>
window.less = {
async: false,
env: 'production',
javascriptEnabled: true,
modifyVars: {
// 配置默认主题
"primary-color": "#1DA57A"
},
};
script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/less.js/2.7.2/less.min.js">script>
window.less.modifyVars({'primary-color':'red'})
// 页面主题色是不是变了
// 但是依旧有一个问题:我们把抽屉的按钮写死了,所以要做以下调整
<div class="setting-handle" @click="visible = !visible">div>
<style lang="less">
@import url("./index.less");
style>
/* 引入默认主题的less */
@import '~ant-design-vue/lib/style/themes/default.less'
.setting-handle {
position: absolute;
top: 300px;
right: 268px;
background-color: saddlebrown;
height: 40px;
width: 40px;
line-height: 40px;
text-align: center;
background-color: @primary-color;
border-radius: 5px;
color: white;
}
import {
LocaleProvider
} from “ant-design-vue”;
// 使用
Vue.use(LocaleProvider)
- App.vue
```html
<script>
import zhCN from "ant-design-vue/lib/locale-provider/zh_CN";
import enUS from "ant-design-vue/lib/locale-provider/en_US";
export default {
data() {
return {
locale: zhCN
};
},
watch: {
// 通过路由监听来切换语言
"$route.query.locale": function(val) {
this.locale = val === "enUS" ? enUS : zhCN;
}
}
};
</script>
// 这是针对Antd自己的组件的国际化方法,但是比如日期,日历组件,使用第三方moment.js,无法像自己组件一样的随意切换,所以需要额外处理
<div id="app">
<LocaleProvider :locale="locale">
<router-view>router-view>
LocaleProvider>
div>
<script>
import zhCN from "ant-design-vue/lib/locale-provider/zh_CN";
import enUS from "ant-design-vue/lib/locale-provider/en_US";
import moment from "moment"; // 处理第三方库的国际化
export default {
data() {
return {
locale: zhCN
};
},
watch: {
// 通过路由监听来切换语言
"$route.query.locale": function(val) {
this.locale = val === "enUS" ? enUS : zhCN;
moment(val === "enUS" ? enUS : zhCN);
}
}
};
</script>
<a-dropdown>
<a class="ant-dropdown-link" @click="e => e.preventDefault()">
<a-icon style="font-size:28px; margin-right:20px" type="global" />
a>
<a-menu
slot="overlay"
@click="localeChange"
:selectedKeys="[$route.query.locale || 'zhCN']"
>
<a-menu-item key="zhCN">
<a href="javascript:;">中文a>
a-menu-item>
<a-menu-item key="enUS">
<a href="javascript:;">Englisha>
a-menu-item>
a-menu>
a-dropdown>
<script>
export default {
data() {
return {};
},
methods: {
localeChange(obj) {
this.$router.push({ query: { locale: obj.key } });
}
}
};
</script>
// 此时你点击切换语言就可以通过路由看到切换成功了
// 怎么验证呢?
// 引入一个日期组件看看
// 是不是你会发现,中英文切换成功了
export default {
// 注意这个key哦
"app.dashboard.analysis.timeLable": "时间"
};
export default {
// 注意这个key哦
"app.dashboard.analysis.timeLable": "Time"
};
// 新建的语言包
import enUS from "./locale/enUS";
// 新建的语言包
import zhCN from "./locale/zhCN";
import VueI18n from "vue-i18n";
import queryString from "query-string";
// 注册国际化插件
Vue.use(VueI18n);
const i18n = new VueI18n({
locale: queryString.parse(location.search).locale || "zhCN", // 解析路由地址看看有没有语言切换标志
messages: {
zhCN: { message: zhCN },
enUS: { message: enUS }
}
});
new Vue({
i18n,// 挂在到vue对象上方便使用
render: h => h(App)
}).$mount("#app");
localeChange(obj) {
this.$router.push({ query: { locale: obj.key } });
// 切换路由的同时,告诉i18n你选的语言
this.$i18n.locale = obj.key;
}
{{ $t("message")["app.dashboard.analysis.timeLable"] }}
// 图标的按需加载
export { default as SettingOutline } from "@ant-design/icons/outline/SettingOutline";
configureWebpack: {
resolve: {
alias: {
"@ant-design-vue/lib/icon$": path.resolve(__dirname, "./src/icons.js")
}
}
},
const webpack = require("webpack"); // 引入webpack
// 添加插件
configureWebpack: {
plugins: [new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/)],
},
import "moment/locale/zh-cn"; // 引入中文包,默认是英文的不必理会
import echarts from "echarts/lib/echarts";
// 用了柱状图,就只引入柱状图
import "echarts/lib/chart/bar";
import "echarts/lib/component/title";
// 导出组件代码字符串
import chartCode from "!!raw-loader!@/components/chart/Chart";
export default {
data() {
return {
chartCode, //chart代码字符串
}
}
import VueHighlightJS from "vue-highlightjs";
Vue.use(VueHighlightJS);
<pre v-highlightjs="chartCode"><code class="html">code>pre>
import 'highlight.js/styles/github.css';
module.exports = {
moduleFileExtensions: ["js", "jsx", "json", "vue"],
transform: {
"^.+\\.vue$": "vue-jest",
".+\\.(css|styl|less|sass|scss|svg|png|jpg|ttf|woff|woff2)$":
"jest-transform-stub",
"^.+\\.jsx?$": "babel-jest"
},
transformIgnorePatterns: ["/node_modules/"],
moduleNameMapper: {
"^@/(.*)$": "/src/$1"
},
snapshotSerializers: ["jest-serializer-vue"],
testMatch: ["**/*.spec.(js|jsx|ts|tsx)|**/__tests__/*.(js|jsx|ts|tsx)"],
testURL: "http://localhost/",
collectCoverage: process.env.COVERAGE === "true", // 是否生成测试报告
collectCoverageFrom: ["src/**/*.{js,vue}", "!**/node_modules/**"] // 测试覆盖
};
env: {
jest: true
},
// 获取权限
const currentAuth = ["admin"];
// 导出当前权限
export { currentAuth };
export function getCurrentAuthority() {
// 这里返回的权限应该是从后端读取回来的,此时用admin替代
return currentAuth;
}
// 鉴权
export function check(authority) {
const current = getCurrentAuthority();
return current.some(item => authority.includes(item));
}
// 判断是否登陆
export function isLogin() {
const current = getCurrentAuthority();
return current && current[0] !== "guest";
}
import { check, currentAuth } from "@/auth";
describe("auth test", () => {
// 测试没权限
it("empty auth", () => {
currentAuth.splice(0, currentAuth.length);
expect(check(["user"])).toEqual(false);
expect(check(["admin"])).toEqual(false);
});
it("user auth", () => {
// 测试user
currentAuth.splice(0, currentAuth.length);
currentAuth.push("user");
expect(check(["user"])).toEqual(true);
expect(check(["admin"])).toEqual(false);
});
it("admin auth", () => {
// 测试admin和user
currentAuth.push("admin");
expect(check(["user"])).toEqual(true);
expect(check(["admin"])).toEqual(true);
expect(check(["user", "admin"])).toEqual(true);
});
});
github.io
gitee.io
https://www.netlify.com
https://github/apps/close-issue-app
欢迎加微信一起学习:13671593005
未完待续…