【问题标题】:How to maintain vue data changing url如何维护vue数据变化的url
【发布时间】:2019-11-19 05:18:07
【问题描述】:

我用Vuejs做了一个组件,里面有数据。
并且此 currentPage 值正在通过单击按钮进行更改。
例如,点击按钮,当前页面值为 3。

data(){
   return{
      currentPage: 1
   }
{

然后我会转到一个特定的网址,例如“/order”。之后,我回到之前的 url,它有 currentPage 数据。
但是我想在回来后将这个 currentPage 保持为 3 而不是 1。
但是当我转到另一个像“/希望”这样的网址并回来时,我希望 currentPage 像默认值一样是 1。
如何使用 vuejs 控制它?非常感谢您阅读。

【问题讨论】:

    标签: vue.js


    【解决方案1】:

    您可以使用您的值在商店中创建一个数组,例如

    urls: [{home: 1}, {details: 2}]
    

    当 url 更改时,只需从与您当前 url 相关的商店中获取值

    【讨论】:

      【解决方案2】:

      你可以使用类似 s 的东西

      <script>
      export default {
        ...
        data() {
          return{
            currentPage: 1
          }
        },
        created() {
          const currentPage = localStorage.getItem('currentPage')
          if (currentPage > 0) {
            this.currentPage = currentPage
          }
        },
        methods: {
          setCurrentPage(value) {
            this.currentPage = value
            localStorage.setItem('currentPage', value)
          }
        },
        ...
      }
      </script>

      【讨论】:

        猜你喜欢
        • 2017-11-14
        • 1970-01-01
        • 2021-06-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多