【问题标题】:VueJS router $routes.push can't push query paramsVueJS 路由器 $routes.push 无法推送查询参数
【发布时间】:2019-06-16 17:04:13
【问题描述】:

为什么这不起作用?

selectChanged(val) {

        let term_ids = [];
        let taxonomy = '';

        val.forEach((obj) => {
          term_ids.push(obj.term_id);
          taxonomy = obj.taxonomy;
        });

        let obj = this.$route.query;
        obj[taxonomy] = term_ids.join(',');

        this.$router.push({
          query: obj,
        });

      },

obj 看起来像这样:

{education_levels: "33,36", candidate_countries: "304"}

如果我对上面的对象进行硬编码,它将按预期工作,VueJS 路由器将推送查询字符串,它看起来像这样:?education_levels=33,36&candidate_countries=304

但是如果我通过query: obj 什么都不会发生...

【问题讨论】:

    标签: vue.js vue-router


    【解决方案1】:

    你需要assignroute的所有属性。

    Object.Assign 复制 $route 的新副本,包括所有可枚举的 OWN 属性

    还有一个建议 -

    当您执行此操作时,let obj = this.$route.query; -
    Objects 被存储到 memory,因此当您尝试复制 object 时,您实际上是在复制 objectaddress记忆。 所以当你这样做时 - obj[taxonomy] = term_ids.join(','); 你会不小心改变原始值,所以需要克隆

    而是克隆它-

    let obj = { ... this.$route.query }
    this.$router.push({
       query: Object.assign({}, this.$route.query, obj),
    });
    

    【讨论】:

    • 不确定如何在我的示例中使用它。什么是查询?对象?请求参数?尝试查询:Object.assign({}, this.$route.query, obj),无济于事。
    • queryobj
    • 是的,谢谢 let obj = {};而不是让 obj = this.$route.query;和查询:Object.assign({}, this.$route.query, obj),做到了。
    • 很乐意提供帮助 :) 请回答接受。 - stackoverflow.com/help/someone-answers
    • 这种情况下 let obj = { ... this.$route.query } 和 just let obj = {} 有什么区别?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-12-14
    • 2021-09-06
    • 2021-08-23
    • 1970-01-01
    • 1970-01-01
    • 2023-03-27
    • 1970-01-01
    相关资源
    最近更新 更多