vue组件内监听路由变化

需求分析:首頁面有两个组件,Artist组件和Tag组件,分别对应文章组件和标签组件,点击Tag组件的某项标签,要动态更新Artist组件里的内容,并且路由要传递查询信息


首先,要根据Tag组件选中的tid去动态更新articles数据,如何去实现呢?
这时候我想到的方法:是把articles数据放在vuex上实现组件共享,点击标签后,Tag组件直接异步请求articles数据,并更新Vuex上的articles,然后再更新路由,同时设置Article组件的articles为Computed属性,这样就可以动态更新页面数据了

store.js
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
const store=new Vuex.Store({
    /*为什么共享文章:因为方便tag页面操作文章,点击不同tag,更新不同文章*/
    state:{
        articles:{
            artContent:[],
            total:0,
        },
    },
    mutations:{
        initArticles(state,obj){
            state.articles.artContent=obj.data;
            state.articles.total=obj.total;
        }
    },
})
export default  store

Article.vue
import store from "@/store";
    export default {
        name: "Article",
        data() {
            return {
                currentPage:1,
            }
        },
        computed:{
            articles(){
                return store.state.articles
            }
        },
        mounted(){ 
                this.initAllArticles()
            }
        },
        
        methods: {
            initAllArticles() {
                this.getRequest('/home/getallarticles?pageId='+this.currentPage).then(resp => {
                    if (resp) {
                        store.commit("initArticles",resp)
                    }
                })
            },     

Tag.vue
getArticlesByTag(tid,tagName)
            {
                this.getRequest('/home/getArticleByTag?tId='+tid+'&pageId='+1).then(resp => {
                    if (resp) {
                        store.commit("initArticles",resp)
                    }
                })
                this.$router.push({path:"/home/getArticleByTag",query:{tid:tid,tagName:tagName}})
            }

问题:
这样做会带来两个问题:
一:路由传递的信息没有充分利用
二:页面后退后,文章列表更新不回全部文章,因为你点击分类文章,更新了Vuex里的articles数据,回退后,Article组件由始至终没有被重新挂载,在mounted里定义的数据请求方法调用不了,自然数据也更新不了。

解决方法:所以要对路由进行监听,充分利用路由里传递的数据
添加watch属性,监听路由
 watch:{
            $route(to,from){
                   if(to.path=='/home')
                       this.initAllArticles()
                if(to.path=='/home/getArticleByTag')
                    this.initTagArticles()
            }
        },

这样路由前进后退,都会进行不同的数据请求方法来重新获得各自的数据,
如果路由为/home,请求全部文章,如果为/home/getArticleByTag则请求分类文章(实际不必如此分开两个方法进行请求,可以为一个请求然后根据传递的参数进行sql查询,在后端进行兼容就行了,这里只是为了方便理解)

并且Tag组件不用再进行数据请求,统一由Atricle组件自己更新自己的数据,也不必再把articles数据存入Vuex。
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue3通过`watch` API和`router`实例的`currentRoute`属性来监听路由变化并完成刷新。 在Vue3中,我们可以使用`watch` API来监听特定的数据变化,并在数据变化时执行相应的操作。而在Vue3中,`router`实例的`currentRoute`属性表示当前的路由信息,我们可以利用`watch` API来监听这个属性的变化。 具体来说,我们可以在Vue组件的`setup()`函数中使用`watch` API来监听`router.currentRoute`,并在回调函数中执行刷新操作。例如,我们可以创建一个新的Vue组件监听路由变化并在路由变化时执行刷新: ```javascript import { onBeforeMount, onUnmounted, watch } from 'vue' import { useRoute, useRouter } from 'vue-router' export default { setup() { const route = useRoute() const router = useRouter() const handleRouteChange = () => { // 执行刷新操作 console.log('路由变化,执行刷新操作') } onBeforeMount(() => { // 在组件挂载前开始监听路由变化 watch(() => route.currentRoute, handleRouteChange) }) onUnmounted(() => { // 在组件卸载时停止监听路由变化 unwatch(handleRouteChange) }) } } ``` 上述代码中,我们使用`useRouter`和`useRoute`来获取`router`实例和当前路由信息。然后,在组件挂载前使用`watch` API来监听`route.currentRoute`的变化,当路由变化时会执行回调函数`handleRouteChange`中的刷新操作。在组件卸载时,我们可以通过`unwatch`函数来停止监听。 这样,当Vue3的路由发生变化时,我们就可以监听路由变化并执行相应的刷新操作了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值