美文网首页
vue 小知识

vue 小知识

作者: 欢欣的膜笛 | 来源:发表于2020-03-27 01:24 被阅读0次
  • 监听数据变化 watch

    data() { 
      return { 
        cart: JSON.parse(localStorage.getItem("cart")) || []
      }
    },
    watch: {
      cart(n) { 
        localStorage.setItem("cart", JSON.stringify(n))
      }
    }
    // 深度监听
    watch: { 
      cart: { 
        handler(n) { 
          localStorage.setItem("cart", JSON.stringify(n))
        },
        deep: true
      }
    }
    
  • vue-router

    • 安装: vue add router
    • 路由配置
      Vue.use(VueRouter)
      const routes = [
      {
          path: '/',
          component: App,
          children: [
              {
                  path: '',
                  name: 'index',
                  component: index
              },
              {
                  path: 'product/:id',
                  name: 'product',
                  // 在对应组件内,props: ['id'] 即可直接使用,避免路由耦合
                  props: true,
                  // 路由独享
                  beforeEnter: (to, from, next) => {
                      if (XXX) {
                          next('/login')
                      } else {
                          next()
                      }
                  },
                  component: detail
              },
              {
                  path: '**',
                  name: 'index',
                  component: index
              }
          ]
        }
      ]
      // 路由实例
      const router = new VueRouter({
          routes,
          mode: 'history',
          strict: true,
          scrollBehavior (to, from, savedPosition) {
             if (savedPosition) {
                return savedPosition
             } else {
                if (from.meta.keepAlive) {
                    from.meta.savedPosition = document.body.scrollTop;
                }
              return { x: 0, y: to.meta.savedPosition ||0 }
            }
          }
      })
      
      // 每次路由激活之前都会执行回调函数
      router.beforeEach((to, from, next) => {
           // 判断是否登录 
           if (to.path.includes('detail') && XXX) {
               next('/login')
           } else {
               next()
           }
       })
      
      export default router
      
    • 路由守卫
      • 全局beforeEach(to,from,next)
      • 路由独享beforeEnter(to,from,next)
      • 组件beforeRouteEnter(to,from,next)
  • vuex

    • 安装 vue add vuex
    • 状态与变更
      export default new Vuex.Store({
          state: { count:0 }, 
          mutations: { add(state) { state.count++ } }
      })
      
    • 派生状态
       export default new Vuex.Store({ 
           getters: { 
               todoCount(state) { 
                  return state.todos.filter(todo=>!todo.completed).length 
               }
           }
       })
      
    • 异步操作
      export default new Vuex.Store({ 
          actions: { 
                // do something
                someAction(context) { 
                //  访问状态
                context.state;
                // 提交变更 
                context.commit();
                // 派发动作
                context.dispatch();
            }
          }
      })
      
    • 简化方法
      export default { 
          computed: { 
              ...mapState(['isLogin']), ...mapGetters(['loginState']) 
          },
          methods: { 
            ...mapActions(['login']) 
          }
      }
      

相关文章

  • 【Vue】常用指令之v-show

    ?【Vue】学习养成记,【程序员必备小知识】 ? 今日小知识——Vue常用指令之v-show 写在前面 Vue (...

  • 【Vue 】:visible.sync 的作用

    ?【Vue】学习养成记,【程序员必备小知识】 ? 今日小知识——Vue中的visible.sync的作用 1.写在...

  • 【Vue】常用指令之v-text

    ?【Vue】学习养成记,【程序员必备小知识】 ? 今日小知识——Vue常用指令之v-text 目录 1.v-tex...

  • 【Vue 】过滤器filters

    ?【Vue】学习养成记,【程序员必备小知识】 ? 今日小知识——Vue过滤器filters 1. 什么是过滤器? ...

  • 【Vue】常用指令之v-html

    ?【Vue】学习养成记,【程序员必备小知识】 ? 今日小知识——Vue常用指令之v-html v-html指令其实...

  • 【Vue】常用指令之v-bind

    ?【Vue】学习养成记,【程序员必备小知识】 ? 今日小知识——Vue常用指令之v-bind v-bind指令是专...

  • 【Vue】常用指令之v-on

    ?【Vue】学习养成记,【程序员必备小知识】 ? 今日小知识——Vue常用指令之v-on v-on指令是事件绑定指...

  • 【Vue】常用指令之v-if

    ?【Vue】学习养成记,【程序员必备小知识】 ? 今日小知识——Vue常用指令之v-if条件判断 1. v-if指...

  • 【Vue】常用指令v-if和v-show的区别

    ?【Vue】学习养成记,【程序员必备小知识】 ? 今日小知识——Vue指令v-if和v-show的区别 v-sho...

  • Vue小知识

    Vue基础知识 Vue框架作用 拓展html的功能,属性 vue是一套构建用户界面的 渐进式框架,Vue.js 的...

网友评论

      本文标题:vue 小知识

      本文链接:https://www.haomeiwen.com/subject/iyhvyhtx.html