[聚焦Vue/React/Webpack]11 项目设计 - 图1组件和状态设计

  • 框架(vue/react)的使用(和高级特性)是必要条件
  • 独立负责项目?(考察设计设计能力)
  • 面试必考(二三面), 场景题
    • 数据驱动视图
    • 状态: 数据结构设计
    • 视图: 组件结构、拆分和组件通信

      React 设计todolist (组件结构, redux state 数据结构)

      设计原型图

      image.png

      state数据结构设计

      1. this.state = {
      2. list: [{
      3. id: 1,
      4. title: '标题'
      5. completedfalse
      6. }]
      7. }

      组件设计

      外层负责汇总, 顶部组件负责输入, 底部组件负责显示(列表显示, 元素显示)
      1. <App>
      2. <Input />
      3. <List>
      4. <ListItem/>
      5. <ListItem/>
      6. <ListItem/>
      7. </List>
      8. </App>

      Vue 设计购物车(组件机构, vuex state 数据结构)

      设计原型图

      image.png

      data数据结构

      1. {
      2. productionList: [{
      3. id: 1,
      4. title: '商品1'
      5. price: 10,
      6. }],
      7. cartList: [{
      8. id: 1,
      9. quantity: 10,
      10. }]
      11. }

      组件设计

      1. <App>
      2. <ProductionList>
      3. <ProductionListItem />
      4. <ProductionListItem />
      5. </ProductionList>
      6. <CartList>
      7. <CartItem />
      8. <CartItem />
      9. </CartList>
      10. </App>

      结合vuex实现购物车

  1. // store/index.js
  2. import cart from './modules/cart'
  3. import products from './modules/products'
  4. export default new Vuex.Store({
  5. modules: {
  6. cart,
  7. products
  8. },
  9. })
  1. // store/modules/index.js
  2. import shop from '../../api/shop'
  3. export default {
  4. namespaced: true,
  5. state: { all: [] },
  6. getters: {},
  7. actions: {
  8. getAllProducts ({ commit }) {
  9. shop.getProducts(products => {
  10. commit('setProducts', products)
  11. })
  12. }
  13. },
  14. mutations: {
  15. setProducts (state, products) {
  16. state.all = products
  17. },
  18. // 减少某一个商品的库存(够买一个,库存就相应的减少一个,合理)
  19. decrementProductInventory (state, { id }) {
  20. const product = state.all.find(product => product.id === id)
  21. product.inventory--
  22. }
  23. }
  24. }
  1. // api/shop.js
  2. export default {
  3. getProducts (cb) {
  4. setTimeout(() => cb(_products), 100)
  5. },
  6. }

🔚 2020-6