【问题标题】:Vue passing props to route componentsVue 传递 props 来路由组件
【发布时间】:2019-10-20 18:20:55
【问题描述】:

我正在尝试通过路由器将数据从 Vue 实例传递到组件。我尝试按照堆栈溢出中的文档和示例进行操作,但无法使其正常工作。我正在尝试传递一个名为“funds”的道具,下面是我的脚本和组件:

main.js

new Vue({
  el: '#app',
  router,
  components: {
    App
  },
  data: {
    funds: [...]
  },
  template: '<App :funds="funds"/>'
});

App.vue

<template>
  <div id="app">
    ...
    <router-view :funds="funds" />
  </div>
</template>

<script>
  export default {
    name: 'App'
  }
</script>

Vue.use(Router)

export default new Router({
 routes: [
   ...
   {
     path: '/funds',
     name: 'funds',
     component: List
   }
 ]
})

List.vue

<template>
  <div>
    <table class="table table-striped">
      <thead>
        <tr>
          <th scope="col">#</th>
          <th scope="col">Name</th>
        </tr>
      </thead>
      <tbody>
        <tr v-for="fund in funds" :key="fund.name">
          ...
        </tr>
      </tbody>
    </table>
  </div>
</template>

<script>
  export default {
    name: 'List',
    props: ['funds']
  }
</script>

我在控制台中看到一条警告:

[Vue warn]: Property or method "funds" is not defined on the instance but referenced during render. Make sure that this property is reactive, either in the data option, or for class-based components, by initializing the property.

found in

---> <App> at src/App.vue
       <Root>

但是,当我运行它时,我看不到表中的任何行正在呈现,实际上表只是空的(只有标题)。我是否遗漏了链条中的某些内容?

【问题讨论】:

  • 你有什么问题?
  • 已更新。希望现在有意义:)
  • 在App.vue中,你也需要定义props,props: ['funds']

标签: vue.js


【解决方案1】:

App.vue中,也需要添加props,如下图;

<template>
  <div id="app">
    ...
    <router-view :funds="funds" />
  </div>
</template>

<script>
  export default {
    name: 'App',
    props: ['funds']
  }
</script>

Vue.use(Router)

export default new Router({
 routes: [
   ...
   {
     path: '/funds',
     name: 'funds',
     component: List
   }
 ]
})

【讨论】:

  • 啊,缺少的链接!谢谢
猜你喜欢
  • 2018-08-03
  • 1970-01-01
  • 2018-06-20
  • 2019-06-20
  • 2018-08-28
  • 2020-07-26
  • 1970-01-01
  • 2017-08-01
  • 2021-02-28
相关资源
最近更新 更多