2024年最新《vue3+ts+element-plus 后台管理系统系列》之vuex4(1),2024年最新高级面试问题

下面是我在学习HTML和CSS的时候整理的一些笔记,有兴趣的可以看下:

HTML、CSS部分截图

进阶阶段

进阶阶段,开始攻 JS,对于刚接触 JS 的初学者,确实比学习 HTML 和 CSS 有难度,但是只要肯下功夫,这部分对于你来说,也不是什么大问题。

JS 内容涉及到的知识点较多,看到网上有很多人建议你从头到尾抱着那本《JavaScript高级程序设计》学,我是不建议的,毕竟刚接触 JS 谁能看得下去,当时我也不能,也没那样做。

我这部分的学习技巧是,增加次数,减少单次看的内容。就是说,第一遍学习 JS 走马观花的看,看个大概,去找视频以及网站学习,不建议直接看书。因为看书看不下去的时候很打击你学下去的信心。

然后通过一些网站的小例子,开始动手敲代码,一定要去实践、实践、实践,这一遍是为了更好的去熟悉 JS 的语法。别只顾着来回的看知识点,眼高手低可不是个好习惯,我在这吃过亏,你懂的。

1、JavaScript 和 ES6

在这个过程你会发现,有很多 JS 知识点你并不能更好的理解为什么这么设计,以及这样设计的好处是什么,这就逼着让你去学习这单个知识点的来龙去脉,去哪学?第一,书籍,我知道你不喜欢看,我最近通过刷大厂面试题整理了一份前端核心知识笔记,比较书籍更精简,一句废话都没有,这份笔记也让我通过跳槽从8k涨成20k。

JavaScript部分截图

2、前端框架

前端框架太多了,真的学不动了,别慌,其实对于前端的三大马车,Angular、React、Vue 只要把其中一种框架学明白,底层原理实现,其他两个学起来不会很吃力,这也取决于你以后就职的公司要求你会哪一个框架了,当然,会的越多越好,但是往往每个人的时间是有限的,对于自学的学生,或者即将面试找工作的人,当然要选择一门框架深挖原理。

开源分享:【大厂前端面试题解析+核心总结学习笔记+真实项目实战+最新讲解视频】

以 Vue 为例,我整理了如下的面试题。

Vue部分截图

import shop from ‘…/…/api/shop’

// initial state

const state = {

all: []

}

// getters

const getters = {}

// actions

const actions = {

getAllProducts ({ commit }) {

shop.getProducts(products => {

commit(‘setProducts’, products)

})

}

}

// mutations

const mutations = {

setProducts (state, products) {

state.all = products

},

decrementProductInventory (state, { id }) {

const product = state.all.find(product => product.id === id)

product.inventory–

}

}

export default {

namespaced: true,

state,

getters,

actions,

mutations

}

vuex4.x 分包(ts基础自动引入版)



目录:

在这里插入图片描述

分包的时候我们往往需要手动在index 文件导入分包模块,这样比较麻烦多人开发时候也容易冲突。

利用webpack require 可以自动导入modules 文件,简化操作。

modules > index.ts 代码:

// require.context 适合在同时引用一个目录下多个文件的问题

// require.context函数接受三个参数(路径,是否遍历文件子目录,匹配文件的正则)

// directory {String} -读取文件的路径

// useSubdirectories {Boolean} -是否遍历文件的子目录

// regExp {RegExp} -匹配文件的正则

const files = require.context(‘.’, true, /.ts$/)

const modules: any = {}

// 遍历取出的file,查找model 给const modules。

files.keys().forEach(key => {

if (key === ‘./index.ts’) return

const path = key.replace(/(./|.ts)/g, ‘’)

const [namespace] = path.split(‘/’)

// namespace 为modules 下的文件名

// if (!modules[namespace]) {

// modules[namespace] = {

// namespace: true

// }

// }

modules[namespace] = { namespaced: true }

modules[namespace].actions = files(key).default.actions

modules[namespace].mutations = files(key).default.mutations

modules[namespace].state = files(key).default.state

modules[namespace].getters = files(key).default.getters

console.log(modules)

})

export default modules

index.ts 代码动态加载moudles里面的模块:

import { createStore } from ‘vuex’

import modules from ‘./modules’

// 使用VUEx 存储

export default createStore({

// vuex的基本数据,用来存储变量

state: { a: 1 },

// 提交更新数据的方法,必须是同步的(如果需要异步使用action)。

mutations: {},

actions: {},

// 从基本数据(state)派生的数据,相当于state的计算属性

getters: {},

// 模块化vuex,可以让每一个模块拥有自己的state、mutation、action、getters

modules: {

…modules

}

})

modules 的模块代码示例:

import { UserModel } from ‘@/views/global/UserModel’

import { Convert } from ‘@/utils/jsonToModel’

import storage, { StorageType } from ‘@/utils/storage’

// import RCStorage from ‘@/utils/storage’

const actions = {}

// 修改状态

const mutations = {

// Vuex提供了commit方法来修改状态 使用时 $store.commit(‘handleUserName’,name)

SET_USER: (state: any, user: UserModel) => {

state.token = user

console.log(user)

// 把登录的用户的名保存到localStorage中,防止页面刷新,导致vuex重新启动,(初始值为空)的情况

// RCStorage.shared()

storage.rcSetItem(StorageType.local, ‘user’, Convert.modelToJson(user))

}

}

// 定义基本属性

const state = {

user: ‘’ || storage.rcGetItem(StorageType.local, ‘user’)

}

// getters 只会依赖 state 中的成员去更新

const getters = {

// 尖头函数

USER_GET: (state: any): UserModel => {

console.log(state.user)

if (state.user) {

return Convert.jsonToModel(state.user)

}

const tempModel: UserModel = {

token: ‘’,

loginType: ‘’,

loginAccount: ‘’,

name: ‘’,

phoneNumber: ‘’

}

return tempModel

}

}

// 使用VUEx 存储

export default {

namespaced: true,

// vuex的基本数据,用来存储变量

state,

// 提交更新数据的方法,必须是同步的(如果需要异步使用action)。

mutations,

actions,

// 从基本数据(state)派生的数据,相当于state的计算属性

getters

}

vuex4.x 分包(ts vuex-module-decorators版)



借助 vuex-module-decorators库,将vuex 使用class话,可以优雅避免硬编码。(以上版本使用逃不开硬编码)

代码示例:传送门

index.ts 代码:

import Vue from ‘vue’

import Vuex from ‘vuex’

import { IAppState } from ‘./modules/app’

import { IUserState } from ‘./modules/user’

import { ITagsViewState } from ‘./modules/tags-view’

import { IErrorLogState } from ‘./modules/error-log’

import { IPermissionState } from ‘./modules/permission’

import { ISettingsState } from ‘./modules/settings’

Vue.use(Vuex)

export interface IRootState {

app: IAppState

user: IUserState

tagsView: ITagsViewState

errorLog: IErrorLogState

permission: IPermissionState

settings: ISettingsState

}

// Declare empty store first, dynamically register all modules later.

export default new Vuex.Store({})

** 以 app.ts 模块代码为例:**

import { VuexModule, Module, Mutation, Action, getModule } from ‘vuex-module-decorators’

import { getSidebarStatus, getSize, setSidebarStatus, setLanguage, setSize } from ‘@/utils/cookies’

import { getLocale } from ‘@/lang’

import store from ‘@/store’

export enum DeviceType {

Mobile,

Desktop,

}

export interface IAppState {

device: DeviceType

sidebar: {

opened: boolean

withoutAnimation: boolean

}

language: string

size: string

}

@Module({ dynamic: true, store, name: ‘app’ })

class App extends VuexModule implements IAppState {

public sidebar = {

opened: getSidebarStatus() !== ‘closed’,

withoutAnimation: false

}

public device = DeviceType.Desktop

public language = getLocale()

public size = getSize() || ‘medium’

@Mutation

private TOGGLE_SIDEBAR(withoutAnimation: boolean) {

this.sidebar.opened = !this.sidebar.opened

this.sidebar.withoutAnimation = withoutAnimation

if (this.sidebar.opened) {

setSidebarStatus(‘opened’)

} else {

setSidebarStatus(‘closed’)

}

}

@Mutation

private CLOSE_SIDEBAR(withoutAnimation: boolean) {

this.sidebar.opened = false

this.sidebar.withoutAnimation = withoutAnimation

setSidebarStatus(‘closed’)

}

@Mutation

private TOGGLE_DEVICE(device: DeviceType) {

this.device = device

}

@Mutation

private SET_LANGUAGE(language: string) {

this.language = language

setLanguage(this.language)

}

@Mutation

private SET_SIZE(size: string) {

this.size = size

setSize(this.size)

}

@Action

public ToggleSideBar(withoutAnimation: boolean) {

this.TOGGLE_SIDEBAR(withoutAnimation)

}

@Action

public CloseSideBar(withoutAnimation: boolean) {

this.CLOSE_SIDEBAR(withoutAnimation)

}

@Action

public ToggleDevice(device: DeviceType) {

this.TOGGLE_DEVICE(device)

}

@Action

public SetLanguage(language: string) {

this.SET_LANGUAGE(language)

}

@Action

public SetSize(size: string) {

this.SET_SIZE(size)

}

}

export const AppModule = getModule(App)

使用:

import { AppModule, DeviceType } from ‘@/store/modules/app’

// state

AppModule.device

//actions

AppModule.CloseSideBar(false)

vuex4.x 分包(ts 类型推断type版)



这种方式是我比较喜欢,也是项目在用的,文件划分清晰,使用可读性高,类型推断好。

代码 demo地址:传送门

整体目录:

在这里插入图片描述

index.ts代码:

vuex index基操,另外做了分包和类型相关的活。

注意:使用一定要用这里的useStore 函数。

最后

开源分享:【大厂前端面试题解析+核心总结学习笔记+真实项目实战+最新讲解视频】

大厂面试问深度,小厂面试问广度,如果有同学想进大厂深造一定要有一个方向精通的惊艳到面试官,还要平时遇到问题后思考一下问题的本质,找方法解决是一个方面,看到问题本质是另一个方面。还有大家一定要有目标,我在很久之前就想着以后一定要去大厂,然后默默努力,每天看一些大佬们的文章,总是觉得只有再学深入一点才有机会,所以才有恒心一直学下去。

  • 20
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值