为什么不使用路由变量更新vue

rn0zuynd  于 2023-05-18  发布在  Vue.js
关注(0)|答案(1)|浏览(116)

尝试创建面包屑,通过获取URL的完整路径并在数组中拆分,然后循环通过该数组来呈现一些面包屑。
问题是crumbs在路由改变时不会得到更新,我如何使它工作?

export default defineComponent({
  setup() {
    const store = useThemeStore();
    const { fullPath } = useRoute();

    return {
      changeThemeStore: computed(() => store.changeTheme),
      currentTheme: store.getCurrentTheme === "dark",
      crumbs: computed(() => fullPath.substring(1, fullPath.length).split('/'))
    };
  },
  methods: {
    changeTheme() {
      this.changeThemeStore();
    },
    goToRoute(crumb: string) {
      this.$router.push('/' + crumb)
    }
  },
i5desfxk

i5desfxk1#

尽量不破坏当前路由,直接使用它,似乎被破坏的对象失去了React性:

const route = useRoute();
...
 crumbs: computed(() => route.fullPath.substring(1, route.fullPath.length).split('/'))

相关问题