每天忙碌的日子,也不要忘记了好好生活 🏠

vuePress-theme-reco happylay 🐑    2020 - 2021
每天忙碌的日子,也不要忘记了好好生活 🏠 每天忙碌的日子,也不要忘记了好好生活 🏠

Choose mode

  • dark
  • auto
  • light
主页
分类
  • 手册
  • 前端
  • 后端
  • 工作
  • 相册
  • 文档
标签
时间轴
文档
  • 轨迹
个人空间
  • 哔哩哔哩
  • 编辑博客
工具集
  • 后端工具

    • 在线json解析
    • yml格式转换
    • websocket测试
    • 时间戳转换
    • cron表达式
    • linux程序包
    • 大小写转换
    • toml格式转换
  • 后端框架

    • Spring
    • GoFrame
  • 前端工具

    • Vant移动端组件库
    • Element桌面端组件库
    • uni-app移动端框架
    • uview移动端框架
    • colorui2.0文档
    • Figma
    • Codepen
    • Dribbble
    • Iconfont阿里矢量图库
    • IconPark图标库
    • Icomoon
    • Remixicon
    • favicon图标制作
  • 开发环境

    • windows包管理器-baulk
    • windows包管理器-scoop
    • windows原版镜像
    • nexus3仓库
  • 微服务

    • 版本兼容关系
    • k8s在线配置
    • k8s接口文档
GitHub
author-avatar

happylay 🐑

34

文章

24

标签

主页
分类
  • 手册
  • 前端
  • 后端
  • 工作
  • 相册
  • 文档
标签
时间轴
文档
  • 轨迹
个人空间
  • 哔哩哔哩
  • 编辑博客
工具集
  • 后端工具

    • 在线json解析
    • yml格式转换
    • websocket测试
    • 时间戳转换
    • cron表达式
    • linux程序包
    • 大小写转换
    • toml格式转换
  • 后端框架

    • Spring
    • GoFrame
  • 前端工具

    • Vant移动端组件库
    • Element桌面端组件库
    • uni-app移动端框架
    • uview移动端框架
    • colorui2.0文档
    • Figma
    • Codepen
    • Dribbble
    • Iconfont阿里矢量图库
    • IconPark图标库
    • Icomoon
    • Remixicon
    • favicon图标制作
  • 开发环境

    • windows包管理器-baulk
    • windows包管理器-scoop
    • windows原版镜像
    • nexus3仓库
  • 微服务

    • 版本兼容关系
    • k8s在线配置
    • k8s接口文档
GitHub
  • 2020

    • api 接口服务
    • dom渲染总结
    • eslint代码规范
    • promise 异步总结
    • scss总结
    • typescript 总结
    • vue2 常用配置
    • vue2总结
    • vue3总结
    • vuex总结
    • 响应式布局
    • 基础样式
    • 常用js写法
    • 常用npm类库
    • 常用样式
    • 样式卡片

常用js写法

vuePress-theme-reco happylay 🐑    2020 - 2021

常用js写法

happylay 🐑 2020-12-26 代码片段模块化

摘要: 常用 js 写法 时间: 2020-12-26


# 异步请求改同步

异步

open () {
  this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    this.$message({
      type: 'success',
      message: '删除成功!'
    })
  }).catch(() => {
    this.$message({
      type: 'info',
      message: '已取消删除'
    })
  })
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

同步

async open () {
  const res = await this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).catch(err => err)

  console.log(res)
}
1
2
3
4
5
6
7
8
9

# vuex 模块化

import Vue from "vue";
import Vuex from "vuex";
import getters from "./getters";

Vue.use(Vuex);

// 读取文件的路径,读取文件的路径,匹配文件的正则
const modulesFiles = require.context("./modules", true, /\.js$/);

// -----------------------参数解析-------------------------------
console.dir(modulesFiles);
console.log(modulesFiles);
console.log("1.id:", modulesFiles.id);
console.log("2.数组[模块名]:", modulesFiles.keys());
console.log("3.解析:", modulesFiles.resolve(modulesFiles.keys()[0]));
console.log("4.模块:", modulesFiles(modulesFiles.keys()[0]));

// 它将自动从模块文件中导入所有的vuex模块
const modules = modulesFiles.keys().reduce((modules, modulePath) => {
  console.log("原始模块", modules, "模块路径", modulePath);

  // 获取模块名称 './app.js' => 'app'模块名
  const moduleName = modulePath.replace(/^\.\/(.*)\.\w+$/, "$1");

  // 传入模块相对路径,返回一个模块 './app.js' => app模块
  const value = modulesFiles(modulePath);

  // 获取模块内容 state,actions 等等
  modules[moduleName] = value.default;

  return modules;
}, {});

console.log("数组[完整模块内容]", modules);

const store = new Vuex.Store({
  modules,
  getters,
});

export default store;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41

# require.context()使用

目录结构

├── App.vue
├── assets
│   └── logo.png
├── components
│   ├── common.vue
│   └── global
│       ├── demo.vue
│       └── index.js
├── main.js
├── router
│   ├── common.js
│   ├── index.js
│   └── modules
│       ├── demo.js
│       ├── home.js
│       └── index.js
└── views
    ├── 403.vue
    ├── 404.vue
    ├── Frame.vue
    ├── demo.vue
    └── index.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

# 自动导入路由

router/index.js

import Vue from "vue";
import VueRouter from "vue-router";
import RouterConfig from "./modules"; // 引入业务逻辑模块
import CommonRouters from "./common"; // 引入通用模块

Vue.use(VueRouter);

const router = new VueRouter({
  mode: "history", // 需要服务端支持
  scrollBehavior: () => ({ y: 0 }),
  routes: RouterConfig.concat(CommonRouters),
});

console.log(router);

router.beforeEach((to, from, next) => {
  console.log(to);
  next();
});

export default router;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

router/common.js

export default [
  // 默认页面
  {
    path: "/",
    redirect: "/index",
    hidden: true,
  },
  // 无权限页面
  {
    path: "/nopermission",
    name: "nopermission",
    component: () => import("@/views/403"),
  },
  // 404
  {
    path: "*",
    name: "lost",
    component: () => import("@/views/404"),
  },
];
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

router/modules/index.js

const files = require.context(".", true, /\.js$/);

// ["./home.js"] 返回一个数组
console.log(files.keys());
let configRouters = [];

files.keys().forEach((key) => {
  if (key === "./index.js") {
    return;
  }
  // 读取出文件中的default模块
  configRouters = configRouters.concat(files(key).default);
});

// 模块排序
configRouters.sort((a, b) => a.sort - b.sort);

console.log(configRouters);

// 抛出一个Vue-router期待的结构的数组
export default configRouters;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

router/modules/home.js

import Frame from "@/views/Frame";
import Home from "@/views/index";
export default [
  // 首页
  {
    path: "/index",
    name: "首页",
    redirect: "/index",
    component: Frame,
    sort: 1,
    children: [
      // 嵌套路由
      {
        path: "",
        component: Home,
      },
    ],
  },
];
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

router/modules/demo.js

import demo from "@/views/demo";
export default [
  // 首页
  {
    path: "/demo",
    name: "测试",
    redirect: "/demo",
    component: demo,
    sort: 2,
  },
];
1
2
3
4
5
6
7
8
9
10
11

# 自动导入全局组件

components/global/index.js

import Vue from "vue";
const contexts = require.context(".", false, /\.vue$/);
contexts.keys().forEach((component) => {
  const componentEntity = contexts(component).default;
  console.log("组件名称", componentEntity.name);
  // 使用内置的组件名称 进行全局组件注册
  Vue.component(componentEntity.name, componentEntity);
});
1
2
3
4
5
6
7
8

components/global/demo.vue

<template>
  <div>
    <h1>我是组件</h1>
    <slot></slot>
  </div>
</template>

<script>
export default {
  name: "demo",
  data() {
    return {};
  },
  mounted() {},
  methods: {},
};
</script>

<style lang="scss" scoped></style>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

src/main.js

import Vue from "vue";
import App from "./App.vue";
import router from "./router";
import "@/components/global";

Vue.config.productionTip = false;

new Vue({
  router,
  render: (h) => h(App),
}).$mount("#app");
1
2
3
4
5
6
7
8
9
10
11

# 标准树结构

<template>
  <div>
    <el-tree
      :data="data"
      show-checkbox
      default-expand-all
      node-key="id"
      highlight-current
      :default-expanded-keys="[1]"
      :default-checked-keys="defaultChecked"
      :props="defaultProps"
      draggable
      @node-drag-end="dragEnd"
    >
      <span class="custom-tree-node" slot-scope="{ node, data }">
        <span>{{ node.label }}</span>
        <span>
          <el-button type="text" size="mini" @click="() => append(data)">
            新增
          </el-button>
          <el-button type="text" size="mini" @click="() => remove(node, data)">
            删除
          </el-button>
        </span>
      </span>
    </el-tree>
  </div>
</template>

<script>
export default {
  methods: {
    dragEnd(draggingNode, dropNode, dropType, ev) {
      console.log("自身id", draggingNode.key, "父节点id", dropNode.parent.key);
    },
    append(data) {
      console.log(data);
    },
    remove(node, data) {
      console.log(node, data);
    },
  },

  data() {
    return {
      data: [
        {
          id: 1,
          label: "一级菜单",
          children: [
            {
              id: 2,
              label: "二级菜单",
              children: [
                {
                  id: 3,
                  label: "三级菜单",
                },
                {
                  id: 4,
                  label: "三级菜单",
                },
              ],
            },
          ],
        },
      ],
      defaultProps: {
        children: "children",
        label: "label",
      },
      defaultChecked: [4],
    };
  },
};
</script>

<style lang="scss" scoped>
.custom-tree-node {
  flex: 1;
  display: flex;
  justify-content: space-between;
}
</style>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84

# 🌈 彩蛋

vscode 代码块左移 cmd + [ ,代码块右移 cmd +]

vscode 取消撤销 shift + cmd + z

npm、yarn 更新项目依赖

说明 yarn npm-check
更新项目依赖,没有交互 yarn upgrade --latest npm-check -y
更新项目依赖,有交互 yarn upgrade-interactive --latest npm-check -u
更新全局依赖,没有交互 yarn global upgrade --latest npm-check -g -y
更新全局依赖,有交互 yarn global upgrade-interactive --latest npm-check -g -u
编辑文档
最后一次更新: 2021/2/2 下午12:34:17
欢迎来到小屋。
看板娘