assets
├─css 重置样式表等
├─icons 生成的icon.js
├─image 图片库
└─svg 图标svg
assets/css/reset.css/* 1. 使用更直观的 box-sizing 模型 */ *, *::before, *::after { box-sizing: border-box; } /* 2. 移除默认 margin */ * { margin: 0; } /* 3. 在应用中允许基于百分比的高度 */ html, body { height: 100%; } /* 排版调整 4. 添加无障碍行高 5. 改进文本渲染 */ body { line-height: 1.5; -webkit-font-smoothing: antialiased; } /* 6. 改进媒体默认设置 */ img, picture, video, canvas, svg { /* display: block; */ max-width: 100%; } /* 7. 删除内置表单排版样式 */ input, button, textarea, select { font: inherit; } /* 8. 避免文字溢出 */ p, h1, h2, h3, h4, h5, h6 { overflow-wrap: break-word; } /* 9. 创建根层叠上下文 */ #root, #__next { isolation: isolate; }
样式表初始化主要目的是为了使代码在不同的游览器中表现一样, 所有需要来初始化默认样式。
vue-svgicon 本质会把 svg 图标解析成 js 文件,在项目中是通过 import 导入生成的 js 文件来达到对 svg 图标的引用
yarn add vue-svgicon@3.2.6 -D
assets/css/vue-svgicon.css/* recommended css code for vue-svgicon */ .svg-icon { display: inline-block; width: 16px; height: 16px; color: inherit; vertical-align: middle; fill: none; stroke: currentColor; text-align: center; } .svg-fill { fill: currentColor; stroke: none; } .svg-up { /* 默认 */ transform: rotate(0deg); } .svg-right { transform: rotate(90deg); } .svg-down { transform: rotate(180deg); } .svg-left { transform: rotate(-90deg); }
plugins/svg.jsimport "@/assets/css/vue-svgicon.css"; import Vue from "vue"; import SvgIcon from "vue-svgicon"; import "@/assets/icons"; Vue.use(SvgIcon, { tagName: "icon", });
plugins/index.js里导入import "./svg";
{ "scripts": { "serve": "vue-cli-service serve", "build": "vue-cli-service build", "svg": "vsvg -s ./src/assets/svg -t ./src/assets/icons" } }
这个脚本的意思是将src/assets/svg下的 svg 文件转为 js, 存储到src/assets/icons下
下载 svg 类型的文件保存到到assets/svg/下
yarn svg
yarn serve
此时, 还需要改造 router, 将 icon 添加上去,
跳到改造 router 继续学习吧
// router/modules/system.js 子应用的菜单 import Layout from "@/components/Layout"; const appRouter = { path: "/system", component: Layout, name: "System", meta: { title: "微应用", icon: "home", }, children: [ { path: "/system/", name: "Home", meta: { title: "微应用首页", icon: "home" }, }, { path: "/system/about", name: "About", meta: { title: "微应用关于", icon: "index" }, }, ], }; export default appRouter;
// router/index.js export const routes = [ { path: "/", name: "home", component: Layout, redirect: "/", meta: { title: "基座", icon: "base" }, children: [ { path: "/", name: "home", meta: { title: "基座首页", icon: "base" }, component: HomeView, }, { path: "/about", name: "about", meta: { title: "基座关于", icon: "nested" }, component: () => import(/* webpackChunkName: "about" */ "../views/AboutView.vue"), }, ], }, SystemRoute, ];
LayoutAsideMenuItem.vue<template>
<li class="layout-aside-menu-item">
<el-submenu v-if="item.children" :index="item.path">
<template slot="title">
<div class="menu-name">
<icon :name="item.meta && item.meta.icon" scale="0.8"></icon>
<span slot="title">{{ item.meta && item.meta.title }}</span>
</div>
</template>
<layout-aside-menu-item
v-for="subItem in item.children"
:item="subItem"
:key="subItem.path"
>
</layout-aside-menu-item>
</el-submenu>
<el-menu-item v-else :route="item.path" :index="item.path">
<template slot="title">
<div class="menu-name">
<icon :name="item.meta && item.meta.icon" scale="0.8"></icon>
<span slot="title">{{ item.meta && item.meta.title }}</span>
</div>
</template>
</el-menu-item>
</li>
</template>
<script>
import { mapGetters } from "vuex";
export default {
name: "LayoutAsideMenuItem",
props: ["item"],
data() {
return {};
},
computed: {
// ...mapGetters("layout", ["isCollapse"]),
},
methods: {},
created() {},
mounted() {},
};
</script>
<style lang="less" scoped>
.layout-aside-menu-item {
.svg-icon {
& + span {
margin-left: 10px;
}
}
.menu-name {
text-align: left;
}
}
</style>
主要是添加了 icon