vue零碎笔记

生命周期

export default {
    beforeCreate(){
        console.log("beforeCreate")
    },
    created(){
        console.log("created")
    },
    beforeMount(){
         console.log("挂载之前")
    },
    mounted(){
         console.log("挂载之后")
    },
    beforeUpdate(){
        console.log('更新之前')
    },
    updated(){
        console.log('更新之后')
    },
    beforeDestroy(){
        console.log('销毁之前')
    },
    destroyed(){
        console.log('销毁之后')
    }

}
// 路由守卫,时间节点
// 每次跳转之前触发
router.beforeEach((to,from,next)=>{
  console.log('每次跳转之前触发')
  // 需不需要继续跳转
  console.log(to)
  if(to.path=='/lifecom'){
    next('/nopage')
  }else{
    next()
  }

路由守卫

// 路由守卫,时间节点
// 每次跳转之前触发
router.beforeEach((to,from,next)=>{
  console.log('每次跳转之前触发')
  // 需不需要继续跳转
  console.log(to)
  if(to.path=='/lifecom'){
    next('/nopage')
  }else{
    next()
  }
  
})
export default {
    beforeRouteEnter(to,from ,next){
        console.log('组件路由进入之前')
        next()
    },
    beforeRouteUpdate(to,from ,next){
        //article/123==>article/456
        console.log('组件路由更新之前')
        next()
    },
    beforeRouteLeave(to,from ,next){
        console.log('组件路由离开之前')
        next()
    }
}

vuex

index.js

// 修改数据/状态的方法methods
  // payload就是commit调用时候的第二个参数
  mutations: {
    addAge(state,payload){
      // console.log(state)
      state.age += payload
    },
    getList(state,payload){
      state.list = payload
    }
// 异步修改数据
  actions: {
    // 去请求提供段子的服务器接口
    getJoke(context){
      let httpUrl = 'https://api.apiopen.top/getJoke?page=1&count=10&type=text'
      fetch(httpUrl).then((res)=>{
        return res.json()
      }).then((res)=>{
        console.log(res)
        context.commit('getList',res.result)
      })
    }

辅助函数

import { mapState,mapGetters,mapMutations,mapActions } from 'vuex

...mapActions(['getJoke']),
        ...mapMutations(['addAge','getList','setMallname']),
        ...mapMutations('luntan',['setLuntan'])
         ...mapState(['username','age','list','mall']),
        ...mapGetters(['xuAge']),
        ...mapState('luntan',['luntan'])

 <h1>姓名:{{$store.state.username}}</h1>
        <h2>年龄:{{$store.state.age}}</h2>
        <h2>虚岁:{{$store.getters.xuAge}}</h2>

        <h1>姓名:{{username}}</h1>
        <h2>年龄:{{age}}</h2>
        <h2>虚岁:{{xuAge}}</h2>

        <button @click="addAge(20)">添加老陈年龄</button>

vuex模块

export default {
    // namespaced:true,
    state:{
        mallname:"老陈商城"
    },
    mutations:{
        setMallname:function(state,payload){
            state.mallname = payload
        }
    }
}
     ...mapState('luntan',['luntan'])
     
        <h1 @click="setMallname('小陈商城')">{{mall.mallname}}</h1>
        <h1 @click="setLuntan('老王论坛')">{{luntan}}</h1>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值