ChatGPT解决这个技术问题 Extra ChatGPT

将 props 传递给 Vue-router 实例化的 Vue.js 组件

假设我有一个像这样的 Vue.js 组件:

var Bar = Vue.extend({
    props: ['my-props'],
    template: '<p>This is bar!</p>'
});

当 vue-router 中的某些路由像这样匹配时,我想使用它:

router.map({
    '/bar': {
        component: Bar
    }
});

通常为了将“myProps”传递给组件,我会做这样的事情:

Vue.component('my-bar', Bar);

在html中:

<my-bar my-props="hello!"></my-bar>

在这种情况下,当路由匹配时,路由器会自动在 router-view 元素中绘制组件。

我的问题是,在这种情况下,如何将道具传递给组件?


l
lukpep
<router-view :some-value-to-pass="localValue"></router-view>

并在您的组件中添加道具:

props: {
      someValueToPass: String
    },

vue-router 将匹配组件中的 prop


或者,可以在此处找到 props: ["prop1", "prop2", "prop3"] 更多内容vuejs.org/guide/components.html#Props
@lukpep 我遇到了类似的情况,当我在 <router-view> 上绑定我的道具时,我收到以下错误 Attribute ":my_prop" is ignored on component <router-view> because the component is a fragment instance。知道有什么问题吗?
这可能是个坏主意。当用户直接通过 URL 访问时会发生什么?
另外,这个道具不会被传递给所有路由组件,而不仅仅是那些实际使用它的人吗?
那么,一个人是如何真正达到所要求的结果的呢?
c
ctf0

遗憾的是,没有一个上一个解决方案实际上回答了这个问题,所以这里有一个来自 quora

基本上docs没有解释清楚的部分是

当 props 设置为 true 时,route.params 将被设置为组件 props。

因此,通过路由发送道具时您真正需要的是将其分配给 params 键 ex

this.$router.push({
    name: 'Home',
    params: {
        theme: 'dark'
    }
})

所以完整的例子是

// component
const User = {
  props: ['test'],
  template: '<div>User {{ test }}</div>'
}

// router
new VueRouter({
  routes: [
    { 
      path: '/user',
      component: User,
      name: 'user',
      props: true 
    }
  ]
})

// usage
this.$router.push({
    name: 'user',
    params: {
        test: 'hello there' // or anything you want
    }
}) 

这是否意味着这只能通过命名路由来实现?
没有 router.vuejs.org/guide/essentials/…,但最好使用命名路由“更易于维护”
J
Jegadesh B S

在路由器中,

const router = new VueRouter({
  routes: [
    { path: 'YOUR__PATH', component: Bar, props: { authorName: 'Robert' } }
  ]
})

<Bar /> 组件内部,

var Bar = Vue.extend({
    props: ['authorName'],
    template: '<p>Hey, {{ authorName }}</p>'
});

L
Lissy93

这个问题很老,所以我不确定在提出问题时是否存在 Function mode,但它只能用于传递正确的道具。它仅在路由更改时调用,但所有 Vue 反应性规则适用于您传递的任何内容,如果它已经是反应性数据。

// Router config:
components: {
  default: Component0,
  named1: Component1
},
props: {
  default: (route) => {
    // <router-view :prop1="$store.importantCollection"/>
    return {
      prop1: store.importantCollection
    }
  },
  named1: function(route) {
    // <router-view :anotherProp="$store.otherData"/>
    return {
      anotherProp: store.otherData
    }
  },
}

请注意,这仅适用于您的 prop 函数的范围,以便它可以看到您要传递的数据。 route 参数不提供对 Vue 实例、Vuex 或 VueRouter 的引用。此外,named1 示例演示 this 也未绑定到任何实例。这似乎是设计使然,因此状态仅由 URL 定义。由于这些问题,最好使用在标记中接收正确道具并让路由器切换它们的命名视图。

// Router config:
components:
  {
    default: Component0,
    named1: Component1
  }
<!-- Markup -->
<router-view name="default" :prop1="$store.importantCollection"/>
<router-view name="named1" :anotherProp="$store.otherData"/>

使用这种方法,您的标记声明哪些视图是可能的并设置它们的意图,但路由器决定激活哪些视图。


非常感谢。文档绝对应该包含组件和功能模式的示例,用法与单个“组件”的用法不同。无论如何,由于紧急需求,我有点讨厌使用语法略有不同的组件和组件选项。
命名视图似乎是我需要的,但我得到一个 linting 错误“模板根需要一个元素”你碰巧知道解决这个问题吗?
您需要将模板代码封装在一个元素中,例如 <div>
a
aboutqx
 const User = {
      props: ['id'],
      template: '<div>User {{ id }}</div>'
    }
    const router = new VueRouter({
      routes: [
        { path: '/user/:id', component: User, props: true }

        // for routes with named views, you have to define the props option for each named view:
        {
          path: '/user/:id', 
          components: { default: User, sidebar: Sidebar },
          props: { default: true, sidebar: false }
        }
      ]
    })

对象模式

const router = new VueRouter({
  routes: [
    { path: '/promotion/from-newsletter', component: Promotion, props: { newsletterPopup: false } }
  ]
})

这是官方的回答。 link


在这种情况下,人们实际上会在哪里传递字符串“hello”,正如所问的那样?
进一步阅读官方文档:“当 props 设置为 true 时,route.params 将被设置为组件 props。”作者在问如何设置自定义值“hello”
我喜欢人们只是复制和粘贴无法回答问题的官方文档。我们都看过官方文档,它没有回答 OP 的问题,所以在没有解释或澄清的情况下再次粘贴它并走开不会增加帖子的价值。
有没有办法传递 app.data 中定义的绑定数据数组? (new Vue({ data: ...)
佚名

利用:

this.$route.MY_PROP

获取路线道具


他想先设置它。