【问题标题】:Vue Router - params not being passedVue路由器 - 未传递参数
【发布时间】:2018-04-24 09:20:17
【问题描述】:

我似乎无法使用编程导航传递参数,路径更改但有一个空对象参数。

main.js

import Vue from 'vue';
  import VueRouter from 'vue-router';

  import App from './App.vue';

  const Hello = { props: ['name'], 
  template: `<h1>Hello {{$route.params}}  </h1>`,

  };
  const World = { template: `<h1>World</h1>`};

  const routes = [
    { path: '/hello', component: Hello, props: true },
    { path: '/world', component: World }
  ];

  const router = new VueRouter({
    routes
  });

  Vue.use(VueRouter);

  new Vue({
    el: '#app',
    router,
    render: h => h(App)
  });

app.vue

<template>
  <div id="app">
   {{ msg }}
   <button @click="move">werwer</button>
       <router-view/>

  </div>
</template>

<script>
export default {
  name: 'app',
  data () {
    return {
      msg: 'Welcome to Your Vue.js App'
    }
  },
  mounted() {
  },
  methods : {
    move() {
    this.$router.push({ path: '/hello', params: { name: 'Paul' } })
    }
  }
}
</script>

【问题讨论】:

    标签: parameters vuejs2 router vue-router


    【解决方案1】:

    看来,这样做时,您必须使用命名路由。

    所以以编程方式推送

    this.$router.push({ name: 'hello', params: { name: 'Paul' }}) 
    

    在定义中

     const routes = [
        { name: 'hello', path: '/hello/', component: Hello, props: true },
        { path: '/world', component: World }
      ];
    

    【讨论】:

    • 是的!似乎只适用于带名称的推送,不适用于路径
    猜你喜欢
    • 2020-06-13
    • 2019-08-07
    • 1970-01-01
    • 2021-09-11
    • 1970-01-01
    • 2021-04-01
    • 1970-01-01
    • 2023-03-14
    • 2018-06-20
    相关资源
    最近更新 更多