admin管理员组文章数量:1405393
This function is in a ponent outside router view.
goToMarkets(){
this.$router.push({path: '/markets', params: {stock: this.model}})
}
But the prop is undefined in the "Markets" ponent
Router:
const routes = [
{
path: '/markets',
name: 'Markets',
ponent: Markets,
props: true
},]
Markets ponent:
props: {
stock: Object
},
The prop is undefined in the Markets ponent even though it's not before it is passed (in the first function).
This function is in a ponent outside router view.
goToMarkets(){
this.$router.push({path: '/markets', params: {stock: this.model}})
}
But the prop is undefined in the "Markets" ponent
Router:
const routes = [
{
path: '/markets',
name: 'Markets',
ponent: Markets,
props: true
},]
Markets ponent:
props: {
stock: Object
},
The prop is undefined in the Markets ponent even though it's not before it is passed (in the first function).
Share edited Nov 4, 2021 at 7:07 mismaah asked Mar 20, 2020 at 8:17 mismaahmismaah 3561 gold badge7 silver badges26 bronze badges1 Answer
Reset to default 7There's a couple of things happening here.
Firstly, route params form part of the URL and are defined upfront in the route definition.
Given the route definition:
{
path: '/markets/:myparam',
name: 'markets',
}
then you would transition to it like this:
this.$router.push({ name: 'markets', params: { myparam: 'foo' } })
and the URL would look like:
/markets/foo
You haven't defined any params for your route, so it won't work.
Secondly, the params must be strings or numbers. You can't pass objects as params. There is just no way to do this; it isn't supported. I think the philosophy here is that the URL alone should contain all router state.
One way to solve this is to pass an ID and then fetch the object from the ID from some shared store (e.g. Vuex).
I don't know what your stock
model is, but perhaps the param could be just a string like msft
so the URL is like /markets/msft
? Can you make it work without needing to pass the full object?
If it's a small object, you can deconstruct the properties of the object into query parameters. But you cannot pass an object instance along with the route transition like you would pass an object as an argument to a function call.
本文标签: javascriptPass object as prop when pushing routeStack Overflow
版权声明:本文标题:javascript - Pass object as prop when pushing route - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744883245a2630334.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论