编程式与行内式路由的选择问题——选择错误会造成传参失败!

文章介绍了在Vue.js应用中,当参数来自v-model绑定的表单时,必须使用编程式路由导航来传递参数。正确的方式是通过`router.push`进行传递。同时,文章详细对比了在接收参数时,使用`route.params`、props直接接收以及props函数式配置的三种方法,并展示了各自的代码示例。
摘要由CSDN通过智能技术生成

一、两者的选择问题

注意:当要传的参数是通过v-model绑定页面中的表单而获取的时候,必须使用编程式路由导航,否则传参为空!

setup:

const planInfo = {
  fromCity: "", //都是v-model绑定了页面的
  backCity: "",
  goDate: "",
};
错误传参方式:
<router-link
             :to="{
                  name: 'TrvalPlan',
                  params: {
                  fromCity: planInfo.fromCity,
                  backCity: planInfo.backCity,
                  goDate: planInfo.goDate,
                  },
                  }"
             target="_blank"
             class="thm-btn tour-search-one__btn"
             >
    制定行程
</router-link>
正确的传参方式:
<button
@click="createPlan"
class="thm-btn tour-search-one__btn"
>
    制定行程
</button>
const createPlan = () => {
  router.push({
    name: "TrvalPlan",
    params: {
      fromCity: planInfo.fromCity,
      backCity: planInfo.backCity,
      goDate: planInfo.goDate,
    },
  });
};

注意:在使用params传参时,必须使用命名式导航,即用name

二、使用参数的方法

1.用route接收并使用参数

注意:在router中useRoute和useRouter完全不一样,useRoute是用来接受参数的

在接受参数的页面中new route,然后使用params

//setup内:
const route = useRoute();
console.log(route.params.fromCity);
console.log(route.params);
image-20221209010029082

2.使用props接收并使用参数

2.1 写法1

传参文件中:

const planInfo = {
  fromCity: "",
  backCity: "",
  goDate: "",
};
const createPlan = () => {
  router.push({
    name: "TrvalPlan",
    params: planInfo,
  });
};

router配置文件中:

props: true,

接受参数的页面中:

props: ["fromCity", "backCity", "goDate"],
// 简单功能的实现
setup(props, context) {
    console.log(props);
}

结果:

image-20221209010151375
2.2 写法2

传参文件中:

const planInfo = {
  fromCity: "",
  backCity: "",
  goDate: "",
};
const createPlan = () => {
  router.push({
    name: "TrvalPlan",
    params: planInfo,
  });
};

router配置文件中:

props(route) {
    console.log("-------");
    console.warn(route);
    console.log("-------");
    return {
        fromCity: route.params.fromCity,
        backCity: route.params.backCity,
        goDate: route.params.goDate,
    };
},

接受参数的页面中:

props: ["fromCity", "backCity", "goDate"],
// 简单功能的实现
setup(props, context) {
    console.log(props);
}

结果:

image-20221209010151375
2.3 写法3

传参文件中:

const planInfo = {
  fromCity: "",
  backCity: "",
  goDate: "",
};
const createPlan = () => {
  router.push({
    name: "TrvalPlan",
    params: planInfo,
  });
};

router配置文件中:

props({ params: { fromCity, backCity, goDate } }) {
    return {
        fromCity,
        backCity,
        goDate,
    };
},

接受参数的页面中:

props: ["fromCity", "backCity", "goDate"],
// 简单功能的实现
setup(props, context) {
    console.log(props);
}

结果:

image-20221209010151375

使用:直接用这三项即可!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

鱼骨编程

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值