如何从父路由访问数据到子路由?
How can I access data from a parent route into the childs route?
我有 2 个路由组件:
1) 人员产品列表组件
2) 产品详情组件
列表显示了产品,然后有一个路由器-link 使用路由器 definition/scope 中的历史记录到该产品。
我想要实现的是将数据从父列表路由获取到子详细信息产品模板中。
到目前为止它没有用,我想知道出了什么问题。这是我的代码:
Vue.use(VueResource);
Vue.use(VueRouter);
Vue.config.devtools = true;
// const Home = { template: '<div>home</div>' }
const PeopleListing = ('people-listing', {
template: '#people-listing-template',
delimiters: ['${', '}'],
data: function() {
return {
products: []
}
},
mounted: function() {
this.getAllProducts();
},
methods: {
getAllProducts: function(){
this.$http.get('/collections/homepage/products.json').then(function (response) {
$.each(response.data.products, function(key, value) {
var product = value;
this.products.push(product);
}.bind(this));
}.bind(this), function (response) {
console.log('error getting beers from the pivot table');
});
}
},
props: ['people-listing']
});
const ProductDetail = ('product-detail', {
template: '#product-detail-template',
delimiters: ['${', '}'],
data: function() {
var filtered = this.$parent.data.products.filter(function(item) {
return (item.handle == parent.handle) ? item : false;
});
return {
product: filtered[0]
}
},
props: ['product']
});
const router = new VueRouter({
mode: 'history',
routes: [
{path: '/', component: PeopleListing},
{name: 'product', path: '/products/:handle', component: ProductDetail }
]
});
new Vue({
router,
el: "#home-container",
delimiters: ['${', '}'],
methods: {
// Load nav in case we need to preload it later
// on for speed
prerenderLink: function(e) {
var head = document.getElementsByTagName("head")[0];
var refs = head.childNodes;
ref = refs[ refs.length - 1];
var elements = head.getElementsByTagName("link");
Array.prototype.forEach.call(elements, function(el, i) {
if (("rel" in el) && (el.rel === "prerender"))
el.parentNode.removeChild(el);
});
var prerenderTag = document.createElement("link");
prerenderTag.rel = "prerender";
prerenderTag.href = e.currentTarget.href;
ref.parentNode.insertBefore(prerenderTag, ref);
},
}
});
然后在模板中我得到了这个:
<section id="home-container" class="parralex-scroll">
<div class="wrapper">
<div class="grid">
<template id="people-listing-template">
<div class="wrapper grid" id="start-parralex">
<div class="grid__item large--one-third medium--one-whole no-padding" v-for="product in products" v-cloak>
<router-link :to="{ name: 'product', params: { handle: product.handle }}">
<div class="inner-container relative">
<div class="pad-normal absolute top-0 left-0 large--one-whole">
<p class="lyon-text">
${ product.title }, <span>£${ product.variants[0].price }</span>
</p>
<p class="univers uppercase smaller body-size">
Buy now
</p>
</div>
<div v-for="image in product.images">
<img class="scale-with-grid archives-image" v-bind:src="image.src" v-bind:alt="image.id">
</div>
</div>
</router-link>
</div>
<router-view></router-view>
</div>
</template>
<template id="product-detail-template">
<div>
<h1>{{ $route }}</h1>
</div>
</template>
</div>
</div>
</section>
这也是我在下面玩的数据 api 的想法:
{
"products": [
{
"id": 8494684881,
"title": "Long Shirt Dress (Gargrave)",
"handle": "copy-of-long-shirt-dress-tudor",
"body_html": "",
"published_at": "2017-01-19T22:24:00-11:00",
"created_at": "2017-01-19T23:25:58-11:00",
"updated_at": "2017-01-19T23:38:05-11:00",
"vendor": "in-grid",
"product_type": "Shirt Dress",
"tags": [],
"variants": [],
"images": [],
"options": []
},
是否可以将父数据导入子路由以获取该产品的详细信息?
Vuex 就是你的答案。
您需要将数据存储在存储中才能真正正确地在组件之间共享数据。
只需遵循 Vuex 文档:https://vuex.vuejs.org/en/intro.html
初始化您的商店,创建必要的actions/mutations
并在您的子组件中获取数据,您只需要使用:
computed() {
someData: {
return this.$store.states.someDataFromParaent
}
}
我有 2 个路由组件:
1) 人员产品列表组件 2) 产品详情组件
列表显示了产品,然后有一个路由器-link 使用路由器 definition/scope 中的历史记录到该产品。
我想要实现的是将数据从父列表路由获取到子详细信息产品模板中。
到目前为止它没有用,我想知道出了什么问题。这是我的代码:
Vue.use(VueResource);
Vue.use(VueRouter);
Vue.config.devtools = true;
// const Home = { template: '<div>home</div>' }
const PeopleListing = ('people-listing', {
template: '#people-listing-template',
delimiters: ['${', '}'],
data: function() {
return {
products: []
}
},
mounted: function() {
this.getAllProducts();
},
methods: {
getAllProducts: function(){
this.$http.get('/collections/homepage/products.json').then(function (response) {
$.each(response.data.products, function(key, value) {
var product = value;
this.products.push(product);
}.bind(this));
}.bind(this), function (response) {
console.log('error getting beers from the pivot table');
});
}
},
props: ['people-listing']
});
const ProductDetail = ('product-detail', {
template: '#product-detail-template',
delimiters: ['${', '}'],
data: function() {
var filtered = this.$parent.data.products.filter(function(item) {
return (item.handle == parent.handle) ? item : false;
});
return {
product: filtered[0]
}
},
props: ['product']
});
const router = new VueRouter({
mode: 'history',
routes: [
{path: '/', component: PeopleListing},
{name: 'product', path: '/products/:handle', component: ProductDetail }
]
});
new Vue({
router,
el: "#home-container",
delimiters: ['${', '}'],
methods: {
// Load nav in case we need to preload it later
// on for speed
prerenderLink: function(e) {
var head = document.getElementsByTagName("head")[0];
var refs = head.childNodes;
ref = refs[ refs.length - 1];
var elements = head.getElementsByTagName("link");
Array.prototype.forEach.call(elements, function(el, i) {
if (("rel" in el) && (el.rel === "prerender"))
el.parentNode.removeChild(el);
});
var prerenderTag = document.createElement("link");
prerenderTag.rel = "prerender";
prerenderTag.href = e.currentTarget.href;
ref.parentNode.insertBefore(prerenderTag, ref);
},
}
});
然后在模板中我得到了这个:
<section id="home-container" class="parralex-scroll">
<div class="wrapper">
<div class="grid">
<template id="people-listing-template">
<div class="wrapper grid" id="start-parralex">
<div class="grid__item large--one-third medium--one-whole no-padding" v-for="product in products" v-cloak>
<router-link :to="{ name: 'product', params: { handle: product.handle }}">
<div class="inner-container relative">
<div class="pad-normal absolute top-0 left-0 large--one-whole">
<p class="lyon-text">
${ product.title }, <span>£${ product.variants[0].price }</span>
</p>
<p class="univers uppercase smaller body-size">
Buy now
</p>
</div>
<div v-for="image in product.images">
<img class="scale-with-grid archives-image" v-bind:src="image.src" v-bind:alt="image.id">
</div>
</div>
</router-link>
</div>
<router-view></router-view>
</div>
</template>
<template id="product-detail-template">
<div>
<h1>{{ $route }}</h1>
</div>
</template>
</div>
</div>
</section>
这也是我在下面玩的数据 api 的想法:
{
"products": [
{
"id": 8494684881,
"title": "Long Shirt Dress (Gargrave)",
"handle": "copy-of-long-shirt-dress-tudor",
"body_html": "",
"published_at": "2017-01-19T22:24:00-11:00",
"created_at": "2017-01-19T23:25:58-11:00",
"updated_at": "2017-01-19T23:38:05-11:00",
"vendor": "in-grid",
"product_type": "Shirt Dress",
"tags": [],
"variants": [],
"images": [],
"options": []
},
是否可以将父数据导入子路由以获取该产品的详细信息?
Vuex 就是你的答案。 您需要将数据存储在存储中才能真正正确地在组件之间共享数据。
只需遵循 Vuex 文档:https://vuex.vuejs.org/en/intro.html
初始化您的商店,创建必要的actions/mutations 并在您的子组件中获取数据,您只需要使用:
computed() {
someData: {
return this.$store.states.someDataFromParaent
}
}