【问题标题】:Vue2 page when to query url in lifecyleVue 2页面在生命周期中什么时候查询url
【发布时间】:2022-11-29 02:39:26
【问题描述】:

我有一个页面,如果用户来自某个 URL,我想在其中显示一条消息:

如果来自此 URL:

http://www.testapp.com?_a=lookingforthis&token=5b6b7f/account/about

<template>Show this message</template>

如果从这里: http://www.testapp.com/account/about

<template>Dont show this message</template>

我需要在 mounted() 或 method() 中执行此操作吗?

这是触发它的正确方法吗?

this.$route.query?.lookingforthis

【问题讨论】:

  • computed() 听起来不错。或者将条件内联到模板中。
  • @kissu 谢谢你。请问this.$route.query?.lookingforthis在这个例子中是否有效?
  • http://www.testapp.com?_a=lookingforthis&amp;token=5b6b7f/account/about 不是真的有效。特别是这里查询的是_a,而且路径在查询之后,应该是相反的。

标签: javascript vuejs2


【解决方案1】:

这样的事情应该运作良好

<template>
  <div>
    {{ showOrNotTheMessage }}
  </div>
</template>

<script>
export default {
  computed: {
    showOrNotTheMessage() {
      return this.$route.query?.lookingforthis
        ? 'Show this message'
        : 'Dont show this message'
    },
  },
}
</script>

http://localhost:3000/about?lookingforthis=TEST 之类的 URL 上触发。

如图所示,如果使用 Vue devtools

【讨论】:

    猜你喜欢
    • 2010-12-18
    • 1970-01-01
    • 2010-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-13
    • 1970-01-01
    相关资源
    最近更新 更多